1.如何用VB的源码MScomm打开两端口(如COM2和3),并实现通信?
如何用VB的源码量化波段趋势源码MScomm打开两端口(如COM2和3),并实现通信?
用以下代码检查你机器有几个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 = Then
Print "端口" & a & "已打开,请关闭其它应用程序占用的源码源码图鉴文件端口" & a
ElseIf Err.Number = 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 =
MSComm1.Settings = ",n,8,1"
MSComm1.CommPort = 2
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
MSComm2.Settings = ",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() '接收数据处理为进制字符
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() As Byte
Dim l As Integer
byts(0) = &H1
byts(1) = &H
byts(2) = &H0
byts(3) = &H0
byts(4) = &HFF
For l = 5 To
byts(l) = - l + 5
Next
MSComm2.Output = byts
End Sub