Private Sub MSComm1_OnComm()
    MSC
End Sub
Private Sub MSC()
    Dim intInputLen As Integer
    Dim i As Integer
    Dim bytInput() As Byte
    Dim hxstr As String
    Select Case Me.MSComm1.CommEvent
        Case comEvReceive
            '此处添加处理接收的代码
            Me.MSComm1.InputMode = comInputModeBinary '二进制接收
            intInputLen = Me.MSComm1.InBufferCount
            ReDim bytInput(intInputLen)
            bytInput = Me.MSComm1.Input
            'jieshou
            For i = 0 To UBound(bytInput)
               If Len(Hex(bytInput(i))) = 1 Then
                   strData = strData & "0" & Hex(bytInput(i))
               Else
                   strData = strData & Hex(bytInput(i))
               End If
            Next
        hxstr = Left$(strData, 4)
        hxstr = Right$(hxstr, 2)
        Text1.Text = CStr(CInt(&H13 * 256) + CInt("&H" & hxstr))
    End Select
End Sub

解决方案 »

  1.   

    Option Explicit
        Private strData As String
    Private Sub Form_Load()
        MSComm1.CommPort = 1
        MSComm1.Settings = "9600,N,8,1"
        MSComm1.RThreshold = 1
        MSComm1.PortOpen = True
    End SubPrivate Sub MSComm1_OnComm()
        Dim intInputLen As Integer
        Dim i As Integer
        Dim bytInput() As Byte
        Dim hxstr As String
        Select Case Me.MSComm1.CommEvent
            Case comEvReceive
                '此处添加处理接收的代码
                MSComm1.InputMode = comInputModeBinary       '二进制接收
                intInputLen = Me.MSComm1.InBufferCount
                ReDim bytInput(intInputLen)
                bytInput = Me.MSComm1.Input
                For i = 0 To UBound(bytInput)
                    If Len(Hex(bytInput(i))) = 1 Then
                        strData = strData & "0" & Hex(bytInput(i))
                    Else
                        strData = strData & Hex(bytInput(i))
                    End If
                Next
                hxstr = Left$(strData, 4)
                hxstr = Right$(hxstr, 2)
                Text1.Text = CStr(CInt(&H13 * 256) + CInt("&H" & hxstr))
        End Select
    End Sub
      

  2.   

    Option Explicit
        Private strData As String
    Private Sub Form_Load()
        MSComm1.CommPort = 1
        MSComm1.Settings = "9600,N,8,1"
        MSComm1.RThreshold = 1 '产生OnComm事件
        MSComm1.PortOpen = True
    End Sub
    Private Sub MSComm1_OnComm()
        Select Case Me.MSComm1.CommEvent
            Case comEvReceive
                MSC
        End Select
    End Sub
    Private Sub MSC()
        Dim intInputLen As Integer
        Dim i As Integer
        Dim bytInput() As Byte
        Dim hxstr As String
        MSComm1.InputMode = comInputModeBinary       '二进制接收
        intInputLen = Me.MSComm1.InBufferCount
        ReDim bytInput(intInputLen)
        bytInput = Me.MSComm1.Input
        For i = 0 To UBound(bytInput)
            If Len(Hex(bytInput(i))) = 1 Then
                strData = strData & "0" & Hex(bytInput(i))
            Else
                strData = strData & Hex(bytInput(i))
            End If
        Next
        hxstr = Left$(strData, 4)
        hxstr = Right$(hxstr, 2)
        Text1.Text = CStr(CInt(&H13 * 256) + CInt("&H" & hxstr))
    End Sub
      

  3.   

    使用这个“Me.MSComm1.CommEvent”只能以Byte格式返回数据!应该是处理方法不对。
      

  4.   

    照着zdingyun 的方法,还是不行啊
      

  5.   

    LZ 你是想接收文本数据呢?还是接收数字数据呢?
    我给出的代码(VB6调试通过),是基于二进制方式接收数据,然后转换为16进制字符串形式显示。然后需按上下位机的通信协议进行数据处理,以达到你所想要求的结果。
      

  6.   

    用轮询来作试试看呢,自己改改。Private Sub Com1_OnComm()
        Dim ccount As Integer
        Dim ReadStr As String  '存放读取读取数据的变量
        
        '直接轮循法
        With Com1
            Select Case .CommEvent
                Case comEvReceive
                .RThreshold = 0
                Do
                    ccount = .InBufferCount
                    Sleep (200)
                Loop Until .InBufferCount = ccount
                ReadStr = .Input            test1.text= ReadStr
                .RThreshold = 1
            End Select
        End With
    End Sub
      

  7.   

    VB的通信利用MSComm控件或利用WIN API函数完成通信。不知道你添加了MSComm控件在窗体吗?