当前位置:编程学习 > VB >>

向达人请教 串口通讯问题 数据已经发送出去了,但是接收的时候总是说类型不匹配

Private Sub Cmdreceived_Click() 
Dim Inbyte() As Byte 
Dim i% 
Dim buf 
Inbyte = MSComm1.Input 
For i = LBound(Inbyte) To UBound(Inbyte) 
  buf = buf + Inbyte(i) + Chr(32) 
Next i 
Text2.Text = buf & vbCrLf 
End Sub 

Private Sub Cmdsend_Click() 
Dim OutByte(1 To 1) As Byte 
OutByte(1) = &H99 
MSComm1.Output = OutByte         '向串口发送‘&H99’.单片机一旦接受到一个 
                                  '‘0x99’的字符则,启动传感器 
End Sub 


向单片机发送一个字节的"&H99',然后单片机返回一个 三字节的数据 , 
这是程序的主要部分 
运行的时候,单片机接受到了'&H99',但是,当我的程序点接收健的时候,提示说,数据类型不 
匹配,请达人给意见 

谢谢 

还有我的硬件是好的,通过串口调试帮手 调过了,谢谢 --------------------编程问答-------------------- Private Sub MSComm1_OnComm()
On Error Resume Next
    Dim BytReceived() As Byte
    Dim strBuff As String
    Dim strData As String
    Dim i As Integer
    Dim x As Integer
    Select Case MSComm1.CommEvent
        Case 2
            MSComm1.InputLen = 0
            strBuff = MSComm1.Input
            BytReceived() = strBuff
            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
            Text3 = Text3 + strData
            If Left(strData, 2) = "99" And Len(strData) = 8 Then '接收4字节
            Text1.Text = Left(strData, 6)
            Call DataClear
            End If
End Select
End Sub
Private Sub Form_Load()
    MSComm1.CommPort = 1                   'COM端口
    MSComm1.Settings = "9600,n,8,1"
    MSComm1.InputMode = comInputModeBinary      '采用二进制传输
    MSComm1.InBufferCount = 0   '清空接受缓冲区
    MSComm1.OutBufferCount = 0  '清空传输缓冲区
    MSComm1.RThreshold = 3      '产生MSComm事件
    MSComm1.PortOpen = True
    Text3 = "" '打开端口
End Sub
Public Sub DataClear()
    MSComm1.OutBufferCount = 0   '清空发送缓冲区
    MSComm1.InBufferCount = 0
    Text3 = ""
End Sub --------------------编程问答-------------------- buf = buf + Inbyte(i) + Chr(32) 

+ 改成 &
补充:VB ,  控件
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,