我正在从热量表接收UART上的数据,但是我需要一些帮助来理解我应该如何处理这些数据。我有文件,但这对我来说还不够,我几乎没有这种计算的经验。
也许有合适技能的人可以向我解释如何用我从文档中得到的更好的例子来完成这件事。
One value consists of the following bytes:
[number of bytes][sign+exponent] (integer)
(integer) is the register data value. The length of the integer value is
specified by [number of bytes]. [sign+exponent] is an 8-bit value that
specifies the sign of the data value and sign and value of the exponent. The
meaning of the individual bits in the [sign+exponent] byte is shown below:

Examples:
-123.45 = 04h, C2h, 0h, 0h, 30h, 39h
87654321*103 = 04h, 03h , 05h, 39h, 7Fh, B1h
255*103 = 01h, 03h , FFh 现在再举一个实际数据的例子。

这是我从有关这方面的文档中获得的信息。
这是我从热量表中得到的一些数据。
10 00 56 25 04 42 00 00 1B E4
所以在我的例子中,04是字节数,42是sign+exponent,00 1B E4是(整数)。但我不知道该如何计算才能得到实际价值。
有什么帮助吗?
发布于 2017-07-18 22:29:20
根据你的例子,你的数据似乎是大端的.下面是如何将这些字节分解为需要使用位移位和掩蔽的字段。
n = b[0]
SI = (b[1] & 0x80) >> 7
SE = (b[1] & 0x40) >> 6
exponent = b[1] & 0x3f
integer = 0
for i = 0 to n-1:
integer = (integer << 8) + b[2+i]发布于 2017-07-18 22:25:04
尾数的符号是通过掩蔽( MSb,byte & 80h != 0 => SI = -1)从Sign+exponent字节的byte & 80h != 0 => SI = -1中获得的。
指数的符号也是用byte & 40h != 0 => SE = -1得到的。
指数值为EXP = byte & 3Fh。
尾数INT是由其他四个字节组成的二进制数,可以作为一个整数读取(但要注意印度)。
最后,计算SI * INT * pow(10, SE * EXP)。
在您的示例中,SI = 1、SE = -1、EXP = 2、INT = 7140,因此
1 * 7140 * pow(10, -1 * 2) = +71.4解释如何有效地实现这一点,不在本答案的范围之内。
https://stackoverflow.com/questions/45177995
复制相似问题