从串口读取总是超时

时间:2016-10-05 14:24:48

标签: vb.net serial-port

我正在为POS终端编写软件。该终端具有连接到现金抽屉的打印机。我需要向打印机发送代码,并听取返回的内容以确定现金抽屉是否打开。使用MSComm,我的逻辑在VB6中有效,所以我知道我发送的实际十六进制代码是正确的。

此代码始终返回"错误:串行端口读取超时"。我不知道我在阅读部分做错了什么。请注意,我如何监听端口发回的内容作为响应?

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

提前致谢!

0 个答案:

没有答案