[英]Serial Port Send Data in VB.NET with Packed message
我有《编程手册》协议的手册,但是在编写代码时,vb.net返回错误消息。
以下是字节数组的表结构
位置1 2 3 4 5 6 7 8
名称SOH LEN SEQ CMD DATA后同步码BCC ETX
长度/字节1 1 1 1 0-200 1 4 1
值(01h)(20h-FFh)(20h-FFh)(20h-FFh)(20h-FFh)(05h)(30h-3Fh)(03h)
缩略语:
“ SOH” -(标题开始)打包消息的开始
“ LEN” -从位置2到位置6的字节总数,加上20h的固定偏移量。
“ SEQ” -数据包的序列号。 SLAVE在回复消息中放置相同的“ SEQ”。 如果SLAVE接收到的消息与上次正确接收的消息一样具有相同的“ SEQ”和“ CMD”,则设备将忽略该消息并重复发送到主机的最后一个数据包。
“ CMD” –命令代码
“ DATA” -数据,根据命令。 如果没有数据,则长度字段为零。
“ BCC” –控制和(0000h-FFFFh)。 从位置2到位置6的数据字节总和。控制总和以ASCІІ类型传输(12АВ作为31h 32h3Аh3Вh传输)。
“ ETX” –(TeXt的结尾)打包消息的结尾。
代码示例:
公开课表格1
Private WithEvents sp As System.IO.Ports.SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each y As String In System.IO.Ports.SerialPort.GetPortNames
Me.ComboBox1.Items.Add(y)
Next
sp = New IO.Ports.SerialPort("COM4")
sp.StopBits = IO.Ports.StopBits.One
sp.BaudRate = 9600
sp.DataBits = 8
sp.Parity = IO.Ports.Parity.None
sp.Handshake = IO.Ports.Handshake.None
sp.Open()
Dim by As Byte = &H20
' MsgBox(&H20)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim By(9) As Byte
By(0) = 1
By(1) = &H2 + &H20
By(2) = &H58
By(3) = &H45
By(4) = &H5
Dim S As String = Hex(&H58 Xor &H45)
' MsgBox(S)
By(5) = &H30
By(6) = &H30
By(7) = &H31
By(8) = &H3D
By(9) = &H3
sp.Write(By, 0, By.Length - 1)
' MsgBox(sp.ReadByte)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
Dim rec As System.IO.Ports.SerialPort = sender
MsgBox(Hex(rec.ReadByte))
End Sub
末级
Annael您可以通过调用更新功能并提供500ms的延迟来做到这一点。 现在,在接收数据时,您可以接收输入数据
Dim inputData As String = ""// Declare this as a global variable in the program
Private WithEvents sp As System.IO.Ports.SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each y As String In System.IO.Ports.SerialPort.GetPortNames
Me.ComboBox1.Items.Add(y)//the code you have written to add Serial ports
SerialPort1.PortName = ComboBox1.Text//this will access your com port as your button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim by As Byte = &H20
By(0) = 1
By(1) = &H2 + &H20
By(2) = &H58
By(3) = &H45
By(4) = &H5
Dim S As String = Hex(&H58 Xor &H45)
' MsgBox(S)
By(5) = &H30
By(6) = &H30
By(7) = &H31
By(8) = &H3D
By(9) = &H3
SerialPort1.BaudRate = 115200
SerialPort1.Open()
SerialPort1.Write(By)
Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Threading.Thread.Sleep(500) ' 500ms delay induced to receive the email
inputData = SerialPort1.ReadExisting 'or SerialPort1.ReadLine
Me.Invoke(New EventHandler(AddressOf DoUpdate))
Public Sub DoUpdate()
MsgBox(inputData)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.