vb.net-通过串行接口接收十六进制数据

vb.net-通过串行接口接收十六进制数据,vb.net,serial-port,Vb.net,Serial Port,我有一个设备,我通过串行接口与之通信 通信是用十六进制完成的,所以我发送十六进制,我接收十六进制 示例:如果我发送“AA 00 03 89 18 0A 98 BB”,设备将以“AA 00 02 00 80 82 BB”返回报告 我的目标是以人类可读的方式获得返回值,特别是作为字符串 发送很好,但是接收是我努力奋斗和需要帮助的地方 发送部分: 这是我定义要发送的命令的部分: Public Class ClassRfidWrapper Public Function Command(By

我有一个设备,我通过串行接口与之通信

通信是用十六进制完成的,所以我发送十六进制,我接收十六进制

示例:如果我发送“AA 00 03 89 18 0A 98 BB”,设备将以“AA 00 02 00 80 82 BB”返回报告

我的目标是以人类可读的方式获得返回值,特别是作为字符串

发送很好,但是接收是我努力奋斗和需要帮助的地方


发送部分:

这是我定义要发送的命令的部分:

Public Class ClassRfidWrapper

    Public Function Command(ByVal theCommand As String) As Byte()

        'Versuche.
        Try

            If theCommand = "SetBuzzer" Then
                Dim bytes() As Byte = {&HAA, &H0, &H3, &H89, &H18, &HA, &H98, &HBB}
                Return bytes
            End If

        Catch ex As Exception
            Console.ForegroundColor = ConsoleColor.Red
            Console.WriteLine("Class -> ClassRfidWrapper, Method -> SendCommand, Error -> " & ex.Message)
        End Try

        Return Nothing
    End Function

End Class
这是我向设备发送十六进制消息的部分:

公共子MySendSerialData(ByVal数据为Byte())

端接头

执行以下操作:

SerialInterfaceObject.MySendSerialData(RfidWrapperObject.Command("SetBuzzer"))
将十六进制转换为字符串的函数:

Public Function Bytes_To_String2(ByVal bytes_Input As Byte()) As String
    Dim strTemp As New StringBuilder(bytes_Input.Length * 2)
    For Each b As Byte In bytes_Input
        strTemp.Append(Conversion.Hex(b))
    Next
    Return strTemp.ToString()
End Function

接收部分:

这就是我的问题所在

Public Shared Sub DataReceivedHandler(sender As Object, e As SerialDataReceivedEventArgs)
    Dim sp As SerialPort = CType(sender, SerialPort)
    Dim data As String = sp.ReadExisting()
    'Mitteilung.
    Main.MessageObject.MyMessage("incoming serial data: ", CStr(data), 3)
    'Log [LogDeviceToWrapper]
    Main.LogObject.MyLog(CStr(data), "LogDeviceToWrapper")
End Sub
问题是,我有垃圾

我理解,.ReadExisting是错误的方法,因为它将接收到的数据解释为字符串,所以我需要一个示例代码,说明如何接收数据并将其转换为包含十六进制代码的字节数组,随后我可以使用我的函数Bytes_to_String2将其转换为字符串


感谢您的帮助

将响应缓冲区作为字节数组读取

Public Shared Sub DataReceivedHandler(sender As Object, e As SerialDataReceivedEventArgs)
    Dim sp As SerialPort = CType(sender, SerialPort)

    Dim respSize As Integer = sp.BytesToRead

    Dim respBuffer As Byte() = New Byte(respSize - 1)

    comport.Read(respBuffer, 0, respSize)

    ' Convert to string and additional processing ...
End Sub

我同意,十六进制作为一个字符串不是很好读,但我仍然可以使用它进行调试和比较,这就是我现在想要做的。问题不在于转换,我已经有一个函数为我做了这个。问题是如何从包含十六进制代码的字节数组中的串行接口获取数据?必须与此有关:Dim data As String=sp.ReadExisting()换句话说,问题在于串行接口的DataReceivedHandler…您不能使用ReadExisting()。在获得&HBB之前,您必须一次读取一个字节。假设设备协议将其用作消息结束标记。要正确地执行此操作,您还必须丢弃字节,直到获得&HAA,它确保您与设备同步。BitConverter.ToString()为您提供了一些可以显示的内容。您有没有示例来说明如何正确执行此操作?您正在发送一个字符串,因此我假设您也在接收一个字符串。如果您希望接收“AA”,那么这是字符串“AA”,所以在字节中它应该是0x41 0x41(或65 65),这是。
Public Shared Sub DataReceivedHandler(sender As Object, e As SerialDataReceivedEventArgs)
    Dim sp As SerialPort = CType(sender, SerialPort)

    Dim respSize As Integer = sp.BytesToRead

    Dim respBuffer As Byte() = New Byte(respSize - 1)

    comport.Read(respBuffer, 0, respSize)

    ' Convert to string and additional processing ...
End Sub