计算机和设备进行通信 
rthreshold设为23,传送二进制数据 
但触发oncomm,传进来的数据大多数是30个(0--29),偶尔是8、6、18个 
时不时的还Lbound()=0,Ubound()=-1 
问题出在哪?

解决方案 »

  1.   

    Option Explicit
        Dim BytReceived() As Byte
        Dim strData As StringPrivate Sub Form_Load()
        MSComm1.Settings = "9600,N,8,1"
        MSComm1.InputMode = comInputModeBinary
        MSComm1.RThreshold = 23
        MSComm1.PortOpen = True
    End Sub
    Private Sub MSComm1_OnComm() '接收数据
        Dim strBuff As String
        Text1 = ""
        Select Case MSComm1.CommEvent
            Case 2
                MSComm1.InputLen = 0
                strBuff = MSComm1.Input
                BytReceived() = strBuff
                jieshou
                '此处写接收后的数据处理代码
                If Len(strData) = 46 Then
                    strData = ""
                End If
        End Select
    End Sub
    Public Function jieshou() '接收数据处理为16进制
        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
        Text1 = strData
        Print Len(strData) / 2
    End Function
      

  2.   

    如果你的接收长度是固定 23则rthreshold=23, 如不固定你就不能设rthreshold此时你应该用 STX(启始符)与ETX(终止符)来抓取中间的字串
      

  3.   

    3楼看贴仔细.
    rthreshold设为1,在接收代码中根据通信协议来判断.