Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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...
 

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 21-Dec-12 3:22am
Edited 21-Dec-12 3:35am
v3
Comments
Philippe Mori at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

We can't help that much - and a lot of that is your own fault! Laugh | :laugh:
 
You are swallowing exceptions that would (probably) tell you why there is a problem:
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!
  Permalink  
Comments
OriginalGriff at 21-Dec-12 8:36am
   
If none of these throw exceptions, then they are working...so what isn't happening?
shasheeke at 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 at 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 at 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 at 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
tiggerc at 21-Dec-12 9:13am
   
how do you create an instance of your class?
do you create an instance display what you want then just leave it.
 
try calling close display in your dispose method.
 
let me know the results.
 
Try
 
dim disp as new yourclass
disp.displaytotal
disp = nothing
VitorHugoGarcia at 21-Dec-12 9:40am
   
I'm curious : Is your app running thru a remote desktop session ?
I had many issues like yours and solved it detecting if the com port exists with System.IO.Ports.SerialPort.GetPortNames() and testing if the port is open with COM_PORT.IsOpen before writing to it.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
shasheeke at 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 at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 460
1 OriginalGriff 220
2 Kornfeld Eliyahu Peter 210
3 DamithSL 210
4 Peter Leow 100
0 OriginalGriff 7,215
1 DamithSL 5,199
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,757
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 21 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100