1,502
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Dim strSj As String
Dim bj As String * 2
Private Sub Form_Load()
'MSComm1初始化
MSComm1.CommPort = 1
MSComm1.Settings = "4800,n,8,2"
MSComm1.InputMode = comInputModeText 'ASCII接收
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim i As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
strSj = strSj & MSComm1.Input
Text1 = strSj
If Mid(strSj, 1, 4) = "1B61" And Mid(strSj, 7, 2) = "04" Then '数据判断及处理
If Mid(strSj, 5, 2) = "41" Then
Label1.Caption = "normal"
ElseIf Mid(strSj, 5, 2) = "42" Then
Label1.Caption = "RAM Error"
ElseIf Mid(strSj, 5, 2) = "43" Then
Label1.Caption = "ROM Error"
End If
strSj = ""
ElseIf Mid(strSj, 1, 4) = "1B64" And Mid(strSj, 11, 2) = "04" Then
Text2 = strSj
strSj = ""
End If
End Select
End Sub
Private Sub Command1_Click() '发送自检请求命令
Dim sendsj As String
sendsj = "1A6004"
'bj = Mid(sendsj, 3, 2)
MSComm1.Output = sendsj
End Sub
Private Sub Command2_Click()
Dim sendsj As String
sendsj = "1A6404"
'bj = Mid(sendsj, 3, 2)
MSComm1.Output = sendsj
End Sub
Option Explicit
Dim strHex As String
Private Sub Form_Load()
'MSComm1初始化
MSComm1.CommPort = 1
MSComm1.Settings = "4800,n,8,2"
MSComm1.InputMode = comInputModeBinary '2进制接收
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim inbuff() As Byte
Dim i As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
inbuff = MSComm1.Input
For i = 0 To UBound(inbuff) '将接收的Byte字节流转换为16进制字符串
strHex = strHex & Right("0" & Hex(inbuff(i)), 2)
Next
If Mid(strHex, 1, 2) = "1B" And Len(strHex) = 8 Then '数据判断及处理
Text1 = strHex
If Mid(strHex, 5, 2) = "41" Then
Label1.Caption = "normal"
ElseIf Mid(strHex, 5, 2) = "42" Then
Label1.Caption = "RAM Error"
ElseIf Mid(strHex, 5, 2) = "43" Then
Label1.Caption = "ROM Error"
End If
strHex = ""
End If
End Select
End Sub
Private Sub Command1_Click() '发送自检请求命令
Dim sendsj(2) As Byte
sendsj(0) = &H1A
sendsj(1) = &H60
sendsj(2) = &H4
MSComm1.Output = sendsj
End Sub
Private Sub Command2_Click()
End
End Sub