请教高手,串口通信数据转换问题!
我在文本框输入12发送出去,单片机接收后为3132,如何在VB里编程让单片机接收后为十六进制的12。
[解决办法]
1、
dim tmp as byte
text1.text=12
tmp=“&H”& text1.text
发送tmp
2、从单片机的接收来看是把12转成了它们的ASCII码 31 32 (16进制)
这样你可以考虑发送 chr(18)
[解决办法]
dim temp as string
dim senddata() as byte
dim i as integer,strlen as string
temp = "123456789"
strlen = len(temp)
if strlen mod 2 <> 0 then '判断长度是否为偶数
temp = "0" & temp
strlen = strlen +1
end if
redim senddata(strlen\2)
for i = 0 to strlen\2 - 1
senddata(i) = Val("&h" & (Mid(temp, i * 2 + 1, 1) & Mid(temp, (i + 1) * 2, 1)))
debug.print hex(senddata(i))
next
最后senddata就是要发送的数据了。
[解决办法]
strlen as string
改成strlen as integer
[解决办法]
senddata(i) = Val("&h" & (Mid(temp, i * 2 + 1, 1) & Mid(temp, (i + 1) * 2, 1)))
可以再简化成
senddata(i) = Val("&h" & (Mid(temp, i * 2 + 1, 2)))