1,453
社区成员
发帖
与我相关
我的任务
分享Option Explicit
Dim i As Integer
Dim strData As String
Dim arr() As Byte
Private Sub Command1_Click()
Dim Data1(31) As Byte
MSComm1.InputLen = 0
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
For i = 0 To 31
'取0到90之间的任意数字
Data1(i) = Int(Rnd * 90) + 1 'HScroll1(i).Value
Next
MSComm1.Output = Data1
End Sub
Private Sub Command2_Click()
MSComm1.PortOpen = True
End Sub
Private Sub Form_Load()
'设置端口
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1 "
MSComm1.InputMode = comInputModeBinary
MSComm1.PortOpen = True
'MSComm2.CommPort = 2
MSComm2.RThreshold = 1
'MSComm2.PortOpen = True
Text1 = ""
End Sub
Private Sub mscomm1_OnComm()
Dim buf As String
Dim intInputLen As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0
intInputLen = MSComm1.InBufferCount
ReDim arr(intInputLen)
buf = MSComm1.Input
'MsgBox buf
arr() = buf
jieshou
Text1 = strData
Print Len(strData) / 2
End Select
End Sub
Public Function jieshou() '接收数据处理为16进制
For i = 0 To UBound(arr)
If Len(Hex(arr(i))) = 1 Then
strData = strData & "0" & Hex(arr(i))
Else
strData = strData & Hex(arr(i))
End If
Next
End Function