简体   繁体   English

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

[英]how to send and receive hex data to serial port from VB.NET

Using vb.NET, i need to manage a device using serial port.使用 vb.NET,我需要使用串口管理设备。 I use for this, SerialPortStream from Nuget.为此,我使用来自 Nuget 的 SerialPortStream。 Here is the GitHub url.这是 GitHub url。 https://github.com/jcurl/serialportstream https://github.com/jcurl/serialportstream

I have info from the device provider and the message format used.我有来自设备提供商的信息和使用的消息格式。 Shown in this image: Image with message format used显示在此图像中:使用消息格式的图像

I open correctly port and correctly connect to device and the event for receive data from the serial port is also ok and is fired when i send something.我正确打开端口并正确连接到设备,从串口接收数据的事件也可以,并且在我发送内容时被触发。

For receive i use this接收我用这个

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

I know what messages i need to send and what messages i expect to receive looking logs i have from other apps running same device.我知道我需要发送哪些消息,以及我希望从运行同一设备的其他应用程序中收到哪些消息。

But i don't know how to format and send the messages from my app and i don't know how to manage the s_DataReceived event to get the responses from the device.但我不知道如何格式化和发送来自我的应用程序的消息,也不知道如何管理 s_DataReceived 事件以从设备获取响应。

Any help will be appreciated.任何帮助将不胜感激。 Thanks in advance提前致谢

The way for send is:发送方式为:

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

for receive is:接收是:

dataArrived = s.ReadExisting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM