VB中如何以十六进制通过串口发送01 01 20
1个回答
展开全部
只要发送吗,我这有一份程序,你看看能不能用。这个程序是没有问题的,把Text1中的文字以16进制的形式发送出去(不包括转换16进制的过程)。
'十六进制发送
Private Sub Hexsent()
Dim hexchrlen%, Hexchr As String, hexcyc%, hexmid As Byte, hexmiddle As String, CmdLenth As Integer
Dim hexchrgroup() As Byte, i As Integer
hexchrlen = Len(Text1text)
For hexcyc = 1 To hexchrlen '检查Text1文本框内数值是否合适
Hexchr = Mid(Text1text, hexcyc, 1)
If InStr("0123456789ABCDEFabcdef", Hexchr) = 0 Then
MsgBox "无效的数值,请重新输入", , "错误信息"
Exit Sub
End If
Next
ReDim hexchrgroup(1 To hexchrlen \ 2) As Byte
For hexcyc = 1 To hexchrlen Step 2 '将文本框内数值分成两个、两个
i = i + 1
Hexchr = Mid(Text1text, hexcyc, 2)
hexmid = Val("&H" & CStr(Hexchr))
hexchrgroup(i) = hexmid
Next
CmdLenth = 5 + hexchrgroup(5) * 2
MSComm1.RThreshold = CmdLenth
MSComm1.Output = hexchrgroup
End Sub
'十六进制发送
Private Sub Hexsent()
Dim hexchrlen%, Hexchr As String, hexcyc%, hexmid As Byte, hexmiddle As String, CmdLenth As Integer
Dim hexchrgroup() As Byte, i As Integer
hexchrlen = Len(Text1text)
For hexcyc = 1 To hexchrlen '检查Text1文本框内数值是否合适
Hexchr = Mid(Text1text, hexcyc, 1)
If InStr("0123456789ABCDEFabcdef", Hexchr) = 0 Then
MsgBox "无效的数值,请重新输入", , "错误信息"
Exit Sub
End If
Next
ReDim hexchrgroup(1 To hexchrlen \ 2) As Byte
For hexcyc = 1 To hexchrlen Step 2 '将文本框内数值分成两个、两个
i = i + 1
Hexchr = Mid(Text1text, hexcyc, 2)
hexmid = Val("&H" & CStr(Hexchr))
hexchrgroup(i) = hexmid
Next
CmdLenth = 5 + hexchrgroup(5) * 2
MSComm1.RThreshold = CmdLenth
MSComm1.Output = hexchrgroup
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询