Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi... i'm new to programming and i'm learning vb.net through some ebooks and internet resources.. I have developed a simple class to work with serial port to handle a POS Display.. but the problem is the port does not close and when i close the program and reopen it, the pos display doesn't work.... please help me, and sorry for my bad english... below is my code...


VB
Imports System.IO
Public Class LineDisplay
    Implements IDisposable
    Dim WithEvents SP As New IO.Ports.SerialPort
    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If SP IsNot Nothing Then
                SP.Dispose()
                SP = Nothing
            End If
        End If
    End Sub
    Protected Overrides Sub Finalize()
        Dispose(False)
    End Sub
    Public Function ConnectDisplay()
        Try
            SP.PortName = "COM1"
            SP.BaudRate = 9600
            SP.Parity = Ports.Parity.None
            SP.StopBits = IO.Ports.StopBits.One
            SP.DataBits = 8
            SP.DtrEnable = True
            SP.RtsEnable = True
            SP.Encoding = System.Text.Encoding.ASCII
            SP.Handshake = IO.Ports.Handshake.None
        Catch ex As Exception

        End Try
        If SP.IsOpen = True Then
            Try
                SP.Write(Chr(&HC))
                SP.Write(Chr(2) & Chr(5) & Chr(68) & Chr(8) & Chr(3))
                Return True
            Catch ex As Exception
                Return False
            End Try
        Else
            Try
                SP.Open()
                Return True
            Catch ex As Exception
                MessageBox.Show("Line Display is not working... Please Re-check the device", "Malsha Fashion", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
                Return False
            End Try
        End If
    End Function

    Public Function WriteText(ByVal Text1 As String, ByVal Text2 As String)
        Dim line1 As String = Text1
        Dim line2 As String = Text2
        Dim rest As Integer
        Dim tab As String = ""
        rest = (20 - line2.Length)

        For i As Integer = 0 To rest - 1
            tab += " "
        Next

        Try
            SP.Write(Chr(&HC))
            SP.Write(Chr(&H1B) & Chr(&H5B) & Chr(&H4C))
            SP.Write(Chr(&H1B) & Chr(&H51) & Chr(&H42) & tab + line2 & Chr(&HD))
            SP.Write(Chr(&H1B) & Chr(&H51) & Chr(&H44) & " " + Text1 & Chr(&HD))
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
       
        Return True
    End Function

    Public Function WriteTotal(ByVal Text2 As String)
        Dim line2 As String = Text2
        Dim rest As Integer
        Dim tab As String = ""
        rest = (20 - line2.Length)

        For i As Integer = 0 To rest - 1
            tab += " "
        Next

        Try
            SP.Write(Chr(&HC))
            SP.Write(Chr(&H1B) & Chr(&H51) & Chr(&H41) & "TOTAL AMOUNT" & Chr(&HD))
            SP.Write(Chr(&H1B) & Chr(&H51) & Chr(&H42) & tab + line2 & Chr(&HD))
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
       
        Return True
    End Function

    Public Function DisplayDemo()
        Try
            SP.Write(Chr(&HC))
            'SP.Write(Chr(2) & Chr(5) & Chr(68) & Chr(8) & Chr(3))
            SP.Write(Chr(&H1B) & Chr(&H51) & Chr(&H44) & " WELCOME TO MALSHA FASHION CENTRE" & Chr(&HD))
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Return True
    End Function

    Public Function DisplayThanks()
        Try
            SP.Write(Chr(&HC))
            SP.Write(Chr(&H1B) & Chr(&H51) & Chr(&H42) & "     COME AGAIN.    " & Chr(&HD))
            SP.Write(Chr(&H1B) & Chr(&H51) & Chr(&H44) & " THANK YOU FOR SHOPPING WITH US." & Chr(&HD))
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Return (True)
    End Function

    Public Function CloseDisplay()
        Try
            SP.Write(Chr(&HC))
            SP.Write(Chr(2) & Chr(5) & Chr(68) & Chr(8) & Chr(3))
            SP.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Return True
    End Function
End Class
Posted
Updated 21-Dec-12 2:35am
v3
Comments
Philippe Mori 21-Dec-12 9:23am    
Yoiu should remove useless code before posting like the DisplayThanks method and you should show how you use the class and give more informations about what you have tried.

We can't help that much - and a lot of that is your own fault! :laugh:

You are swallowing exceptions that would (probably) tell you why there is a problem:
VB
Catch ex As Exception

End Try
Instead of this, try logging or reporting the problem with its error message (and any InnerException) - that way tou get some information which may help you to solve the problem!
 
Share this answer
 
Comments
OriginalGriff 21-Dec-12 8:36am    
If none of these throw exceptions, then they are working...so what isn't happening?
shasheeke 21-Dec-12 8:55am    
here is the problem... when i work with the port for the first time, it works perfectly.. but when i close the port and try to reopen it.. it doesn't work...
here's the error message which displays when i try to open the port.....

http://i298.photobucket.com/albums/mm260/shasheeke/Capture-1_zpsa00423d5.jpg
OriginalGriff 21-Dec-12 8:58am    
Which implies that the port is open - so you probably haven't called close. Add some code which logs events to a text file:
File.AppendText should do it nicely, and add logging code for every serial port related action. Then when it fails, you should have a record in the log of what happened up to that point - it may be for some reason that your class is not getting Disposed, so the port is still in use to a (dead) application. You need to know what is happening first, before you can fix it.
Try looking at the SerialPort.IsOpen property before you try to open it - it should be closed because this is a new instance (if it is a new application)
shasheeke 21-Dec-12 9:28am    
thanks.. i've solved it . there's a problem in my class.. i don't know what... i used the serial port component directly and now it works perfectly..
Philippe Mori 21-Dec-12 9:10am    
You should have made an hyperlink so that it would be much easier to see the image as it is hard to select text on CodeProject site (at least with IE 10).
http://i298.photobucket.com/albums/mm260/shasheeke/Capture-1_zpsa00423d5.jpg
Effectivelly as mentionned by OriginalGriff, you should not have so much Try/Catch in your code. Typically, you should only have them when necessary and generally not at the Library level but at the UI level.

On the other hand, you might need to add Finally clause where the port should be closed if an execption occurs.


But if you are closing the application and the port cannot be opened after that, the problem is probably not in the application itself. I think that Windows will close a port when an application exits.

Also try simple things like opening and closing the port and then restart the application. Same thing without closing it... That kind of tests will help you understand the actual behavior (your serial driver and its device).

In my own testing, if I kill an application that has an opened port, I can restart the application and open the port again without any problem.

So if you quit the application and your driver and your device is working properly (and maybe wait a few seconds for timeouts), you should be able to open the port even if it was not explicitly closed as Windows so close any handle associated to an application when the application is closed.

By the way, you have to ensure that the application is really closed (and not still waiting something). Open Task Manager to see if the application is running and if so kill it.
 
Share this answer
 
Comments
shasheeke 22-Dec-12 5:02am    
Thanks for the info... please don't get angry on me, coz i'm still learning.. you're saying that i shouldn't use try/catch statements so much.. why is that? how do we catch an error?
Philippe Mori 22-Dec-12 8:46am    
Basically, you should have done more effort to cleanup your code (remove unnecessary functions and error handling) and show minimal caller code needed to reproduce the problem.

It make no sense that a library class like LineDisplay handle exceptions as it make the code not reusable if you want to do something else than displaying message boxes when an error occurs. Exception handling should be done at the caller if necessary. This is basic programming rules. In particular the first Try/Catch in ConnectDisplay is useless and swallowing errors is rarely the proper thing to do.

By the way, I would think that your Dispose method should call CloseDisplay if the display was opened.
shasheeke 22-Dec-12 10:03am    
thanks.... i appreciate ur help...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900