通过COM从标尺请求重量值

时间:2015-12-14 17:46:53

标签: vb.net serial-port port

我一直在尝试使用COM串口从两个不同的比例中检索权重值。

秤是CAS ER Plus和UWE AM-15K。

我已经设法创建了一个小程序来启动PC和Scales之间的通信,但我遇到了问题而且我一直无法找到解决方案。

我可以发送一个ENQ命令,并且我从两个等级成功接收到ACK,但是当我发送DC1命令来检索权重时,我总是从消息中得到各种变化

  

“十六进制数必须小于7fffffffffffffff”

  

“十六进制值必须是32个字符的最大长度。”

我配置COM端口的代码如下:

Try
        comport.PortName = "COM1"
        comport.BaudRate = 9600
        comport.Parity = Parity.None
        comport.DataBits = 8
        comport.StopBits = StopBits.One
        comport.Handshake = Handshake.XOnXOff
        If DTRCheck.Checked Then
            comport.DtrEnable = True
        End If
        If RTSCheck.Checked Then
            comport.RtsEnable = True
        End If
        'Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("IBM860")
        Dim encoding As System.Text.Encoding = System.Text.Encoding.ASCII
        comport.Encoding = encoding
        comport.Open()
        erroCOM = False
    Catch ex As Exception
        erroCOM = True
    End Try

发送命令的代码:

 If comport.IsOpen Then
        TxtValor.Clear()
        Try
            If EnqComBtn.Checked Then 'Verify if the scale is "listening"
                ListBox1.Items.Add("PC: ENQ - >")
                comport.Write(New Byte() {&H5}, 0, 1)
                Thread.Sleep(20)
                ListBox1.Items.Add("PC: DC1 - >")
                comport.Write(New Byte() {&H11}, 0, 1)
            End If
            If DC1ComBtn.Checked Then 'Request weight value
                ListBox1.Items.Add("PC: DC1 - >")
                comport.Write(New Byte() {&H11}, 0, 1)
                Thread.Sleep(20)
            End If
        Catch ex As Exception

        End Try

    End If

发送命令后,我让程序读取通过COM串行端口的DataReceived事件接收到的值,并将Hex值转换为Decimal。

编辑:

这是我用来读取接收的字节数并将它们从字节转换为十六进制的代码。我在StackOverflow上的另一篇文章中找到了这段代码并编辑了空格部分。

Private Sub comport_DataReceived(sender As Object,e As SerialDataReceivedEventArgs)处理comport.DataReceived

    Dim RXByte As Byte
    Do
        RXCnt = 0
        Do
            RXByte = comport.ReadByte
            RXArray(RXCnt) = LookUpTable(RXByte >> 4) ' Convert each byte to two hexadecimal characters 
            RXCnt = RXCnt + 1
            RXArray(RXCnt) = LookUpTable(RXByte And 15)
            RXCnt = RXCnt + 1
        Loop Until (comport.BytesToRead = 0)
        '----- End of communication protocol handling ------------------------------------------------------------- 

        Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread 

    Loop Until (comport.BytesToRead = 0)  ' Don't return if more bytes have become available in the meantime 

End Sub

之前是否有人发现此问题,如果是,您是否找到了解决方案? 谢谢!

1 个答案:

答案 0 :(得分:-1)

我找到了解决问题的方法!

事情是我逐字节地读取BytesToRead,我不得不做ReadExisting。

上一个代码:

Dim RXByte As Byte
    'Dim RXBytes() As Byte
    Dim bytString As String = ""
    Do
        RXCnt = 0
        Do
            RXByte = comport.ReadByte
            RXArray(RXCnt) = LookUpTable(RXByte >> 4) ' Convert each byte to two hexadecimal characters 
            RXCnt = RXCnt + 1
            RXArray(RXCnt) = LookUpTable(RXByte And 15)
            RXCnt = RXCnt + 1
        Loop Until (comport.BytesToRead = 0)
        '----- End of communication protocol handling ------------------------------------------------------------- 

    Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread 

    Loop Until (comport.BytesToRead = 0)  ' Don't return if more bytes have become available in the meantime 

解决方案:

RXValue = ""

    Try

        RXValue = comport.ReadExisting()
        If RXValue <> "" Then
            Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread 
        Else

        End If
    Catch ex As Exception

    End Try

希望这可以帮助其他有同样问题的人!