1,453
社区成员
发帖
与我相关
我的任务
分享
Private Sub MSComm1_OnComm()
Dim buf As String
Static counter As Integer ';定义起始位计数器
Select Case MSComm1.CommEvent
Case comEvReceive
buf = MSComm1.Input
Text1.Text = Text1.Text + buf
If InStr(buf, "#") > 0 Then ';“#”作为起始和结束标志,该段对#进行记数
counter = counter + 1
Text2 = counter
End If
If counter = 2 Then ';如果记数为2(起始和结束位被找到),显示传输完毕
MSComm1.PortOpen = False
MsgBox "传输完毕"
End If
End Select
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.InBufferSize = 2048
MSComm1.OutBufferSize = 1024
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = comInputModeText
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
Text1 = ""
End Sub
Option Explicit
Dim strData As String
Dim bytInput() As Byte
Dim i As Integer
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = 1
MSComm1.RThreshold = 1
MSComm1.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
Text1 = strData
End Function