如何从VB.NET向串口发送和接收十六进制数据

问题描述

使用 vb.NET,我需要使用串口管理设备。 我为此使用了 Nuget 的 SerialPortStream。这是 GitHub 网址。 https://github.com/jcurl/serialportstream

我有来自设备提供商的信息和使用的消息格式。如图所示: Image with message format used

我正确打开端口并正确连接到设备,从串行端口接收数据的事件也正常,并且在我发送内容时被触发。

接收我用这个

Private Sub s_DataReceived(sender As Object,e As SerialDataReceivedEventArgs) Handles s.DataReceived
        Console.Beep()
        Console.WriteLine("<---- " & s.BytesToRead)
 End Sub

我知道我需要发送哪些消息,以及我希望从运行同一设备的其他应用程序接收哪些信息。

但我不知道如何格式化和从我的应用发送消息,我不知道如何管理 s_DataReceived 事件以从设备获取响应。

任何帮助将不胜感激。 提前致谢

解决方法

发送方式为:

//Operations for windows
Private void WindowsFunc 
{ code } 

//Operations for mac
Private void MacFunc
{ code }

//library's functions
Public void Do
{
    //Performs different operations
    //for different machines

    If (windows)
        WindowsFunc
    else if (Mac)
        MacFunc
}

接收是:

 Dim b() As Byte = New Byte() {&H01,&H02,&H03,&H04,&H05} (and so on... once data is completed.)
 s.Write(b,b.Length)