Solved

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

Posted on 2006-07-17
9
507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

628 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