vb串口通信中怎么实现16进制的接收?
PrivateSubForm_Load()MSComm1.CommPort=1MSComm1.Settings="9600,n,8,1"MSComm1.InputMode...
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = 0
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub cmdSend_Click()
MSComm1.Output = Textsend.Text
End Sub
Private Sub MSComm1_OnComm()
Dim intInputLen As Integer
Select Case Me.MSComm1.CommEvent
Case comEvReceive
TextReceive.Text = TextReceive.Text + MSComm1.Input
End Select
End Sub
Private Sub Cmdauto_Click()
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
MSComm1.Output = Textsend.Text
TextReceive.Text = TextReceive.Text + MSComm1.Input
End Sub
Private Sub Cmdstop_Click()
Timer1.Enabled = False
End Sub
Private Sub Cmdquit_Click()
MSComm1.PortOpen = False
End
End Sub
Private Sub CmdReceivecl_Click()
TextReceive.Text = ""
End Sub
Private Sub Cmdsendcl_Click()
Textsend.Text = ""
End Sub
上面是实现文本类型的通信,怎样加入十六进制接收的功能? 展开
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputMode = 0
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub cmdSend_Click()
MSComm1.Output = Textsend.Text
End Sub
Private Sub MSComm1_OnComm()
Dim intInputLen As Integer
Select Case Me.MSComm1.CommEvent
Case comEvReceive
TextReceive.Text = TextReceive.Text + MSComm1.Input
End Select
End Sub
Private Sub Cmdauto_Click()
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
MSComm1.Output = Textsend.Text
TextReceive.Text = TextReceive.Text + MSComm1.Input
End Sub
Private Sub Cmdstop_Click()
Timer1.Enabled = False
End Sub
Private Sub Cmdquit_Click()
MSComm1.PortOpen = False
End
End Sub
Private Sub CmdReceivecl_Click()
TextReceive.Text = ""
End Sub
Private Sub Cmdsendcl_Click()
Textsend.Text = ""
End Sub
上面是实现文本类型的通信,怎样加入十六进制接收的功能? 展开
3个回答
推荐于2018-04-12 · 知道合伙人软件行家
关注
展开全部
实现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
设置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 & " " '接收的数据显示到文本框中
发送代码 :
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进制了吗?
具体我记得不清楚了 仅提供思路
具体我记得不清楚了 仅提供思路
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询