Option Explicit
Dim strData As String
Dim bytInput() As Byte
Private Sub Form_Load()
MSComm1.Settings = "2400,n,8,1"
MSComm1.CommPort = 4
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
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
晕。你还要多学学VB啊。哪有你这么写的。
MSComm1.InputMode = comInputModeText (这里改成BINARY的,你怎么用ASCII形式传输呢?)
多参考一下人家的例子。不要想当然地自己写。很可能有哪个对象的属性没有设置就不行了。
Text1.Text = AscB(MSComm1.Input)
ff