Link to home
Start Free TrialLog in
Avatar of haneri
haneri

asked on

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

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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

Avatar of haneri
haneri

ASKER

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.
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
Avatar of haneri

ASKER

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.
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
Avatar of haneri

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of haneri

ASKER

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.
Avatar of haneri

ASKER

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