现在的位置: 首页 > 综合 > 正文

vb6串口接收数据事件的数据读出示例

2013年05月20日 ⁄ 综合 ⁄ 共 825字 ⁄ 字号 评论关闭

Private Sub MSComm1_OnComm()

Dim BytReceived() As Byte
    Dim strBuff As String
    Dim i As Integer
    Select Case MSComm1.CommEvent    '事件发生
        Case 2
            Cls
            
            MSComm1.InputLen = 0     '读入缓冲区全部内容
            strBuff = MSComm1.Input '读入到缓冲区

         If MSComm1.InputMode = comInputModeBinary Then
            BytReceived() = strBuff '如果是二进制接收模式则进行数据处理,否则直接显示字符串
            For i = 0 To UBound(BytReceived)
                If Len(Hex(BytReceived(i))) = 1 Then
                    strData = strData & "0" & Hex(BytReceived(i)) & " " '如果只有一个字符,则前补0,如F显示0F,最后补空格
                Else                                                    '方便显示观察如: 00 0F FE
                    strData = strData & Hex(BytReceived(i)) & " "
                End If
            Next
            Text2 = Text2 & strData
            strData = ""
        Else
            Text2 = Text2 & strBuff
        End If
    End Select
End Sub

抱歉!评论已关闭.