我在做串口通讯程序时遇到了这样一个问题:在VB中我需要将一个浮点型数据(single)转换成一个四位字节的字符串后,送给下位才能实现通讯.
例如:需要将数据 1.11
转换成 (3F 8E 14 7B) 字符串的形式发给下位
另外,如何将下位传给我的字符串(3F 8E 14 7B)
转换成浮点数 1.11
请各位高手给看看,谢谢了!
例如:需要将数据 1.11
转换成 (3F 8E 14 7B) 字符串的形式发给下位
另外,如何将下位传给我的字符串(3F 8E 14 7B)
转换成浮点数 1.11
请各位高手给看看,谢谢了!
Option ExplicitPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Sub Command1_Click()
Dim num As Single
num = 1.11
Dim buff(3) As Byte
CopyMemory buff(0), num, 4
Dim s As String
s = Hex(buff(3)) + " " + Hex(buff(2)) + " " + Hex(buff(1)) + " " + Hex(buff(0))
MsgBox s
End SubPrivate Sub Command2_Click()
Dim s As String
s = "3F 8E 14 7B"
Dim buff(3) As Byte
Dim a() As String
a = Split(s, " ")
Dim i As Long
For i = UBound(a) To 0 Step -1
buff(3 - i) = Val("&H" + a(i))
Next
Dim J As Single
CopyMemory J, buff(0), 4
MsgBox CStr(J)
End Sub
在按下Command1_Click()时,提示:实时错误'453'找不到DLL入口点RtlMoveMemory in kernel32.
在按下Command2_Click()时,提示:实时错误'6' 溢出
请问我还需要设置什么地方吗?
谢谢!!!
另外,copymemory的函数声明你最好通过你的api浏览器添加
如果是传过来的是一个double数据类型的话,你也可以使用copymemory
dim x as double
copymemory x,buff,8