实现16进制接收实质就是按2进制接收
设置MSComm控件的属性InputMode = comInputModeBinary '二进制接收
接收后由HEX函数转为16进制字符串形式显示
Option Explicit
Dim strData As String
Dim bytInput() As Byte
Private Sub MsComm1_OnComm()
Dim intInputLen As Integer
Select Case Me.MSComm2.CommEvent
Case comEvReceive
'此处添加处理接收的代码
MSComm1.InputMode = comInputModeBinary '二进制接收
intInputLen = MSComm1.InBufferCount
ReDim bytInput(intInputLen)
bytInput = 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
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 512
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
Text1 = ""
End Sub
参考一下吧:
发送代码 :
Dim Send_Chr(5) As Byte
Dim Send_Data As String
Send_Chr(0) = &H68
Send_Chr(1) = &H3
Send_Chr(2) = &H10 '命令
Send_Chr(3) = &H1 '网关
Check_val = 0
For i = 0 To 3
Check_val = Check_val + Send_Chr(i)
Next i
Send_Chr(4) = Check_val '校验
For i = 0 To 5
Text1.Text = Text1.Text & Send_Chr(i) & " " 此处显示的是6个10进制的数据
Next i
.Output = Send_Chr
接受
Buffer = MSComm1.Input ' 接收一个二进制数据
Arr(0) = AscB(Buffer) '存入到二进制数组数组中
Receive_Count_Byte = Receive_Count_Byte + 1
Real_Receive = Arr(0) '
Text1.Text = Text1.Text & Real_Receive & " " '接收的数据显示到文本框中
呵呵 这个我以前做过 兄弟 收到的数据都是2进制码,你把收到的数据处理一下 不就是16进制了吗?
具体我记得不清楚了 仅提供思路