使用定长接收10个字节内容,将RThreshold = 10,接收别人上位机程序串口发送过来的10个数据始终不能实现定长接收,每次只接收一个或两个字节内容,使用串口调试工具发送同样内容可以实现定每次只长接收10数据.求教它出现这个问题的可能原因.
接收别人上位机发送的内容:串口调试接收内容:串口设置:    With MSComm1
        .Settings = "19200,N,8,1"        '基本参数
        .InputMode = comInputModeBinary '按2进制接收方式
        .RThreshold = 10                 '接收字节处理数
        .SThreshold = 1                 '以字节发送
        .InputLen = 0
        .InBufferCount = 0
        .CommPort = 4                   '打开4口
        .PortOpen = True
    End With串口接收:
Private Sub MSComm1_OnComm()
Dim i      As Integer
Dim strinput() As Byte
   strinput = MSComm1.Input   '得到当前数据
   For i = 0 To UBound(strinput)
      If strinput(i) < 10 Then
         temp = temp & " " & "0" & Hex(strinput(i)) '每个字节两位数格式
      Else
         temp = temp & " " & Hex(strinput(i))
      End If
   Next
   Label2.Caption = CStr(temp)    '显示接受内容
   Print temp                     '打印内容
End Sub

解决方案 »

  1.   

    试试
    Private Sub MSComm1_OnComm()
    Dim i As Integer, temp As String
    Dim strinput() As Variant
    If MSComm1.CommEvent = comEvReceive Then   strinput = MSComm1.Input   '得到当前数据
       For i = 0 To UBound(strinput)
          If strinput(i) < 16 Then
             temp = temp & " " & "0" & Hex(strinput(i)) '每个字节两位数格式
          Else
             temp = temp & " " & Hex(strinput(i))
          End If
       Next
       Label2.Caption = temp    '显示接受内容End If
    End Sub