VB 两个MScomm控件的问题。
MScomm1完全没有任何问题。收发自如。MScomm2和MScomm1的编程模式一模一样。。但是都收不到数据啊。。
串口的灯在闪烁。说明确实收发成功了?
为什么不能在Text中显示呢?
还有别的需要注意的吗? --------------------编程问答-------------------- 没有代码,无法回答。 --------------------编程问答-------------------- Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Sub Command1_Click()
MSComm2.PortOpen = True
End Sub
Private Sub Command3_Click()
Dim Temp(0) As Byte
Dim strBuff As String
If Option1.Value = True Then '如果显示16进制发送则进行16进制处理 ,这里只发送一个
Temp(0) = "&H" & TextSend
MSComm2.Output = Temp '发送一个16进制
Else
strBuff = TextSend
End If
If MSComm2.PortOpen = False Then
MsgBox "请打开串口"
End If
On Error GoTo uerror
MSComm2.Output = strBuff
Label11.Caption = Label11.Caption + Len(strBuff) '发送计数
uerror:
End Sub
Private Sub Form_Load()
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
If MSComm2.PortOpen = True Then MSComm2.PortOpen = False
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.AddItem "COM9"
Combo1.AddItem "COM10"
Combo1.AddItem "COM11"
Combo1.AddItem "COM12"
Combo1.AddItem "COM13"
Combo1.AddItem "COM14"
Combo1.AddItem "COM15"
Combo1.AddItem "COM16"
Combo1.ListIndex = 2
Combo2.AddItem "256000"
Combo2.AddItem "128000"
Combo2.AddItem "115200"
Combo2.AddItem "57600"
Combo2.AddItem "38400"
Combo2.AddItem "28800"
Combo2.AddItem "19200"
Combo2.AddItem "14400"
Combo2.AddItem "12800"
Combo2.AddItem "11520"
Combo2.AddItem "9600"
Combo2.AddItem "4800"
Combo2.AddItem "2400"
Combo2.AddItem "1200"
Combo2.AddItem "600"
Combo3.AddItem "无校验"
Combo3.AddItem "奇校验"
Combo3.AddItem "偶校验"
Combo4.AddItem "4"
Combo4.AddItem "5"
Combo4.AddItem "6"
Combo4.AddItem "7"
Combo4.AddItem "8"
Combo5.AddItem "1"
Combo5.AddItem "2"
MSComm1.CommPort = Combo1.ListIndex + 1
MSComm1.Settings = "9600,n,8,1"
ComOpen.Caption = "打开串口"
Shape1.FillColor = &HFFFFC0
MSComm2.CommPort = 9
MSComm2.Settings = "9600,n,8,1"
MSComm2.InputMode = comInputModeBinary
End Sub
Private Sub ComOpen_Click()
On Error GoTo uerror '发现错误跳转到错误处理
If ComOpen.Caption = "关闭串口" Then
MSComm1.PortOpen = False
ComOpen.Caption = "打开串口" '按钮文字改变
Shape1.FillColor = &HFFFFC0 '灯颜色改变
Else
MSComm1.PortOpen = True
ComOpen.Caption = "关闭串口"
Shape1.FillColor = &HFF
End If
Exit Sub
uerror:
msg$ = "无效端口号" '错误显示
Title$ = "串口调试助手"
X = MsgBox(msg$, 48, Title$) '48标示显示警告图标
End Sub
Private Sub MSComm1_OnComm()
Dim BytReceived() As Byte
Dim strBuff As String
Dim i As Integer
Select Case MSComm1.CommEvent '事件发生
Case 2
Cls
MSComm1.InputLen = 0 '读入缓冲区全部内容
strBuff = MSComm1.Input '读入到缓冲区
Label10.Caption = Label10.Caption + Len(strBuff) '接收计数
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)) & " " '如果只有一个字符,则前补0,如F显示0F,最后补空格
Else '方便显示观察如: 00 0F FE
strData = strData & Hex(BytReceived(i)) & " "
End If
Next
TextReceive = TextReceive & strData
strData = ""
Else
TextReceive = TextReceive & strBuff
End If
End Select
End Sub
Private Sub ComSend1_Click() '手动发送
Dim Temp(0) As Byte
Dim strBuff As String
If Option1.Value = True Then '如果显示16进制发送则进行16进制处理 ,这里只发送一个
Temp(0) = "&H" & TextSend
MSComm1.Output = Temp '发送一个16进制
Else
strBuff = TextSend
End If
If MSComm1.PortOpen = False Then
MsgBox "请打开串口"
End If
On Error GoTo uerror
MSComm1.Output = strBuff
Label11.Caption = Label11.Caption + Len(strBuff) '发送计数
uerror:
End Sub
Private Sub ComSend2_Click()
If ComSend2.Caption = "自动发送" Then
ComSend2.Caption = "关闭自动发送"
Timer1.Interval = TextTime.Text
Timer1.Enabled = True
Else
ComSend2.Caption = "自动发送"
Timer1.Enabled = False
End If
End Sub
Private Sub ComClean1_Click()
TextSend.Text = "" '清空发送窗口
End Sub
Private Sub MSComm2_OnComm()
Dim BytReceived() As Byte
Dim strBuff As String
Dim i As Integer
Select Case MSComm2.CommEvent '事件发生
Case 2
Cls
MSComm2.InputLen = 0 '读入缓冲区全部内容
strBuff = MSComm2.Input '读入到缓冲区
Label10.Caption = Label10.Caption + Len(strBuff) '接收计数
If MSComm2.InputMode = comInputModeBinary Then
BytReceived() = strBuff '如果是二进制接收模式则进行数据处理,否则直接显示字符串
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i)) & " " '如果只有一个字符,则前补0,如F显示0F,最后补空格
Else '方便显示观察如: 00 0F FE
strData = strData & Hex(BytReceived(i)) & " "
End If
Next
TextReceive = TextReceive & strData
strData = ""
Else
TextReceive = TextReceive & strBuff
End If
End Select
End Sub
Private Sub Option3_Click()
MSComm1.InputMode = comInputModeBinary '选择接收方式
End Sub
Private Sub Option4_Click()
MSComm1.InputMode = comInputModeText '选择接收方式
End Sub
Private Sub Timer1_Timer()
Call ComSend1_Click '定时调用手动发送
End Sub
Private Sub Timer2_Timer()
If Combo3 = "无校验" Then
MSComm1.Settings = Str(Combo2) + "N" + Str(Combo4) + Str(Combo5)
ElseIf Combo3 = "奇校验" Then
MSComm1.Settings = Str(Combo2) + "O" + Str(Combo4) + Str(Combo5)
ElseIf Combo3 = "偶校验" Then
MSComm1.Settings = Str(Combo2) + "E" + Str(Combo4) + Str(Combo5)
End If
End Sub
Private Sub Combo1_Click()
If MSComm1.PortOpen = True Then '如果串口打开先关闭后再进行其他操作
MSComm1.PortOpen = False
End If
MSComm1.CommPort = Combo1.ListIndex + 1 '读取com口号
End Sub
Private Sub ComClean3_Click()
Label10.Caption = 0
Label11.Caption = 0
End Sub
Private Sub ComClean2_Click()
TextReceive.Text = "" '接收窗口
End Sub
--------------------编程问答--------------------
见代码。 --------------------编程问答-------------------- MSComm1和MSComm2分别对应不同的串口? --------------------编程问答-------------------- 对啊对啊。~~ --------------------编程问答-------------------- MSComm2_OnComm中的strBuff应该定义为Variant数据类型:
Private Sub MSComm2_OnComm()--------------------编程问答-------------------- MScomm1中的不用Variant类型啊。。是可以的鹅 --------------------编程问答-------------------- 将下位机对换一下串口,检查是否是下位机或者有问题
Dim BytReceived() As Byte
Dim strBuff As Variant
Dim i As Integer
Select Case MSComm2.CommEvent '事件发生
Case 2
Cls
MSComm2.InputLen = 0 '读入缓冲区全部内容
strBuff = MSComm2.Input '读入到缓冲区
Label10.Caption = Label10.Caption + Len(strBuff) '接收计数
If MSComm2.InputMode = comInputModeBinary Then
BytReceived() = strBuff '如果是二进制接收模式则进行数据处理,否则直接显示字符串
For i = 0 To UBound(BytReceived)
If Len(Hex(BytReceived(i))) = 1 Then
strData = strData & "0" & Hex(BytReceived(i)) & " " '如果只有一个字符,则前补0,如F显示0F,最后补空格
Else '方便显示观察如: 00 0F FE
strData = strData & Hex(BytReceived(i)) & " "
End If
Next
TextReceive = TextReceive & strData
strData = ""
Else
TextReceive = TextReceive & strBuff
End If
End Select
End Sub
补充:VB , 控件