Private Sub MSComm1_OnComm()
Dim Inbyte() As Byte
Dim buffer As String
Dim datatemp(1000) As Single
If num > 199 Then Call renew '刷新绘图区
'读取仪表返回数据串
Select Case MSComm1.CommEvent
Case comEvReceive
Inbyte = MSComm1.Input
For i = LBound(Inbyte) To UBound(Inbyte)
buffer = buffer + Hex(Inbyte(i)) + Chr(32)
Next i
Case comEvSend
End Select
“在这里 ,我的到的buffer是 1 3 3 0 0 0 0 0 0 。1 是地址,3 是命令号。第三个3代表的小数位,后面的是压力传感器仪表的数据,仪表显示是00.000。到这里没问题,就是下面获取十进制的数据显示的不对,大家看看,是什么原因,
'获取十进制测量数据
’If Len(Trim(Mid(buffer, 1, 2))) = 1 Then
datatemp(num) = Val("&H" & Mid(buffer, 3, 2) & Str("0") & Mid(buffer, 1, 2)) * 0.001
Else
datatemp(num) = Val("&H" & Mid(buffer, 3, 2) & Mid(buffer, 1, 2)) * 0.001
End If |