我需要接收下位机的一个34字节的数据,协议就是mscomm1.Settings = "9600,n,8,1"我用下边的代码接收,发现有的时候会有一次数据接收到没有解析出来,可能是接收的时候有问题。所以想在群里请教高手给指点下,要是这样接收34字节的数据,什么样的接收方式比较好,不会出现接收错误。谢谢(对方一共发3遍,每遍的间隔为20ms。)
Select Case MSComm1.CommEvent
        Case comEvReceive
        MSComm1.RThreshold = 0
        m = Timer
        While Timer - m < 0.038         '从0.01到0.02(20sm)做延时
        Wend
        inByte = MSComm1.Input
        
        For i = 0 To UBound(inByte)
        If Len(Hex(inByte(i))) = 1 Then
            str1 = 0 & Hex(inByte(i))
            Text14 = str1
        Else
            str1 = Hex(inByte(i))
            Text15 = str1
        End If
            strData = strData & str1
            Text16 = strData
        Next
    End Select
        MSComm1.RThreshold = 1我目前Mscomm1的设置如下
    With MSComm1
        .CommPort = 1
        .Settings = "9600,n,8,1"
        .InBufferCount = 0
        .InputLen = 0
        .RThreshold = 0   
        .InputMode = comInputModeBinary '以2进制接收
        .PortOpen = True
    End With

解决方案 »

  1.   

    Private Declare Function GetTickCount Lib "kernel32" () As Long
    Private Sub MSComm1_OnComm()
    Select Case MSComm1.CommEvent
    Case comEvReceive
    t = GetTickCount()
    Do
    DoEvents
    Loop Until GetTickCount - t > 20
    inByte = MSComm1.Input 
    For i = lBound(inByte)  To UBound(inByte) 
            If Len(Hex(inByte(i))) = 1 Then 
                str1 = 0 & Hex(inByte(i)) 
                Text14 = str1 
            Else 
                str1 = Hex(inByte(i)) 
                Text15 = str1 
            End If 
                strData = strData & str1 
                Text16 = strData 
            Next i
        End Select 
    Private Sub Form_Load()
    MSComm1.RThreshold = 1
    MSComm1.PortOpen = True
    End Sub
      

  2.   

    你试验一下下面的代码,是否可行Dim recive
    Dim j
     With MSComm3
       Select Case .CommEvent
          Case comEvReceive
            j = .InBufferCount '取数据的字节数
            recive = .Input     '读取全部数据到recive        If j > 34 Then j = 34 '对于超出34字节的数据截取数据为前34字节        Select Case j:
                Case 34:
                    If ((recive(xx))) = &Hxx  Then '条件是依据协议对取到的数据进行分析,数据是否异常
                         xxxxx '这里的代码对取到的正确数据,进行加工处理
                    Else
                         MsgBox "取到的数据异常"
                    End If
            End Select
            .InBufferCount = 0
          Case Else
        End Select
      End With如果有什么问题,请再回复!
      

  3.   

    我现在这么弄了一下,达到我的目的了,就是在设置里边把
     Mscomm1.RThreshold = 34  
    Mscomm1.InBufferSize= 34
    然后我在我接收的代码里边把延时去了
    Select Case MSComm1.CommEvent 
            Case comEvReceive 
            inByte = MSComm1.Input 
            For i = 0 To UBound(inByte) 
            If Len(Hex(inByte(i))) = 1 Then 
                str1 = 0 & Hex(inByte(i)) 
                Text14 = str1 
            Else 
                str1 = Hex(inByte(i)) 
                Text15 = str1 
            End If 
                strData = strData & str1 
                Text16 = strData 
            Next 
        End Select 
    这样就可以了。
      

  4.   

    楼主,32736个字节是不能实现的。。溢出了,RThreshold属性是整形的
      

  5.   

    Integer 数据类型
    Integer 变量存储为 16位(2 个字节)的数值形式,其范围为 -32,768 到 32,767 之间。
    32736 < 32767
      

  6.   

    把缓冲区开大点就是,默认的太小。
    如果字节数定长:
    Mscomm1.RThreshold = 32736
    Mscomm1.InBufferSize= 4096 
      

  7.   

    我准备定义个数组,然后保存在里边。目前准备这样操作
        With MSComm1
            .CommPort = 1
            .Settings = "9600,n,8,1"
            .InBufferCount = 0
            .InputLen = 0
            .RThreshold = 33   '32768
            .InBufferSize = 33
            .InputMode = comInputModeBinary '以2进制接收
            .PortOpen = True
        End WithPrivate Sub MSComm1_OnComm()
    Dim inByte() As Byte
    Dim i As Integer
    Dim Buf(0 To 1031) As String
    Select Case MSComm1.CommEvent
            Case comEvReceive
            inByte = MSComm1.Input
            
            For i = 0 To UBound(inByte)
            If Len(Hex(inByte(i))) = 1 Then
                str1 = 0 & Hex(inByte(i))
            Else
                str1 = Hex(inByte(i))
            End If
                strData = strData & str1
            Next
       
            Buf(i) = strData
            i = i + 1
        End Select
        If i > 1031 Then
            Open App.Path & "\Buf.txt" For Output As #1
            For i = 1 To 1031
            Print #1, Buf(i)
            Next i
            Close #1
        End If
    End Sub但是这样接收完我想判断如果下位机给我发送代表数据结束的代码“FF01”这个怎么在接收数据的时候判断啊?
      

  8.   

    还有我现在用上边的代码接收,发现没有形成buf.txt文本在我文件夹中,是哪出问题了吗?
      

  9.   

    因为.RThreshold = 33 ,inByte = MSComm1.Input,所以每次触发事件
    UBound(inByte)等于33,+1也就是34,永远也不会大于1031。