笔记本电脑没有串口,我用蓝牙管理软件来虚拟了一个串口,与另外一个蓝牙温度传感器设备连接,用于获取温度。蓝牙管理软件安装完毕后,可以与传感器顺利进行连接,并获取数据,我用串口调试助手打开相应的串口,可以顺利看到温度数据。之后准备用vb自己编写程序,来获取温度。mscomm参数设置与串口助手中设置的一致。可以打开相应的串口,但是一旦接收到数据,就会报错,8020号错误,读comm设备错误。text2中可以看到结果为1 ,说明通讯已经建立,端口打开都没有问题的。
Private Sub Comm1_OnComm()
Dim buffer As String
Text2.Text = Comm1.InBufferCount
buffer = Comm1.Input  出错语句。
 Text1.Text = buffer
End Sub
Private Sub Form_Load()
Comm1.CommPort = 2
Comm1.Settings = "9600,N,8,1"
Comm1.RThreshold = 1  '设置接收一个字节就产生OnComm事件    Comm1.PortOpen = True  '打开端口
    If PortOpen = False Then   '判断通信口是否打开
        If Err Then    '错误处理
            MsgBox "串口通信无效"
            Exit Sub
        End If
    End If
End Sub

解决方案 »

  1.   

    Dim buffer As String这句有问题,改为:
    Dim buffer As Variant
      

  2.   

    据说 MSComm 不支持某些 USB 转串芯片的驱动程序……
      

  3.   

    不行的话,用API写一点串口通信测测,论坛有很多现成代码
      

  4.   

    依照这个代码改写你自己的程序
    http://download.csdn.net/source/1262066
      

  5.   

    不能写在OnComm()事件里,因为每当有一个字节数据来就执行一次是不行的
    Private Sub MSComm1_OnComm()
        If MSComm1.CommEvent = comEvReceive Then Timer2.Enabled = True '如果事件类型不是错误信息,那么延时100毫秒以便于接收完全
    End Sub
      

  6.   

    改为
    Dim buffer As Byte
    而且摆在最上面的通用区,不能摆在 Comm1_OnComm这个事件里面这个你得设定啊
    Comm1.InputMode = comInputModeBinary '二进制模式接收
      

  7.   

    【CBM666 的二进制串口收送(1)】
    http://cbm666.net/forum.php?mod=viewthread&tid=1011&fromuid=2
      

  8.   

    查了很多文献,国外碰到这个问题的较多,很多人用来与手机的红外或者蓝牙通讯。google了一下,8020 read comm device。貌似有人说这是驱动程序的问题?
      

  9.   

    MS 给出的解释是这样的。
    SYMPTOMS
    When you use the Input method of the MSComm control with some serial ports, the MSComm.Input method may fail, and you may receive the comReadError error (error 8020).
    CAUSE
    This problem occurs because MSComm.Input reads only the bytes that are immediately available in the serial driver. By design, MSComm.Input does not return any bytes if the serial driver has not received any bytes. Because the MSComm control does not expect the ReadFile method to return ERROR_IO_PENDING, MSComm returns the comReadError error if this occurs.
    RESOLUTION
    A serial driver should return SUCCESS synchronously for all read requests that can be immediately fulfilled with data that is currently present.This problem is only known to occur with some third-party serial drivers. To correct the problem, contact the manufacturer of the serial driver to obtain an updated driver. 
    -----
      

  10.   

    没见到LZ的全部代码,无法判别.不过LZMsComm控件的OnComm事件代码过于简单,可能产生错误.
    Private Sub MSComm_OnComm()
        Select Case MSComm1.CommEvent
            Case comEvReceive   ' 收到 RThreshold # of chars.
                '在此处写接收和数据处理代码
                
                
                
       End Select
    End Sub
      

  11.   


    全部代码就在上面了。串口我也不是第一次弄,以前都能成功,当然,是用的台式机直接带串口的。这次的则不同,用的蓝牙的虚拟串口。程序是很简单,因为我现在只要求取得数据,然后再完善。 如图,是串口调试助手取得数据的界面。COM2口,9600,无校验,1位停止位。
      

  12.   

    图片怎么不行?IVT管理软件,中间的连线表示pc与远端蓝牙发送数据模块连接成功。