VB中使用MSCOMM,出现实时错误8002,无效的端口号,以及实时错误8012,设备未打开
我的程序代码如下:OptionExplicitDimr_buff()AsByte'4字节数据接收缓冲区Dimt_buff()AsByte'4字节数据发送缓冲区Dimsta...
我的程序代码如下:
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 = ""
Text3.Text = ""
MSComm1.InBufferCount = 0 ' 清空接收缓冲区
strBuff = MSComm1.Input
r_buff() = strBuff
End Select
init_word0 = r_buff(1)
init_word1 = r_buff(2)
init_word2 = r_buff(3)
Text1.Text = AscB(init_word0)
Text2.Text = AscB(init_word1)
Text3.Text = AscB(init_word2)
If init_word0 > init_word1 Or init_word0 < init_word2 Then
MsgBox "Alarming:Denger!", vbOKOnly, "Tips"
End If
End Sub
Private Sub Command2_Click()
MSComm1.PortOpen = False '关闭串口
Text1.Text = 0
Text2.Text = 0
Text3.Text = 0
End Sub
Private Sub Command3_Click()
Dim max As Integer: Dim min As Integer
start_word = 83
MSComm1.PortOpen = True
t_buff(1) = CByte(start_word)
max = Val(Text2.Text): min = Val(Text3.Text)
t_buff(2) = CByte(max): t_buff(3) = CByte(min)
MSComm1.OutBufferCount = 0 '清空发送缓冲区
For i = 1 To i = 3
MSComm1.Output = t_buff(i)
Next i
Select Case MSComm1.CommEvent
Case comEvSend
MsgBox "OK", vbOKOnly, "Tips"
End Select
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub Form1_Load()
MSComm1.Settings = "9600,N,8,1" '设置波特率和发送字符格式
MSComm1.CommPort = 1 '设置通讯串口
MSComm1.InputLen = 0 '设置或返回一次从接收缓冲区中读取字节数,0表示一次读取所有数据
MSComm1.InBufferSize = 4
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.InputMode = comInputModeBinary
MSComm1.RThreshold = 4
'MSComm1.PortOpen = True
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub Form1_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub 展开
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 = ""
Text3.Text = ""
MSComm1.InBufferCount = 0 ' 清空接收缓冲区
strBuff = MSComm1.Input
r_buff() = strBuff
End Select
init_word0 = r_buff(1)
init_word1 = r_buff(2)
init_word2 = r_buff(3)
Text1.Text = AscB(init_word0)
Text2.Text = AscB(init_word1)
Text3.Text = AscB(init_word2)
If init_word0 > init_word1 Or init_word0 < init_word2 Then
MsgBox "Alarming:Denger!", vbOKOnly, "Tips"
End If
End Sub
Private Sub Command2_Click()
MSComm1.PortOpen = False '关闭串口
Text1.Text = 0
Text2.Text = 0
Text3.Text = 0
End Sub
Private Sub Command3_Click()
Dim max As Integer: Dim min As Integer
start_word = 83
MSComm1.PortOpen = True
t_buff(1) = CByte(start_word)
max = Val(Text2.Text): min = Val(Text3.Text)
t_buff(2) = CByte(max): t_buff(3) = CByte(min)
MSComm1.OutBufferCount = 0 '清空发送缓冲区
For i = 1 To i = 3
MSComm1.Output = t_buff(i)
Next i
Select Case MSComm1.CommEvent
Case comEvSend
MsgBox "OK", vbOKOnly, "Tips"
End Select
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub Form1_Load()
MSComm1.Settings = "9600,N,8,1" '设置波特率和发送字符格式
MSComm1.CommPort = 1 '设置通讯串口
MSComm1.InputLen = 0 '设置或返回一次从接收缓冲区中读取字节数,0表示一次读取所有数据
MSComm1.InBufferSize = 4
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.InputMode = comInputModeBinary
MSComm1.RThreshold = 4
'MSComm1.PortOpen = True
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub Form1_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub 展开
3个回答
展开全部
程序中你设置的 是 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
祝你顺利
你可以去 设备管理器 里 看看!
如果没有,建议你使用 USB转串口!如下:
810 USB与1路RS-232接口转换器
820B USB与1路RS-232/485/422隔离转换器
有光盘驱动。
用 820B 的朋友居多!
http://www.homewaygd.com/cn/list.php/9-1.html
祝你顺利
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询