Dim vReceived As Variant If MSComm1.CommEvent = comEvReceive Then vReceived = MSComm1.Input ElseIf MSComm1.CommEvent > 1000 Then 'Clear receive buffer if any errorEvent occoured MSComm1.InBufferCount = 0 Exit Sub Else Exit Sub End If
Public Sub MSComm2_OnComm() Dim Buffer As Variant Dim i As Integer ' 依据 CommEvent 属性进行分支 Select Case MSComm2.CommEvent
' 事件信息 Case comEvReceive i = MSComm2.InBufferCount Buffer = MSComm2.Input ShowData txtTerm, (StrConv(Buffer, vbUnicode)), Buffer, i end select Private Static Sub ShowData(Term As Control, Data As String, Buffer As Variant, DataLenth As Integer) On Error GoTo Handler Const MAXTERMSIZE = 320000 Dim TermSize, i As Long Dim strtemp, s2 As String Dim s1 As Byte Dim n3, n1, n2, StrLen As Integer ' DoEvents ' 确定现存的文本不会太大。
For n3 = 0 To DataLenth - 1 ' DoEvents s1 = Buffer(n3) s2 = Hex(s1) s2 = "0" & s2 s2 = Right(s2, 2) Term.SelText = s2 & " " Term.SelStart = Term.SelStart + 3 HexWhere = HexWhere + 3 If HexWhere > HexWhid * 3 - 2 Then Term.Text1.SelText = Chr(13) & Chr(10) Term.Text1.SelStart = Term.SelStart + 2 HexWhere = 0 End If Next n3 end sub 直接考代码即可 mscomm1.inputmode=1 mscomm1.rthreshold=1
你说mscomm1.input有值可能是在断点调试时用鼠标停留在其上看到的,但是它已被从接收缓冲区读出,s自然就无值了(其实将鼠标移开再移回去也看不到值了),要监测应该用 s=mscomm1.input debug.print s 或 text1.text=text1.text & s & chr(13) & chr(10) 若是二进制接收模式还应先转换为可显示字符
If MSComm1.CommEvent = comEvReceive Then
vReceived = MSComm1.Input
ElseIf MSComm1.CommEvent > 1000 Then
'Clear receive buffer if any errorEvent occoured
MSComm1.InBufferCount = 0
Exit Sub
Else
Exit Sub
End If
Dim Buffer As Variant
Dim i As Integer
' 依据 CommEvent 属性进行分支
Select Case MSComm2.CommEvent
' 事件信息
Case comEvReceive
i = MSComm2.InBufferCount
Buffer = MSComm2.Input
ShowData txtTerm, (StrConv(Buffer, vbUnicode)), Buffer, i
end select
Private Static Sub ShowData(Term As Control, Data As String, Buffer As Variant, DataLenth As Integer)
On Error GoTo Handler
Const MAXTERMSIZE = 320000
Dim TermSize, i As Long
Dim strtemp, s2 As String
Dim s1 As Byte
Dim n3, n1, n2, StrLen As Integer
' DoEvents
' 确定现存的文本不会太大。
For n3 = 0 To DataLenth - 1
' DoEvents
s1 = Buffer(n3)
s2 = Hex(s1)
s2 = "0" & s2
s2 = Right(s2, 2)
Term.SelText = s2 & " "
Term.SelStart = Term.SelStart + 3
HexWhere = HexWhere + 3
If HexWhere > HexWhid * 3 - 2 Then
Term.Text1.SelText = Chr(13) & Chr(10)
Term.Text1.SelStart = Term.SelStart + 2
HexWhere = 0
End If
Next n3
end sub
直接考代码即可
mscomm1.inputmode=1
mscomm1.rthreshold=1
s=mscomm1.input
debug.print s
或
text1.text=text1.text & s & chr(13) & chr(10)
若是二进制接收模式还应先转换为可显示字符