Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-07-17
9
Medium Priority
?
510 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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