Solved

Printing a text file from vb.net to serial port quitting after about 56 lines.

Posted on 2006-07-17
9
498 Views
Last Modified: 2010-04-23
I’m using a streamreader to send data from a text file to an old Serial Plate Embosser connected to the computer’s serial port (using  Generic Text driver).   It reads and sends about 56 lines to the Embosser and then quits.  (The files may have anywhere from a few lines to several hundred lines – each one with the data for one serial plate.)  It sounds like what would fit on a “page” but that shouldn’t be the case?  Any suggestions?  Thanks.
We were previously using the computer as a terminal when the program was in VMS-Basic on an DEC/COMPAQ/HP  Alpha and didn’t have this problem.

VB.NET 2003 code

Public Sub PrintFile(ByVal filePath as string)
      streamToPrint = New StreamReader(filePath)
      Try
            printFont = New Font(“Courier New”, 12)
dim pd as New PrintDocument
            AddHandler pd.PrintPage, AddressOf pd_PrintPage
            Pd.Print()
      Finally
            streamToPrint.Close()
      End Try
End Sub

Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim yPos As Single = 0
        Dim leftMargin As Single = 0 'ev.MarginBounds.Left
        Dim topMargin As Single = 0 'ev.MarginBounds.Top
        Dim line As String = Nothing

        ' Iterate over the file, printing each line.
        While True
            line = streamToPrint.ReadLine()
            If line Is Nothing Then
                Exit While
            End If                
            yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
            ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat)                            
        End While
    End Sub 'pd_PrintPage
0
Comment
Question by:haneri
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Sounds like you are only printing a page, where a page is 56 lines.  There is the e.HasMorePages = true, which you can set until you are done reading lines.

Bob

0
 

Author Comment

by:haneri
Comment Utility
After I had posted the above I did try that:

        If Not (line Is Nothing) Then
            ev.HasMorePages = True
        Else
            ev.HasMorePages = False
        End If

after the While True loop - but am still getting about 56 (it may actually be 57 or 58) lines.  I've found various posts about printing from vb.net to a serial port but have been unable to get any of them to work.  Any other ideas?  Thanks.
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I am assuming that you have a print driver, so if you print to that outside of your application will it print more than 56 lines?

Bob
0
 

Author Comment

by:haneri
Comment Utility
I don't have an answer to that (yet).  I'll experiment with that on Monday.  When I've printed a few Serial Number Tags that way it has messed up the formatting, so I have to do it at a time that we're not busy and be willing to throw away the poorly formatted tags.  So - Monday I'll try.  The other idea I may resort to is to create multiple files of say 50 lines and then "print" all of them.  Thanks for looking at this.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I have to admit that it has been quite a while since I have dealt with a serial printer, and the problems surrounding that, so I might not really be of any help, but I am not afraid of trying.

Bob
0
 

Author Comment

by:haneri
Comment Utility
I opened a text file with notepad and printed about 70 tags (without it quitting at 56 or57), but had some formatting problems that may or may not be able to fix.  So it would appear that my vb code is the culprit.
Any further ideas?  Thanks.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
How many times is e.Graphics.DrawString called?

Bob
0
 

Author Comment

by:haneri
Comment Utility
I ran the program from my computer which has the output directed to a laser printer (rather than the embosser) on a file with 290 entries.  I used a counter in a debug statement and the drawstring is called 290 times.  The output on the laser printer gives one page (again aobut 56 lines and quits.  But, this gives me an idea that I'll try tomorrow am.  I put paging back in (say 50 lines and then call for a new page), but then have the first line be the printer initialization commands.  I tried this previously without the additional initialization commands and it didn't work.  Thanks for getting me thinking.
0
 

Author Comment

by:haneri
Comment Utility
Resending the embosser initialization code after every 50 lines did the trick.  I'm including the working code just in case it helps someone else.  Bob, while you didn't "give" me the answer you certainly helped me to look in the right directio.  Thanks.

    Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
     
        Dim yPos As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = 0 'ev.MarginBounds.Left
        Dim topMargin As Single = 0 'ev.MarginBounds.Top
        Dim line As String = Nothing
        ' Iterate over the file, printing each line.
        While count < 50
            If count = 0 Then
                line = strHeaderLine  'contains serial device initialization code
            Else
                line = streamToPrint.ReadLine()
            End If
            If line Is Nothing Then
                Exit While
            End If
            yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
            ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat)
            count += 1
        End While

        ' If more lines exist, print another page.
        If Not (line Is Nothing) Then
            count = 0
            ev.HasMorePages = True
        Else
            ev.HasMorePages = False
        End If
    End Sub 'pd_PrintPage
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now