用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

解决方案 »

  1.   

    楼主为此问题已经发过1贴,看来在那贴的回复你不太满意。
    不知道你试过那些建议没有,如有问题你可将你的实际情况贴出。
    或将详细的通信协议贴出。才能依此提供帮助。    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
      

  2.   

    记得你开过帖子的啊,我建议过你用延时再读串口的方法,不然数据很长肯定是要进好几次oncomm事件的,我也遇到过这种问题,用oncomm事件是可以的,但是要做好多判断,时序看起来有点乱,后来改成发送指令,再sleep(300),比如300ms,然后再读取数据,那样数据就是一个整体,再处理就方便多了.
    在oncomm里确实可行,那样就会有点乱.
      

  3.   

    我前面开贴前没注意看我前面的帖子,不好意思~~谢谢2楼的建议,但是现在还有个问题,我现在用timer控制时间,但是,每次收到的N段报文有可能中断在报文的中间,就是不知道怎么拼接成完整的D2打头,EA+校验位结束,然后EA后面的一部分还要拼接到下一段~~
      

  4.   

    2楼的意思是等我把所有的报文全部截下来了在一起处理,这样的话也可以,但是我就想有没有办法就每次的一小段(DA..EA)我截下来处理~~
      

  5.   

    那样你就试试在oncomm里吧,每次接收到数据就要判断一次Static intSegIndex As Integer
    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
      

  6.   

     刚发的代码少了个地方加一了,改正下,顺便加上校验码在下一段的问题Static intSegIndex As Integer
    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
      

  7.   

    用了楼上的代码,运行之后 run-time error '9':Sunscript out of range..啥意思。。
      

  8.   

    有个地方ElseIf打错了,打成了EndIf.你的错误是哪一行啊?那个我没怎么调试,只是思路大致是这样的!
      

  9.   

    我的意见接收按ONCOMM事件进行,具体代码已经在那个贴子提供。
      

  10.   

    运行程序上的机器没有装vb~~我自己在看看吧,我现在做到的就是用timer把一整大断截下来处理,十分感谢sulipeng007 的支持~~
    楼上的~~通信协议不是自己做的啊~~
      

  11.   

    我换在自己机器上了,出错的在
     For i = 0 To UBound(abytSegment)这行上~~
      

  12.   

    哦!可能是第一次进去abytSegment还没被赋值,数组是空的.你改成Static intSegIndex As Integer
    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
      

  13.   

    按楼主叙述每段数据倒数第2字节为&HEA,最后1字节为校验位,接收判断代码似乎应如下:Private 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 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