Solved

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

Posted on 2006-07-17
9
505 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
ID: 17140593
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
ID: 17140867
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
ID: 17141299
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:haneri
ID: 17154840
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17160242
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
ID: 17167498
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
ID: 17168721
How many times is e.Graphics.DrawString called?

Bob
0
 

Author Comment

by:haneri
ID: 17169902
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
ID: 17177649
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

680 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