0

I am writing software for a POS terminal. This terminal has a printer that is attached to a cash drawer. I need to send a code to the printer, and listen for what is returned to determine whether or not the cash drawer is open. Using MSComm, I had logic that worked in VB6, so I know the actual Hex code I am sending is correct.

This code always returns "Error: Serial Port read timed out". I do not know what I am doing wrong with the read portion. Please advise, how do I listen for what the port is sending back as a response?

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    ' Receive strings from a serial port. 
    Dim returnStr As String = ""
    Dim x As Integer = 0
    Dim com3 As IO.Ports.SerialPort = Nothing

    logfile.WriteLine("STARTCASHDRAWERSTATUSCHECK")
    Try
        com3 = My.Computer.Ports.OpenSerialPort("COM3")
        com3.WriteLine(ChrW(&H1B) & ChrW(&H75) & ChrW(&H0))
        com3.BaudRate = SetPortBaudRate(9600)
        com3.Parity = IO.Ports.Parity.None
        com3.DataBits = SetPortDataBits(8)
        com3.StopBits = SetPortStopBits(1)
        com3.Handshake = IO.Ports.Handshake.RequestToSend
        com3.ReadTimeout = 10000
        Do
            x = x + 1
            Dim Incoming As String = com3.ReadLine()
            logfile.WriteLine(x & "incoming" & Incoming & "x")
            If Incoming Is Nothing Then
                logfile.WriteLine("Button2resultEXITDO" & x)
                Exit Do
            Else
                returnStr &= Incoming & vbCrLf
            End If
            If x > 10 Then
                Exit Do
            End If
        Loop
    Catch ex As TimeoutException
        returnStr = "Error: Serial Port read timed out."
    Finally
        If com3 IsNot Nothing Then com3.Close()
    End Try

    logfile.WriteLine("Button2result:" & returnStr)
End Sub

Thanks in advance!

4
  • 1
    You must configure the serial port correctly. Baudrate, Parity, DataBits and StopBits. And Handshake, super important. Start with Handshake.RequestToSend. Try this first with a program like Putty or HyperTerminal so you can be sure that the basic wiring is okay. Oct 5, 2016 at 16:05
  • Please further explain, how to I code this Handshake.requesttosend? I edited my code above to what I thought needed to be added. Please advise further. Thanks! Oct 5, 2016 at 16:30
  • 1
    You need your port configuration to occur before you start writing/reading to the port.
    – helrich
    Oct 5, 2016 at 19:19
  • com3.PortName = "COM3" should replace the first line after the Try. After the ReadTimeout property line, then com3.Open and com3.WriteLine would occur.
    – topshot
    Oct 5, 2016 at 20:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.