VB中使用MSCOMM,出现实时错误8002,无效的端口号,以及实时错误8012,设备未打开

来源:百度知道 编辑:UC知道 时间:2024/06/30 16:14:02
我的程序代码如下:
Option Explicit
Dim r_buff() As Byte '4字节数据接收缓冲区
Dim t_buff() As Byte '4字节数据发送缓冲区
Dim start_word As Integer
Dim i As Integer

Private Sub Command1_Click() '写“S”至单片机请求连接
start_word = 83

Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
MSComm1.PortOpen = True
t_buff(1) = CByte(start_word)
For i = 1 To i = 3
MSComm1.Output = t_buff(i)
Next i
End Sub

Private Sub MScomm1_OnComm()
Dim strBuff As String
Dim init_word0 As Integer: Dim init_word1 As Integer: Dim init_word2 As Integer

Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
MSComm1.InBufferCount = 0 ' 清空接收缓冲区
Select Case MSComm1.CommEvent '接收单片机发过来的4字节数据
Case comEvReceive
Text1.Text = ""
Text2.Text = &qu

程序中你设置的 是 COM1,你确信 你的 电脑 有 这个 端口嘛?

你可以去 设备管理器 里 看看!

如果没有,建议你使用 USB转串口!如下:

810 USB与1路RS-232接口转换器
820B USB与1路RS-232/485/422隔离转换器

有光盘驱动。
用 820B 的朋友居多!

http://www.homewaygd.com/cn/list.php/9-1.html

祝你顺利

MSComm1.RThreshold = 4改为MSComm1.RThreshold = 1
加MSComm1.OutBufferSize = 4
或是硬件设置问题,确定计算机是否选中com1,可能是笔记本电脑,一般无串口的。

sadf