Dim bytInput() As Byte
Dim i As Integer
Dim buffer As String'这个语句是定义在通用里面的
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputMode = 1 '0:文本方式,1:二进制方式
bytInput = MSComm1.Input
For i = LBound(bytInput) To UBound(bytInput)
buffer = buffer + Hex(bytInput(i)) + Chr(32)
Text1.Text = buffer
Next i
End Select
我现在接收到的数据是68 2F 2F 68 0 2 8 32 3 0 0 0 0 0 2 0 1E 0 0 4 4 FF 4 0 8 1 0 FF 4 0 8 2 0 FF 4 0 8 7D 0 FF 4 0 40 64 0 0 0 0 0 0 0 AF 16
我现在的问题是:一、如何把这个字符串转化成字节数组
二、如何让 MSComm1收完这个长度的字符串后就清空buffer变量的值,以免数据积累下去。
多谢大家的指教
8 个解决方案
#1
strconv函数处理
#2
然后,用mscomm2.input读出之后缓冲区会自动清除的
#3
dim s as string
s = "68 2F 2F 68 0 2 8 32 3 0 0 0 0 0 2 0 1E 0 0 4 4 FF 4 0 8 1 0 FF 4 0 8 2 0 FF 4 0 8 7D 0 FF 4 0 40 64 0 0 0 0 0 0 0 AF 16"
dim datastr() as string
datastr = Split(s, " ")
dim result() as byte
redim result(lbound(datastr) to ubound(datastr))
dim i as integer
for i = lbound(datastr) to ubound(datastr)
result(i) = cbyte(val("&H" & datastr))
next
s = "68 2F 2F 68 0 2 8 32 3 0 0 0 0 0 2 0 1E 0 0 4 4 FF 4 0 8 1 0 FF 4 0 8 2 0 FF 4 0 8 7D 0 FF 4 0 40 64 0 0 0 0 0 0 0 AF 16"
dim datastr() as string
datastr = Split(s, " ")
dim result() as byte
redim result(lbound(datastr) to ubound(datastr))
dim i as integer
for i = lbound(datastr) to ubound(datastr)
result(i) = cbyte(val("&H" & datastr))
next
#4
用Split()函数。
#5
Dim str As String = "68 2F 2F 68 0 2 8 32 3 0 0 0 0 0 2 0 1E 0 0 4 4 FF 4 0 8 1 0 FF 4 0 8 2 0 FF 4 0 8 7D 0 FF 4 0 40 64 0 0 0 0 0 0 0 AF 16 "
Dim ary = str.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries).[Select](Function(t) CByte(Convert.ToInt32(t, 16))).ToList()
Dim ary = str.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries).[Select](Function(t) CByte(Convert.ToInt32(t, 16))).ToList()
#6
一、如何把这个字符串转化成字节数组
Dim bytInput() As Byte
bytInput = MSComm1.Input
这里bytInput不就是字节数组嘛?
Dim bytInput() As Byte
bytInput = MSComm1.Input
这里bytInput不就是字节数组嘛?
#7
你收到的 bytInput 就是字节数组。是后续代码又转成字符串的。
#8
向高手学习了!
#1
strconv函数处理
#2
然后,用mscomm2.input读出之后缓冲区会自动清除的
#3
dim s as string
s = "68 2F 2F 68 0 2 8 32 3 0 0 0 0 0 2 0 1E 0 0 4 4 FF 4 0 8 1 0 FF 4 0 8 2 0 FF 4 0 8 7D 0 FF 4 0 40 64 0 0 0 0 0 0 0 AF 16"
dim datastr() as string
datastr = Split(s, " ")
dim result() as byte
redim result(lbound(datastr) to ubound(datastr))
dim i as integer
for i = lbound(datastr) to ubound(datastr)
result(i) = cbyte(val("&H" & datastr))
next
s = "68 2F 2F 68 0 2 8 32 3 0 0 0 0 0 2 0 1E 0 0 4 4 FF 4 0 8 1 0 FF 4 0 8 2 0 FF 4 0 8 7D 0 FF 4 0 40 64 0 0 0 0 0 0 0 AF 16"
dim datastr() as string
datastr = Split(s, " ")
dim result() as byte
redim result(lbound(datastr) to ubound(datastr))
dim i as integer
for i = lbound(datastr) to ubound(datastr)
result(i) = cbyte(val("&H" & datastr))
next
#4
用Split()函数。
#5
Dim str As String = "68 2F 2F 68 0 2 8 32 3 0 0 0 0 0 2 0 1E 0 0 4 4 FF 4 0 8 1 0 FF 4 0 8 2 0 FF 4 0 8 7D 0 FF 4 0 40 64 0 0 0 0 0 0 0 AF 16 "
Dim ary = str.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries).[Select](Function(t) CByte(Convert.ToInt32(t, 16))).ToList()
Dim ary = str.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries).[Select](Function(t) CByte(Convert.ToInt32(t, 16))).ToList()
#6
一、如何把这个字符串转化成字节数组
Dim bytInput() As Byte
bytInput = MSComm1.Input
这里bytInput不就是字节数组嘛?
Dim bytInput() As Byte
bytInput = MSComm1.Input
这里bytInput不就是字节数组嘛?
#7
你收到的 bytInput 就是字节数组。是后续代码又转成字符串的。
#8
向高手学习了!