VB.NET RS232接收十六进制数据

时间:2019-06-27 02:58:47

标签: vb.net

我正在向温度和湿度传感器后续手册发送rs232命令十六进制。 但是结果不能按预期工作。

Dim data As Byte() = {&H1, &H4, &H0, &H1, &H0, &H1, &H60, &HA}
SerialPort1.Write(data, 0, 8)

手动输入结果= 0x131

我的程序= 01 04 02 01 2B F8 BF

Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

    'ReceivedText(SerialPort1.ReadExisting())
    Dim Count As Integer = SerialPort1.BytesToRead
    Dim Buffer(0 To Count - 1) As Byte
    Dim Text2Display As String = ""

    SerialPort1.Read(Buffer, 0, Count)

    For I As Integer = 0 To Buffer.GetUpperBound(0)
        Text2Display += Buffer(I).ToString("X2") & " "
    Next

    ReceivedText(Text2Display)

End Sub

Private Sub ReceivedText(ByVal [text] As String)

    If Me.rtbReceived.InvokeRequired Then
        Dim x As New SetTextCallback(AddressOf ReceivedText)
        Me.Invoke(x, New Object() {(text)})
    Else
        Me.rtbReceived.Text &= [text] & vbNewLine
    End If

End Sub


End Class

0 个答案:

没有答案