4 个解决方案
#1
改出来一个,还没试,你看看就是把字符串转化16进制数据存入数组,然后串口发送即可。
'把字符串转化为字节发送
Function strtobyte(ByVal str As String) As Byte
Dim i, j As Byte
Dim temp As String
Dim l As Integer
Dim data() As Byte
Dim crcH, crcL As Byte
Dim b() As Byte
j = 0
str = Replace(str, " ", "")
l = Len(str) / 2
ReDim b(l - 1)
For i = 0 To l - 1
If j < 2 * l Then
b(i) = Val("&h" & Mid(str, j + 1, 2))
End If
j = j + 2
Next
If Me.COM2.IsOpen Then
Me.COM1.DiscardOutBuffer()
Me.COM1.Write(b, 0, l)
Else
MsgBox("串口出错", , "串口错误")
End If
Return 1
End Function
'把字符串转化为字节发送
Function strtobyte(ByVal str As String) As Byte
Dim i, j As Byte
Dim temp As String
Dim l As Integer
Dim data() As Byte
Dim crcH, crcL As Byte
Dim b() As Byte
j = 0
str = Replace(str, " ", "")
l = Len(str) / 2
ReDim b(l - 1)
For i = 0 To l - 1
If j < 2 * l Then
b(i) = Val("&h" & Mid(str, j + 1, 2))
End If
j = j + 2
Next
If Me.COM2.IsOpen Then
Me.COM1.DiscardOutBuffer()
Me.COM1.Write(b, 0, l)
Else
MsgBox("串口出错", , "串口错误")
End If
Return 1
End Function
#2
没看出来有任何地方需要序列化反序列化
串口数据是byte[]数组
TCP数据包也是byte[]数组
直接发送不就得了?
串口数据是byte[]数组
TCP数据包也是byte[]数组
直接发送不就得了?
#3
至于每2个byte作为一个数据
循环里处理一下不就得了,很难吗
循环里处理一下不就得了,很难吗
#4
Public Function ByteArrayToHexString(ByVal data As Byte()) As String
Dim builder As New StringBuilder((data.Length * 3))
Dim num As Byte
For Each num In data
builder.Append(Convert.ToString(num, &H10).PadLeft(2, "0"c).PadRight(3, " "c))
Next
Return builder.ToString.ToUpper
'C0 00 A6 A2 A4 B2 AA 62 60 84 8E 70 8A 94 A8 EE AE 92 88 8A 62 40 62 AE 92 88 8A 64 40 63 03 F0 60 70 4A 2D 6C 2A 67 4B 5C 3E 3D 0D C0
End Function
Dim builder As New StringBuilder((data.Length * 3))
Dim num As Byte
For Each num In data
builder.Append(Convert.ToString(num, &H10).PadLeft(2, "0"c).PadRight(3, " "c))
Next
Return builder.ToString.ToUpper
'C0 00 A6 A2 A4 B2 AA 62 60 84 8E 70 8A 94 A8 EE AE 92 88 8A 62 40 62 AE 92 88 8A 64 40 63 03 F0 60 70 4A 2D 6C 2A 67 4B 5C 3E 3D 0D C0
End Function
#1
改出来一个,还没试,你看看就是把字符串转化16进制数据存入数组,然后串口发送即可。
'把字符串转化为字节发送
Function strtobyte(ByVal str As String) As Byte
Dim i, j As Byte
Dim temp As String
Dim l As Integer
Dim data() As Byte
Dim crcH, crcL As Byte
Dim b() As Byte
j = 0
str = Replace(str, " ", "")
l = Len(str) / 2
ReDim b(l - 1)
For i = 0 To l - 1
If j < 2 * l Then
b(i) = Val("&h" & Mid(str, j + 1, 2))
End If
j = j + 2
Next
If Me.COM2.IsOpen Then
Me.COM1.DiscardOutBuffer()
Me.COM1.Write(b, 0, l)
Else
MsgBox("串口出错", , "串口错误")
End If
Return 1
End Function
'把字符串转化为字节发送
Function strtobyte(ByVal str As String) As Byte
Dim i, j As Byte
Dim temp As String
Dim l As Integer
Dim data() As Byte
Dim crcH, crcL As Byte
Dim b() As Byte
j = 0
str = Replace(str, " ", "")
l = Len(str) / 2
ReDim b(l - 1)
For i = 0 To l - 1
If j < 2 * l Then
b(i) = Val("&h" & Mid(str, j + 1, 2))
End If
j = j + 2
Next
If Me.COM2.IsOpen Then
Me.COM1.DiscardOutBuffer()
Me.COM1.Write(b, 0, l)
Else
MsgBox("串口出错", , "串口错误")
End If
Return 1
End Function
#2
没看出来有任何地方需要序列化反序列化
串口数据是byte[]数组
TCP数据包也是byte[]数组
直接发送不就得了?
串口数据是byte[]数组
TCP数据包也是byte[]数组
直接发送不就得了?
#3
至于每2个byte作为一个数据
循环里处理一下不就得了,很难吗
循环里处理一下不就得了,很难吗
#4
Public Function ByteArrayToHexString(ByVal data As Byte()) As String
Dim builder As New StringBuilder((data.Length * 3))
Dim num As Byte
For Each num In data
builder.Append(Convert.ToString(num, &H10).PadLeft(2, "0"c).PadRight(3, " "c))
Next
Return builder.ToString.ToUpper
'C0 00 A6 A2 A4 B2 AA 62 60 84 8E 70 8A 94 A8 EE AE 92 88 8A 62 40 62 AE 92 88 8A 64 40 63 03 F0 60 70 4A 2D 6C 2A 67 4B 5C 3E 3D 0D C0
End Function
Dim builder As New StringBuilder((data.Length * 3))
Dim num As Byte
For Each num In data
builder.Append(Convert.ToString(num, &H10).PadLeft(2, "0"c).PadRight(3, " "c))
Next
Return builder.ToString.ToUpper
'C0 00 A6 A2 A4 B2 AA 62 60 84 8E 70 8A 94 A8 EE AE 92 88 8A 62 40 62 AE 92 88 8A 64 40 63 03 F0 60 70 4A 2D 6C 2A 67 4B 5C 3E 3D 0D C0
End Function