我用vb编写了一段关于串口通信的程序,调试的时候发现只要一点打开串口,他就会跳出‘无效的端口号’信息
当然我用的是Mscomm控件,还有就是用的虚拟串口,难道不能用虚拟串口么,有哪位仁兄,知道原因的,帮忙指点一下,谢谢!PrivateSubCombol_Click()If...
当然我用的是Mscomm控件,还有就是用的虚拟串口,难道不能用虚拟串口么,有哪位仁兄,知道原因的,帮忙指点一下,谢谢!
Private Sub Combol_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False '关闭通信端口
End If
MSComm1.CommPort = Combol.ListIndex + 1
End Sub
Private Sub Command1_Click()
On Error GoTo uerror
If Command1.Caption = "关闭串口" Then
MSComm1.PortOpen = False
Command1.Caption = "打开串口"
Shape1.FillColor = &HFFFFC0
Else
MSComm1.PortOpen = True
Command1.Caption = "关闭窗口"
Shape1.FillColor = &HFF
End If
Exit Sub
uerror:
msg$ = "无效端口号"
Title$ = "串口调试助手"
X = MsgBox(msg$, 48, Title$)
End Sub
Private Sub form_load()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
Else
End If
Combo1.AddItem "COM1"
Combo1.AddItem "COM2"
Combo1.AddItem "COM3"
Combo1.AddItem "COM4"
Combo1.AddItem "COM5"
Combo1.AddItem "COM6"
Combo1.AddItem "COM7"
Combo1.AddItem "COM8"
Combo1.ListIndex = 0
MSComm1.CommPort = 3
MSComm1.Settings = "19200,n,8,1"
Command1.Caption = "打开串口"
Shape1.FillColor = &HFFFFC0
Option2.Value = True
Combo2.AddItem "256000"
Combo2.AddItem "128000"
Combo2.AddItem "115200"
Combo2.AddItem "57600"
Combo2.AddItem "38400"
Combo2.AddItem "28800"
Combo2.AddItem "19200"
Combo2.AddItem "9600"
Combo2.AddItem "4800"
Combo2.AddItem "2400"
Combo2.AddItem "1200"
Combo3.AddItem "无 None"
Combo3.AddItem "奇 Odd"
Combo3.AddItem "偶 Even"
Combo4.AddItem "4"
Combo4.AddItem "5"
Combo4.AddItem "5"
Combo4.AddItem "6"
Combo4.AddItem "7"
Combo4.AddItem "8"
Combo5.AddItem "1"
Combo5.AddItem "2"
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 512
End Sub
Private Sub MSComm1_OnComm()
Dim BytReceived() As Byte
Dim i As Integer
Select Case MSComm1.CommEvent
Case comEventRxOver
Text1.Text = ""
Exit Sub
Case comEvReceive
MSComm1.InputLen = 0
strBuff = MSComm1.Input
If MSComm1.InputMode = comInputModeBinary Then
BytReceived() = strBuff
For i = 0 To ubond(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i)) & ""
Else
strData = strData & Hex(BytReceived(i)) & ""
End If
Next
Text1 = Text1 & strData
strData = ""
Else
Text1 = Text1 & strBuff
End If
End Select
End Sub
Private Sub Option1_Click()
MSComm1.InputMode = comInputModeText
End Sub
Private Sub Option2_Click()
MSComm1.InputMode = comInputModeBinary
End Sub 展开
Private Sub Combol_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False '关闭通信端口
End If
MSComm1.CommPort = Combol.ListIndex + 1
End Sub
Private Sub Command1_Click()
On Error GoTo uerror
If Command1.Caption = "关闭串口" Then
MSComm1.PortOpen = False
Command1.Caption = "打开串口"
Shape1.FillColor = &HFFFFC0
Else
MSComm1.PortOpen = True
Command1.Caption = "关闭窗口"
Shape1.FillColor = &HFF
End If
Exit Sub
uerror:
msg$ = "无效端口号"
Title$ = "串口调试助手"
X = MsgBox(msg$, 48, Title$)
End Sub
Private Sub form_load()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
Else
End If
Combo1.AddItem "COM1"
Combo1.AddItem "COM2"
Combo1.AddItem "COM3"
Combo1.AddItem "COM4"
Combo1.AddItem "COM5"
Combo1.AddItem "COM6"
Combo1.AddItem "COM7"
Combo1.AddItem "COM8"
Combo1.ListIndex = 0
MSComm1.CommPort = 3
MSComm1.Settings = "19200,n,8,1"
Command1.Caption = "打开串口"
Shape1.FillColor = &HFFFFC0
Option2.Value = True
Combo2.AddItem "256000"
Combo2.AddItem "128000"
Combo2.AddItem "115200"
Combo2.AddItem "57600"
Combo2.AddItem "38400"
Combo2.AddItem "28800"
Combo2.AddItem "19200"
Combo2.AddItem "9600"
Combo2.AddItem "4800"
Combo2.AddItem "2400"
Combo2.AddItem "1200"
Combo3.AddItem "无 None"
Combo3.AddItem "奇 Odd"
Combo3.AddItem "偶 Even"
Combo4.AddItem "4"
Combo4.AddItem "5"
Combo4.AddItem "5"
Combo4.AddItem "6"
Combo4.AddItem "7"
Combo4.AddItem "8"
Combo5.AddItem "1"
Combo5.AddItem "2"
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 512
End Sub
Private Sub MSComm1_OnComm()
Dim BytReceived() As Byte
Dim i As Integer
Select Case MSComm1.CommEvent
Case comEventRxOver
Text1.Text = ""
Exit Sub
Case comEvReceive
MSComm1.InputLen = 0
strBuff = MSComm1.Input
If MSComm1.InputMode = comInputModeBinary Then
BytReceived() = strBuff
For i = 0 To ubond(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i)) & ""
Else
strData = strData & Hex(BytReceived(i)) & ""
End If
Next
Text1 = Text1 & strData
strData = ""
Else
Text1 = Text1 & strBuff
End If
End Select
End Sub
Private Sub Option1_Click()
MSComm1.InputMode = comInputModeText
End Sub
Private Sub Option2_Click()
MSComm1.InputMode = comInputModeBinary
End Sub 展开
展开全部
LZ的代码缺省打开的是COM3,不知LZ的虚拟串口号是啥。
仔细看了下LZ的COM口选择控件的名称有误,数字1在代码中是字母l,以致造成COM口无法选择。
修改如下:
Private Sub Combo1_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False '关闭通信端口
End If
MSComm1.CommPort = Combo1.ListIndex + 1
Text3 = MSComm1.CommPort
End Sub
Private Sub Command1_Click()
On Error GoTo uerror
If Command1.Caption = "关闭串口" Then
MSComm1.PortOpen = False
Command1.Caption = "打开串口"
Shape1.FillColor = &HFFFFC0
Else
MSComm1.PortOpen = True
Command1.Caption = "关闭窗口"
Shape1.FillColor = &HFF
End If
Exit Sub
uerror:
msg$ = "无效端口号"
Title$ = "串口调试助手"
X = MsgBox(msg$, 48, Title$)
End Sub
Private Sub form_load()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
Else
End If
Combo1.AddItem "COM1"
Combo1.AddItem "COM2"
Combo1.AddItem "COM3"
Combo1.AddItem "COM4"
Combo1.AddItem "COM5"
Combo1.AddItem "COM6"
Combo1.AddItem "COM7"
Combo1.AddItem "COM8"
Combo1.ListIndex = 0
MSComm1.CommPort = Combo1.ListIndex + 1
MSComm1.Settings = "19200,n,8,1"
Command1.Caption = "打开串口"
Shape1.FillColor = &HFFFFC0
Option2.Value = True
Combo2.AddItem "256000"
Combo2.AddItem "128000"
Combo2.AddItem "115200"
Combo2.AddItem "57600"
Combo2.AddItem "38400"
Combo2.AddItem "28800"
Combo2.AddItem "19200"
Combo2.AddItem "9600"
Combo2.AddItem "4800"
Combo2.AddItem "2400"
Combo2.AddItem "1200"
Combo3.AddItem "无 None"
Combo3.AddItem "奇 Odd"
Combo3.AddItem "偶 Even"
Combo4.AddItem "4"
Combo4.AddItem "5"
Combo4.AddItem "5"
Combo4.AddItem "6"
Combo4.AddItem "7"
Combo4.AddItem "8"
Combo5.AddItem "1"
Combo5.AddItem "2"
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 512
MSComm1.RThreshold = 1
End Sub
Private Sub MSComm1_OnComm()
Dim BytReceived() As Byte
Dim i As Integer
Select Case MSComm1.CommEvent
Case comEventRxOver
Text1.Text = ""
Exit Sub
Case comEvReceive
MSComm1.InputLen = 0
strBuff = MSComm1.Input
If MSComm1.InputMode = comInputModeBinary Then
BytReceived() = strBuff
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i)) & ""
Else
strData = strData & Hex(BytReceived(i)) & ""
End If
Next
Text1 = Text1 & strData
strData = ""
Else
Text1 = Text1 & strBuff
End If
End Select
End Sub
Private Sub Option1_Click()
MSComm1.InputMode = comInputModeText
End Sub
Private Sub Option2_Click()
MSComm1.InputMode = comInputModeBinary
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询