VB串口读取浮点型数据转换
问个问题,VB串口通讯仪表的时候,接受到的浮点型数据怎么处理?AABBCCDDPrivateSubCommand1_Click()DimBB(0To3)AsByteDim...
问个问题,VB串口通讯仪表的时候,接受到的浮点型数据怎么处理? AA BB CC DD
Private Sub Command1_Click()
Dim BB(0 To 3) As Byte
Dim se As Single
BB(0) = BB
BB(1) = AA
BB(2) = DD
BB(3) = CC
CopyMemory se, ByVal VarPtr(BB(0)), 4
Text1.Text = se
End Sub
我不明白为什么要这样处理,SE=BBAADDCC ,但是为什么直接这样给SE,结果是不正确的? 展开
Private Sub Command1_Click()
Dim BB(0 To 3) As Byte
Dim se As Single
BB(0) = BB
BB(1) = AA
BB(2) = DD
BB(3) = CC
CopyMemory se, ByVal VarPtr(BB(0)), 4
Text1.Text = se
End Sub
我不明白为什么要这样处理,SE=BBAADDCC ,但是为什么直接这样给SE,结果是不正确的? 展开
2个回答
展开全部
要如此处理:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim BB(0 To 3) As Byte
Dim se As Single
BB(0) = &HBB
BB(1) = &HAA
BB(2) = &HDD
BB(3) = &HCC
CopyMemory se, ByVal VarPtr(BB(0)), 4
Text1.Text = se
End Sub
追问
我是不明白为什么要这样弄?而且还有其他方法转换嘛?按照IEEE标准也转不出来,34 50 43 4D SE=205.2043 ,符号是 1 , 指数是160-127=32 移动32位?
追答
LZ是否仔细看过http://blog.csdn.net/zdingyun/article/details/2528026的博文?
浮点数205.2043应该是0X434D344D,转换如下:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim BB(0 To 3) As Byte
Dim se As Single
BB(0) = &H4D
BB(1) = &H34
BB(2) = &H4D
BB(3) = &H43
CopyMemory se, ByVal VarPtr(BB(0)), 4
Text1.Text = se
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询