VB关于串口com口可选问题
原本COM口设定是在代码写好可运行的,我自己添加了一段关于自动检测COM口的程序来取代固定com口的,但是一运行后就报错“实时错误8002无效的端口”这是为什么?麻烦知道...
原本COM口设定是在代码写好可运行的,我自己添加了一段关于自动检测COM口的程序来取代固定com口的,但是一运行后就报错“实时错误8002 无效的端口”这是为什么?麻烦知道的指导我一下,谢谢。
贴出部分程序:
'****窗口加载初始化代码****
Private Sub Form_Load()
'*************************自动检测可用COM口*****************************
' If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
' For i = 1 To 16
' On Error Resume Next
' MSComm1.CommPort = i
' MSComm1.PortOpen = True
' Select Case Err.Number
' Case 0
' cboPort.AddItem "Com" & Trim(i)
' MSComm1.PortOpen = False
' Case 8005
' cboPort.AddItem "Com" & Trim(i) & " 已经占用"
' MSComm1.PortOpen = False
' End Select
' Err = 0
' Next
'***********************************************************************
MSComm1.CommPort = 4
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 512
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.SThreshold = 0
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
counter = 0
End Sub
'****开始接收按钮单击事件****
Private Sub Command1_Click()
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
If Err Then
MsgBox "串口通信无效"
Exit Sub
End If
End Sub
'****退出按钮单击事件****
Private Sub Command2_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
Unload Me
End
End Sub 展开
贴出部分程序:
'****窗口加载初始化代码****
Private Sub Form_Load()
'*************************自动检测可用COM口*****************************
' If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
' For i = 1 To 16
' On Error Resume Next
' MSComm1.CommPort = i
' MSComm1.PortOpen = True
' Select Case Err.Number
' Case 0
' cboPort.AddItem "Com" & Trim(i)
' MSComm1.PortOpen = False
' Case 8005
' cboPort.AddItem "Com" & Trim(i) & " 已经占用"
' MSComm1.PortOpen = False
' End Select
' Err = 0
' Next
'***********************************************************************
MSComm1.CommPort = 4
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 512
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.SThreshold = 0
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
counter = 0
End Sub
'****开始接收按钮单击事件****
Private Sub Command1_Click()
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
If Err Then
MsgBox "串口通信无效"
Exit Sub
End If
End Sub
'****退出按钮单击事件****
Private Sub Command2_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
Unload Me
End
End Sub 展开
3个回答
展开全部
Private Sub Form_Load()
'*************************自动检测可用COM口*****************************
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
On Error Resume Next
For i = 1 To 16
MSComm1.CommPort = i
MSComm1.PortOpen = True
Select Case Err.Number
Case 0
Combo1.AddItem "Com" & i
Case 8005
Combo1.AddItem "Com" & i & " 已经占用"
End Select
MSComm1.PortOpen = False
Err.Clear
Next
'***********************************************************************
On Error GoTo 0
MSComm1.CommPort = 4
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 512
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.SThreshold = 0
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Counter = 0
End Sub
'****开始接收按钮单击事件****
Private Sub Command1_Click()
If MSComm1.PortOpen = False Then
On Error Resume Next
MSComm1.PortOpen = True
If Err.Number Then MsgBox "串口通信无效"
End If
End Sub
'****退出按钮单击事件****
Private Sub Command2_Click()
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
End
End Sub
'*************************自动检测可用COM口*****************************
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
On Error Resume Next
For i = 1 To 16
MSComm1.CommPort = i
MSComm1.PortOpen = True
Select Case Err.Number
Case 0
Combo1.AddItem "Com" & i
Case 8005
Combo1.AddItem "Com" & i & " 已经占用"
End Select
MSComm1.PortOpen = False
Err.Clear
Next
'***********************************************************************
On Error GoTo 0
MSComm1.CommPort = 4
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 512
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.SThreshold = 0
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
Counter = 0
End Sub
'****开始接收按钮单击事件****
Private Sub Command1_Click()
If MSComm1.PortOpen = False Then
On Error Resume Next
MSComm1.PortOpen = True
If Err.Number Then MsgBox "串口通信无效"
End If
End Sub
'****退出按钮单击事件****
Private Sub Command2_Click()
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
End
End Sub
更多追问追答
追问
你确定没错么?为什么我用了错误又多了=.=
追答
我测试无误。如果不行那就无法帮你了。
补充一下:
8002表示端口无效
8005表示端口已占用
再补充一下:
现在的主板大多数只支持一个串行口即Com1,除非安装扩展卡
展开全部
建议你在 End Select 之前加一个 Case else 把 Err.Number 输出一下看看。
出错可不止 8005 这一种错误。
出错可不止 8005 这一种错误。
追问
输出的err.number是8002不是8005,我运行报错也是8002
追答
改成这样试试:
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
For i = 1 To 16
On Error Resume Next
MSComm1.CommPort = i
MSComm1.PortOpen = True
Select Case Err.Number
Case 0
cboPort.AddItem "Com" & Trim(i)
Case Else
cboPort.AddItem "Com" & Trim(i) & " 不可用"
End Select
MSComm1.PortOpen = False
Err.Clear
Next
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
关注这个问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询