VB 发送十六进制串口数据
我想用VB做的软件,发送一组十六进制串口数据,请问怎么做?数据“30h30h5Fh63h31h0Dh”悬赏100,奉上,请笑纳!...
我想用VB做的软件,发送一组十六进制串口数据,请问怎么做?
数据“30h 30h 5Fh 63h 31h 0Dh”
悬赏100,奉上,请笑纳! 展开
数据“30h 30h 5Fh 63h 31h 0Dh”
悬赏100,奉上,请笑纳! 展开
5个回答
展开全部
Dim 数据(8) As Byte
数据(0) = &H68
数据(1) = &H11
数据(2) = &H22
数据(3) = &H33
数据(4) = &H44
数据(5) = &H55
数据(6) = &H66
数据(7) = &H77
串口1.Write(数据, 0, 8)
//数组可以,单个数据就不对
数据(0) = &H68
数据(1) = &H11
数据(2) = &H22
数据(3) = &H33
数据(4) = &H44
数据(5) = &H55
数据(6) = &H66
数据(7) = &H77
串口1.Write(数据, 0, 8)
//数组可以,单个数据就不对
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
Dim sj() As String
Dim sjByt() As Byte
Dim i As Long
sj = Split(Text1, " ")
ReDim sjByt(UBound(sj))
For i = 0 To UBound(sj)
sjByt(i) = Val("&H" & Str(Val(sj(i))))
Next i
MSComm1.Output = sjByt
End Sub
Private Sub Form_Load()
Text1 = "30h 30h 5Fh 63h 31h 0Dh"
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Dim sandcher() As Byte
ReDim sandcher(5)
sandcher(0) = &H30
sandcher(1) = &H30
sandcher(2) = &H5F
sandcher(3) = &H63
sandcher(4) = &H31
sandcher(5) = &H0D
With MSComm1
If .PortOpen = False Then .PortOpen = True
.Output = sandcher
End With
追问
你这个是锁定发送的命令,能做成 text 文本框,我自己输入什么就发送什么吗?
我的代码格式都是 “02H 50H 4FH 46H 03H” 这样的格式,如果需要转换什么进制,那我也需要转换进制的代码。
追答
您的代码格式每个数值后面都加了H说明是十六进制的 所以不用转换进制 。所以参考下面的知友的代码稍微修改一下如下:
Private Sub Command1_Click()
Dim sj() As String
Dim sjByt() As Byte
Dim i As Long
sj = Split(Text1, " ")
ReDim sjByt(UBound(sj))
For i = 0 To UBound(sj)
sjByt(i) = Val("&H" & Str(Val(left(sj(i),2))))
Next i
MSComm1.Output = sjByt
End Sub
Private Sub Form_Load()
Text1 = “02H 50H 4FH 46H 03H"
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
只要发送吗,我这有一份程序,你看看能不能用。这个程序是没有问题的,把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
更多追问追答
追答
- - 这个有点麻烦呢 你是使用的MSComm控件吧?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询