收到的字符格式为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 81 09...EA A9
其中每1小段前3位为D2 81 XX 结束为EA XX,中间的数据都是我要获取的数据,一直延续下去很多段,每段的总长度不一样,我怎么样把那么长一大段分成以D2开头的很多小段,把每个小段存下来~~高手求救啊~~
其中每1小段前3位为D2 81 XX 结束为EA XX,中间的数据都是我要获取的数据,一直延续下去很多段,每段的总长度不一样,我怎么样把那么长一大段分成以D2开头的很多小段,把每个小段存下来~~高手求救啊~~
Private Sub Form_Load()
s = "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 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 b7"
s = Replace(s, "D2", "|D2")
s = Split(s, "|")
For i = 1 To UBound(s)
List1.AddItem s(1)
Next
End Sub
Option Explicit
Dim strData As StringPrivate Sub Form_Load()
MSComm1.Settings = "9600,N,8,1"
MSComm1.RThreshold = 1
MSComm1.InputMode = comInputModeBinary
MSComm1.PortOpen = True
End SubPrivate Sub MSComm1_OnComm()
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 Right(strData, 2) = "EA" Then
Text1 = strData
strData = ""
End If
End Select
End Sub
Dim astrFinalData() As StringPrivate Sub Command_Click()
MSComm1.output=...'//发送指令
Sleep(n)'//n为延时的毫秒数,自己根据接收数据的时间来确定
Dim abytReceived() As Byte
abytReceived= MSComm1.Input'//接收数据
'//数据接收完开始整合数据
Dim i As Integer
Dim strResult As String
For i =0 to Ubound(bytReceived)
If BytReceived(i)<10 Then
strResult = strResult & "0" & Hex(BytReceived(i)) & " "
Else
strResult =strResult & Hex(BytReceived(i)) & " "
End If
Next
'//开始处理数据,把数据分段,采用二楼的方法就行
Replace(strResult, "D2", "|D2")
astrFinalData= Split(strResult, "|")
For i = 1 To UBound(astrFinalData)
debug.print astrFinalData(i)
Next
把接收到的数据写入文本Option Explicit
Dim strData As StringPrivate Sub Form_Load()
MSComm1.Settings = "9600,N,8,1"
MSComm1.RThreshold = 1
MSComm1.InputMode = comInputModeBinary
MSComm1.PortOpen = True
End SubPrivate Sub MSComm1_OnComm()
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 Right(strData, 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