If Combo1.Text = "COM1" Then Frm2.MSComm1.CommPort = 1 End If If Combo1.Text = "COM2" Then Frm2.MSComm1.CommPort = 2 End If If Combo1.Text = "COM3" Then Frm2.MSComm1.CommPort = 3 End If If Combo1.Text = "COM4" Then Frm2.MSComm1.CommPort = 4 End If If Combo2.Text = "2400" Then Frm2.MSComm1.Settings = "2400,n,8,1" End If If Combo2.Text = "4800" Then Frm2.MSComm1.Settings = "4800,n,8,1" End If If Combo2.Text = "9600" Then Frm2.MSComm1.Settings = "9600,n,8,1" End If If Combo2.Text = "14400" Then Frm2.MSComm1.Settings = "14400,n,8,1" End If If Combo2.Text = "155200" Then Frm2.MSComm1.Settings = "155200,n,8,1" End If End Sub下面石达开串口的: Private Sub Commddkck_Click() If Frm2.MSComm1.PortOpen = False Then Frm2.MSComm1.PortOpen = True '打开通信口 Commddkck.Caption = "关闭串口" shpconnect.FillColor = &HFF&
ElseIf MSComm1.PortOpen = True Then Frm2.MSComm1.PortOpen = False '关闭通信口 Commddkck.Caption = "开启串口" shpconnect.FillColor = &HFF00& End If Frm2.MSComm1.InputMode = comInputModeBinary '以文本方度接收 Frm2.MSComm1.InBufferCount = 0 '清空接受缓冲区 Frm2.MSComm1.OutBufferCount = 0 '清空传输缓冲区 Frm2.MSComm1.RThreshold = 1 '产生MSComm事件 End Sub接收的: Dim strBuff As String Select Case MSComm1.CommEvent Case 2 MSComm1.InputLen = 0 strBuff = MSComm1.Input BytReceived() = strBuff Dim i As Integer 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 Textreceive.Text = strData TextCL.Text = strData lenInput = Len(strData)
'Text2 = lenInput \ 2 '数据处理代码 End Select 发送的: Private Sub Command1_Click() ' 发送 If Textsend.Text = "" Then MsgBox "发送的数据不能为空!", vbOKOnly, 16 Exit Sub End If Dim sj() As Byte Dim sj_Txt As String Dim i As Integer sj_Txt = Textsend If (Len(Textsend) Mod 3 = 0) Or ((Len(Textsend) + 1) Mod 3 = 0) And Len(Textsend) <> 0 Then '检验16进制字符串长 ReDim sj(Len(sj_Txt) / 3 - 1) For i = 0 To Len(sj_Txt) Step 3 sj(i / 3) = Val("&H" & Mid(sj_Txt, i + 1, 2)) Next If MSComm1.PortOpen = True Then MSComm1.Output = sj Else MSComm1.PortOpen = True MSComm1.Output = sj End If Else MsgBox ("格式不对!") End If End Sub 现在是明显串口是存在的,把本本上的USB转串口都不用了!直接在台式上实验,提示8002无效的串口!不知道是怎么回事。
Private Sub Form_KeyPress (KeyAscii As Integer) Dim Buffer as Variant
' 设置并打开窗口 MSComm1.CommPort = 1 MSComm1.PortOpen = True Buffer = Chr$(KeyAscii) MSComm1.Output = Buffer End Sub 这是msdn的例子, 没查到什么OutputSignal
做小程序玩玩可以,如果你真想把这种东西运用到工程里,建议换 VC 来做把,这种建议是有依据的,不相信你可以看看你在搞平凡的串口通讯时的 CPU 占用率就明白了。如果工程中同时还需要处理图形、网络、数据库等资源,这样的工程是没法继续下去的。建议采用多线程处理方案,但因为 VB 对多线程处理起来比较复杂,很难做到兼容性、稳定性比较好的程度,所以建议使用 C 语言来开发,效率高,稳定性也不错。 你可以参考我的这个例子,其实也是很简单的,我还用了很多 VB 的开发思路进去,你应该能看懂。最重要的是实现了多线程,比用 Microsoft 的控件效率高很多,在几年前的电脑上跑,这种技术 CPU 占用峰值也就是1%,通常是看不出有什么占用率,为同样是 VC ,跑 Microsoft 的控件,CPU 占用峰值可以跑到 100%,通常是 80% 以上,所以这种方案的效率是很不错的,你可以看一下。 http://download.csdn.net/detail/SupermanKing/2690778
这句要改,你一个字一个字地获取,然后一个一个地转成 byte型 再一个个地发送
或者直接发送 MSComm1.Output = Chr(&H5) & Chr(&H52) & Chr(&H1) & Chr(&H3) & Chr(&H0) & Chr(&H1) & Chr(&H4) & Chr(&H50)
TextCL.Visible = False
If Combo1.Text = "COM1" Then
Frm2.MSComm1.CommPort = 1
End If
If Combo1.Text = "COM2" Then
Frm2.MSComm1.CommPort = 2
End If
If Combo1.Text = "COM3" Then
Frm2.MSComm1.CommPort = 3
End If
If Combo1.Text = "COM4" Then
Frm2.MSComm1.CommPort = 4
End If
If Combo2.Text = "2400" Then
Frm2.MSComm1.Settings = "2400,n,8,1"
End If
If Combo2.Text = "4800" Then
Frm2.MSComm1.Settings = "4800,n,8,1"
End If
If Combo2.Text = "9600" Then
Frm2.MSComm1.Settings = "9600,n,8,1"
End If
If Combo2.Text = "14400" Then
Frm2.MSComm1.Settings = "14400,n,8,1"
End If
If Combo2.Text = "155200" Then
Frm2.MSComm1.Settings = "155200,n,8,1"
End If
End Sub下面石达开串口的:
Private Sub Commddkck_Click()
If Frm2.MSComm1.PortOpen = False Then Frm2.MSComm1.PortOpen = True '打开通信口 Commddkck.Caption = "关闭串口"
shpconnect.FillColor = &HFF&
ElseIf MSComm1.PortOpen = True Then Frm2.MSComm1.PortOpen = False '关闭通信口 Commddkck.Caption = "开启串口"
shpconnect.FillColor = &HFF00&
End If Frm2.MSComm1.InputMode = comInputModeBinary '以文本方度接收
Frm2.MSComm1.InBufferCount = 0 '清空接受缓冲区
Frm2.MSComm1.OutBufferCount = 0 '清空传输缓冲区
Frm2.MSComm1.RThreshold = 1 '产生MSComm事件
End Sub接收的:
Dim strBuff As String
Select Case MSComm1.CommEvent
Case 2
MSComm1.InputLen = 0
strBuff = MSComm1.Input
BytReceived() = strBuff
Dim i As Integer
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
Textreceive.Text = strData
TextCL.Text = strData lenInput = Len(strData)
'Text2 = lenInput \ 2
'数据处理代码
End Select
发送的:
Private Sub Command1_Click() ' 发送
If Textsend.Text = "" Then
MsgBox "发送的数据不能为空!", vbOKOnly, 16
Exit Sub
End If
Dim sj() As Byte
Dim sj_Txt As String
Dim i As Integer
sj_Txt = Textsend
If (Len(Textsend) Mod 3 = 0) Or ((Len(Textsend) + 1) Mod 3 = 0) And Len(Textsend) <> 0 Then '检验16进制字符串长
ReDim sj(Len(sj_Txt) / 3 - 1)
For i = 0 To Len(sj_Txt) Step 3
sj(i / 3) = Val("&H" & Mid(sj_Txt, i + 1, 2))
Next
If MSComm1.PortOpen = True Then
MSComm1.Output = sj
Else
MSComm1.PortOpen = True MSComm1.Output = sj
End If
Else
MsgBox ("格式不对!")
End If
End Sub
现在是明显串口是存在的,把本本上的USB转串口都不用了!直接在台式上实验,提示8002无效的串口!不知道是怎么回事。
Frm2.MSComm1.RThreshold = 1 '产生MSComm事件这些东西放在打开串口之前设置试试看
sj(i / 3) = Val("&H" & Mid(sj_Txt, i + 1, 2))这句不妥 Val 返回的是一个double吧(我忘记了是什么类型了)
你最好确保它是byte的输出
Dim Buffer as Variant
' 设置并打开窗口
MSComm1.CommPort = 1
MSComm1.PortOpen = True Buffer = Chr$(KeyAscii)
MSComm1.Output = Buffer
End Sub
这是msdn的例子, 没查到什么OutputSignal
你可以参考我的这个例子,其实也是很简单的,我还用了很多 VB 的开发思路进去,你应该能看懂。最重要的是实现了多线程,比用 Microsoft 的控件效率高很多,在几年前的电脑上跑,这种技术 CPU 占用峰值也就是1%,通常是看不出有什么占用率,为同样是 VC ,跑 Microsoft 的控件,CPU 占用峰值可以跑到 100%,通常是 80% 以上,所以这种方案的效率是很不错的,你可以看一下。
http://download.csdn.net/detail/SupermanKing/2690778