用mscomm收的数据,每次都是长短不一的小段,我怎么把他分成我要的一小段~~
完整的格式应该如下:
D2 81 01 07 01 04 07 02 03 07 03 03 07 05 03 07 06 04 07 07 04 07 08 03 07 09 04 07 0A 03 07 0B 03 07 0C 04 07 0E 04 07 0F 03 07 10 04 07 11 03 07 12 03 07 13 04 07 14 03 07 15 03 07 16 03 EA A6
但是收到的是比如先收到 D2,断一下在收到8101070104,然后再收0702如何把他规范成上面的格式(D2开头EA+校验位结束),求高手啊~~收数据的代码如下:
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
BytReceived() = MSComm1.Input
Dim i As Integer
For i = LBound(BytReceived) To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next i
End Select
完整的格式应该如下:
D2 81 01 07 01 04 07 02 03 07 03 03 07 05 03 07 06 04 07 07 04 07 08 03 07 09 04 07 0A 03 07 0B 03 07 0C 04 07 0E 04 07 0F 03 07 10 04 07 11 03 07 12 03 07 13 04 07 14 03 07 15 03 07 16 03 EA A6
但是收到的是比如先收到 D2,断一下在收到8101070104,然后再收0702如何把他规范成上面的格式(D2开头EA+校验位结束),求高手啊~~收数据的代码如下:
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
BytReceived() = MSComm1.Input
Dim i As Integer
For i = LBound(BytReceived) To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next i
End Select
不知道你试过那些建议没有,如有问题你可将你的实际情况贴出。
或将详细的通信协议贴出。才能依此提供帮助。 Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
BytReceived() = MSComm1.Input
Dim i As Integer
For i = LBound(BytReceived) To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i)) & " "
Else
strData = strData & Hex(BytReceived(i)) & " "
End If
Next i
End Select
在oncomm里确实可行,那样就会有点乱.
Static blnCountStart As Boolean
Dim abytSegment() As ByteSelect Case MSComm1.CommEvent
Case 2:
MSComm1.InputLen = 0
BytReceived() = MSComm1.Input
'//接下来判断是不是有D2打头的或EA+校验位结尾的
Dim intIndex As Integer
For intIndex = 0 To UBound(bytReceive)
If bytReceive(intIndex) = &HD2 Then '//遇到文件头了
blnCountStart = True
intSegIndex = 0
End If
If bytReceive(intIndex) = &HEA And intIndex <> UBound(bytReceive) Then '//遇到文件尾了,后面跟着校验位
blnCountStart = False '//停止截取
ReDim Preserve abytSegment(intSegIndex)
abytSegment(intSegIndex) = bytReceive(intIndex)
ReDim Preserve abytSegment(intSegIndex + 1)
abytSegment(intSegIndex + 1) = bytReceive(intIndex + 1)
Else '//校验位在下一个段中,不知道会不会碰到,等碰到再加,呵呵!
End If
If blnCountStart Then '//可以截取了
ReDim Preserve abytSegment(intSegIndex)
abytSegment(intSegIndex) = bytReceive(intIndex)
intSegIndex = intSegIndex + 1
Else '//截取结束,整合数据
Dim i As Integer
For i = 0 To UBound(abytSegment)
If abytSegment(i) < 10 Then
strData = strData & "0" & Hex(abytSegment(i)) & " "
Else
strData = strData & Hex(abytSegment(i)) & " "
End If
Next
'//以上strData就是截取的数据了,看你怎么处理了
End If
Next
End Select
Static blnCountStart As Boolean
Static intCheck As Integer
Static blnCheck As Boolean
Dim abytSegment() As ByteSelect Case MSComm1.CommEvent
Case 2:
MSComm1.InputLen = 0
BytReceived() = MSComm1.Input
'//接下来判断是不是有D2打头的或EA+校验位结尾的
Dim intIndex As Integer
For intIndex = 0 To UBound(bytReceive)
If bytReceive(intIndex) = &HD2 Then '//遇到文件头了
blnCountStart = True
intSegIndex = 0
End If
If bytReceive(intIndex) = &HEA And intIndex <> UBound(bytReceive) Then '//遇到文件尾了,后面跟着校验位
blnCountStart = False '//停止截取
ReDim Preserve abytSegment(intSegIndex)
abytSegment(intSegIndex) = bytReceive(intIndex)'//取尾
ReDim Preserve abytSegment(intSegIndex + 1)
intIndex=intIndex+1'//取校验码
abytSegment(intSegIndex + 1) = bytReceive(intIndex)
ElseIf bytReceive(intIndex) = &HEA And intIndex = UBound(bytReceive) Then'//遇到文件尾了,校验码不在本段中
intCheck=0
blnCheck=True
End If intCheck=intCheck+1
If intCheck=2 And blnCheck Then
blnCountStart=Flase'//得到校验位了
blnCheck=False
EndIf intCheck>3 Then
intCheck=3
End If If blnCountStart Then '//可以截取了
ReDim Preserve abytSegment(intSegIndex)
abytSegment(intSegIndex) = bytReceive(intIndex)
intSegIndex = intSegIndex + 1
Else '//截取结束,整合数据
Dim i As Integer
For i = 0 To UBound(abytSegment)
If abytSegment(i) < 10 Then
strData = strData & "0" & Hex(abytSegment(i)) & " "
Else
strData = strData & Hex(abytSegment(i)) & " "
End If
Next
'//以上strData就是截取的数据了,看你怎么处理了
End If
Next
End Select
楼上的~~通信协议不是自己做的啊~~
For i = 0 To UBound(abytSegment)这行上~~
Static blnCountStart As Boolean
Static intCheck As Integer
Static blnCheck As Boolean
Dim abytSegment() As ByteSelect Case MSComm1.CommEvent
Case 2:
MSComm1.InputLen = 0
BytReceived() = MSComm1.Input
'//接下来判断是不是有D2打头的或EA+校验位结尾的
Dim intIndex As Integer
For intIndex = 0 To UBound(bytReceive)
If bytReceive(intIndex) = &HD2 Then '//遇到文件头了
blnCountStart = True
intSegIndex = 0
End If
If bytReceive(intIndex) = &HEA And intIndex <> UBound(bytReceive) Then '//遇到文件尾了,后面跟着校验位
blnCountStart = False '//停止截取
ReDim Preserve abytSegment(intSegIndex)
abytSegment(intSegIndex) = bytReceive(intIndex)'//取尾
ReDim Preserve abytSegment(intSegIndex + 1)
intIndex=intIndex+1'//取校验码
abytSegment(intSegIndex + 1) = bytReceive(intIndex)
ElseIf bytReceive(intIndex) = &HEA And intIndex = UBound(bytReceive) Then'//遇到文件尾了,校验码不在本段中
intCheck=0
blnCheck=True
End If intCheck=intCheck+1
If intCheck=2 And blnCheck Then
blnCountStart=Flase'//得到校验位了
blnCheck=False
EndIf intCheck>3 Then
intCheck=3
End If If blnCountStart Then '//可以截取了
ReDim Preserve abytSegment(intSegIndex)
abytSegment(intSegIndex) = bytReceive(intIndex)
intSegIndex = intSegIndex + 1
ElseIf intSegIndex>0 '//截取结束,整合数据
Dim i As Integer
For i = 0 To UBound(abytSegment)
If abytSegment(i) < 10 Then
strData = strData & "0" & Hex(abytSegment(i)) & " "
Else
strData = strData & Hex(abytSegment(i)) & " "
End If
Next
'//以上strData就是截取的数据了,看你怎么处理了
End If
Next
End Select
Dim BytReceived() As Byte
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
BytReceived() = MSComm1.Input
Dim i As Integer
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i))
Else
strData = strData & Hex(BytReceived(i))
End If
Next
'按通信协议写接收数据处理代码
If Mid(strData, 1, 2) = "D2" And left(Right(strData, 4),2) = "EA" Then
Text1 = strData
Open "c:\data.txt" For Append As #1
Print #1, strData
Close #1
strData = ""
End If
End Select
End Sub