Private Sub Form_Load()
MSComm1.CommPort = 1 '选择端口1
MSComm1.Settings = "9600,n,8,1" '波特率9600bps,奇偶校验无,8位数据位,1位停止位
MSComm1.InputLen = 2 '设置Input 一次从接收缓冲读取字节数为2
MSComm1.OutBufferSize = 2 '设置发送缓冲区为2字节
MSComm1.InBufferSize = 2 '设置接收缓冲区为52字节
MSComm1.RThreshold = 2 '每一字符到接受缓冲区都会触发接收事件。若为0 则事件无效。
MSComm1.InputMode = comInputModeBinary '二进制形式读数据,为0为文本形式
MSComm1.PortOpen = True '打开串口1
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.InBufferCount = 0 '滑空接收缓冲区
End Sub
'=============PC机接收数据处理程序=======================
Private Sub MSComm1_OnComm() '接收处理
Static s As Long
Select Case MSComm1.CommEvent
Case comEvReceive '...有接受事件发生
Dim strdata As String
Dim InByte() As Byte
InByte = MSComm1.Input
For I = 0 To UBound(InByte)
If Len(Hex(InByte(I))) = 1 Then
strdata = strdata & "0" + Hex(InByte(I))
Else
strdata = strdata & Hex(InByte(I))
End If
Next I
MSComm1.InBufferCount = 0 '...清空输入寄存器
End Select
'-----------------------------------------------
Select Case strdata
Case "0114"
启动成功 = "ok"
StatusBar1.Panels(3).Text = "01 14"
n1 = "ok"
Case "0115"
启动失败 = "ok"
End Select
End Sub
Private Sub fasong(f0, f1) '发送数据子程序
Dim X As Variant
Dim Y As Variant
Dim a(1) As Byte
X = f0
Y = f1
a(0) = X
a(1) = Y
MSComm1.Output = a
MSComm1.OutBufferCount = 0
End Sub