您好,欢迎来到赴品旅游。
搜索
您的当前位置:首页VB的MScomm打开两端口(如COM2和3),

VB的MScomm打开两端口(如COM2和3),

来源:赴品旅游
如何用VB的MScomm打开两端口(如COM2和3),并实现通信?

悬赏分:30|解决时间:2008-3-14 10:02|提问者:zhanghechuan

问题补充:

那么串口2怎么打开呢?》我是新手 可不可以说的详细点啊

最好有源代码 太感谢了

最佳答案

用以下代码检查你机器有几个COM口,那些COM口可用,那些不存在, Option Explicit Dim a As Integer

Private Sub Command1_Click() On Error GoTo uerror For a = 1 To 4

MSComm1.CommPort = a

MSComm1.PortOpen = True '当True时是打开 If MSComm1.PortOpen = True Then Print \"可用Com号= \"; a

MSComm1.PortOpen = False 当False时是关闭 Else End If Next Exit Sub uerror:

If Err.Number = 8005 Then

Print \"端口\" & a & \"已打开,请关闭其它应用程序占用的端口\" & a ElseIf Err.Number = 8002 Then Print \"出错Com号= \"; a End If Resume Next

End Sub

以下代码使用COM2口和COM3口通信: Option Explicit

Dim strData As String Dim bytInput() As Byte Dim dataSend() As Byte Dim sj() As Byte Dim i As Long Dim Ulen As Long Dim Llen As Long

Dim for_Sum As Long Dim Yu_sum As Integer

Private Sub Form_Load() '初始化 Timer2.Interval = 10

MSComm1.Settings = \"9600,n,8,1\" MSComm1.CommPort = 2 MSComm1.RThreshold = 1 MSComm1.PortOpen = True MSComm2.Settings = \"9600,n,8,1\" MSComm2.CommPort = 3 MSComm2.RThreshold = 1 MSComm2.PortOpen = True End Sub

Private Sub MsComm1_OnComm() Dim intInputLen As Integer

Select Case Me.MSComm1.CommEvent Case comEvReceive

'此处添加处理接收的代码

Me.MSComm1.InputMode = comInputModeBinary '二进制接收 intInputLen = Me.MSComm1.InBufferCount ReDim bytInput(intInputLen) bytInput = Me.MSComm1.Input jieshou End Select End Sub

Public Function jieshou() '接收数据处理为16进制字符 Dim i As Integer

For i = 0 To UBound(bytInput) If Len(Hex(bytInput(i))) = 1 Then

strData = strData & \"0\" & Hex(bytInput(i)) Else

strData = strData & Hex(bytInput(i)) End If Next

RichTextBox1 = strData Text1 = Len(strData) \\ 2 End Function

Private Sub Command2_Click() Dim byts(131) As Byte

Dim l As Integer byts(0) = &H1 byts(1) = &H39 byts(2) = &H0 byts(3) = &H0 byts(4) = &HFF For l = 5 To 131 byts(l) = 131 - l + 5 Next

MSComm2.Output = byts End Sub

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- fupindai.com 版权所有 赣ICP备2024042792号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务