笔记本电脑没有串口,我用蓝牙管理软件来虚拟了一个串口,与另外一个蓝牙温度传感器设备连接,用于获取温度。蓝牙管理软件安装完毕后,可以与传感器顺利进行连接,并获取数据,我用串口调试助手打开相应的串口,可以顺利看到温度数据。之后准备用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
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
解决方案 »
- 如何让程序在运行时能锁定XP的桌面?退出程序才能显示XP桌面?
- 奇怪的SQL查询,烦请高人指点
- 如何列出工程下所有的窗体的名称?
- 关于picturebox 内部拖动以及自动用滚动条改变大小的问题//高分相赠
- 如何使用 Messenger API
- 举报!支持者都有分! 有人瞎送分!不公平!!!
- 网络问题,怎么编程向QQ好友发送消息
- 求助各位大侠:用VB中的VB应用程序向导生成的程序为何不能运行???
- 使用datareport制作报表,报表的最后一行总是没有表格,请问怎么解决?
- 希望大家能帮忙,谢谢
- 如何用VB程序引用项的Flash插件的版本号
- VB操作WORD,一共三个问题,积累了好久了,实在没法自己解决了。。。
Dim buffer As Variant
http://download.csdn.net/source/1262066
Private Sub MSComm1_OnComm()
If MSComm1.CommEvent = comEvReceive Then Timer2.Enabled = True '如果事件类型不是错误信息,那么延时100毫秒以便于接收完全
End Sub
Dim buffer As Byte
而且摆在最上面的通用区,不能摆在 Comm1_OnComm这个事件里面这个你得设定啊
Comm1.InputMode = comInputModeBinary '二进制模式接收
http://cbm666.net/forum.php?mod=viewthread&tid=1011&fromuid=2
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.
-----
Private Sub MSComm_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive ' 收到 RThreshold # of chars.
'在此处写接收和数据处理代码
End Select
End Sub
全部代码就在上面了。串口我也不是第一次弄,以前都能成功,当然,是用的台式机直接带串口的。这次的则不同,用的蓝牙的虚拟串口。程序是很简单,因为我现在只要求取得数据,然后再完善。 如图,是串口调试助手取得数据的界面。COM2口,9600,无校验,1位停止位。