VB中MSComm控件来发送接收16进制数怎样解决?
外面的仪器接收到十六进制的“FEo3ooo1o1o1FD”信号后,会以十六进制的方式返回数据,我要把它显示在文本框里面,能写下完整的程序么?在线等,各位大神,帮帮忙吧,谢...
外面的仪器接收到十六进制的“FE o3 oo o1 o1 o1 FD”信号后,会以十六进制的方式返回数据,我要把它显示在文本框里面,能写下完整的程序么?
在线等,各位大神,帮帮忙吧,谢谢 展开
在线等,各位大神,帮帮忙吧,谢谢 展开
1个回答
展开全部
Private Sub MSComm1_OnComm()
With MSComm1
Select Case .CommEvent
Case comEvReceive
Call RecieveText
End Select
End With
End Sub
'功能:从输入缓冲区中接收数据并显示到界面上
Private Sub RecieveText()
Static bFrameOver As Boolean '当前处理指令帧处理完毕
Dim lData As Long
Dim j As Integer
Dim sTemp As String
Dim sNewStr As String
Dim byteInBytes() As Byte
Dim i As Integer
Dim s As String
Dim nTemp As Integer
Dim sResultBytesArr() As String
Dim sFrameDataVal As String '接收到数据中分解出一帧的结果
Dim nCollectCupNo As Integer
Dim nRet As Integer
Dim RecievesInstruction As String '接受到的指令
On Error GoTo errExit
' bFrameOver = False
If MSComm1.PortOpen Then
'收数据
With MSComm1
byteInBytes = .Input
DoEvents
End With
ReIn:
sTemp = "" 'FE o3 oo o1 o1 o1 FD
For i = LBound(byteInBytes) To UBound(byteInBytes)
bFrameOver = False
'新到一个字节数据
s = Hex$(byteInBytes(i))
sNewStr = IIf(Len(s) < 2, "0" & s, s) '根据情况追加前导0后的字符串
If sNewStr = "FE" Then RecievesInstruction = "" '接收到帧头FE
If sNewStr = "FD" Then bFrameOver = True '接收到帧尾FD
'''''收集收到的串口数据
RecievesInstruction = RecievesInstruction & sNewStr
If bFrameOver = True Then
Text1.Text = RecievesInstruction
RecievesInstruction = ""
End If
Next_For:
Next i
byteInBytes = MSComm1.Input
If UBound(byteInBytes) > 0 Then GoTo ReIn
End If
Exit Sub
errExit:
MsgBox Err.Description
End Sub
With MSComm1
Select Case .CommEvent
Case comEvReceive
Call RecieveText
End Select
End With
End Sub
'功能:从输入缓冲区中接收数据并显示到界面上
Private Sub RecieveText()
Static bFrameOver As Boolean '当前处理指令帧处理完毕
Dim lData As Long
Dim j As Integer
Dim sTemp As String
Dim sNewStr As String
Dim byteInBytes() As Byte
Dim i As Integer
Dim s As String
Dim nTemp As Integer
Dim sResultBytesArr() As String
Dim sFrameDataVal As String '接收到数据中分解出一帧的结果
Dim nCollectCupNo As Integer
Dim nRet As Integer
Dim RecievesInstruction As String '接受到的指令
On Error GoTo errExit
' bFrameOver = False
If MSComm1.PortOpen Then
'收数据
With MSComm1
byteInBytes = .Input
DoEvents
End With
ReIn:
sTemp = "" 'FE o3 oo o1 o1 o1 FD
For i = LBound(byteInBytes) To UBound(byteInBytes)
bFrameOver = False
'新到一个字节数据
s = Hex$(byteInBytes(i))
sNewStr = IIf(Len(s) < 2, "0" & s, s) '根据情况追加前导0后的字符串
If sNewStr = "FE" Then RecievesInstruction = "" '接收到帧头FE
If sNewStr = "FD" Then bFrameOver = True '接收到帧尾FD
'''''收集收到的串口数据
RecievesInstruction = RecievesInstruction & sNewStr
If bFrameOver = True Then
Text1.Text = RecievesInstruction
RecievesInstruction = ""
End If
Next_For:
Next i
byteInBytes = MSComm1.Input
If UBound(byteInBytes) > 0 Then GoTo ReIn
End If
Exit Sub
errExit:
MsgBox Err.Description
End Sub
更多追问追答
追问
追答
发送就使用MSComm1.Output 看看MSDN
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询