问题
I have difficulty in converting two bytes in temperature. I have a control unit (temperature sensor) where I get the message of temperature with two bytes.
1 ) Example:
message: [ 40 ][ 25 ]
LSBYTE : [ 40 ]
MSBYTE : [ 25 ]
0.03125 C/bit
temperature: 25C° ( seen from the display of the control unit )
2 ) Example:
message: [ 40 ][ 26 ]
LSBYTE : [ 40 ]
MSBYTE : [ 26 ]
0.03125 C/bit
temperature: 30C° ( seen from the display of the control unit )
3 ) Example:
message: [ 20 ][ 26 ]
LSBYTE : [ 20 ]
MSBYTE : [ 26 ]
0.03125 C/bit
temperature: 32C° ( seen from the display of the control unit )
4 ) Example:
message: [ c0 ][ 25 ]
LSBYTE : [ c0 ]
MSBYTE : [ 26 ]
0.03125 C/bit
temperature: 29C° ( seen from the display of the control unit )
I do not know how to convert the message in temperature.
I ask for your support. who can give me a solution, it can collaborate in my project ( create a app android to receive messages from the control unit )
回答1:
This is simply Little endian byte order. Your most important byte would be the MSB. It looks like it's natively in celsius. So simply read it like this (please excuse the pseudo-code):
var msbyte = read() // whatever you need to get the value
var lsbyte = read() // whatever you need to get the value
var temperature = msbyte
temperature += (lsbyte / 100)
In your case its simply 25.40C°
回答2:
It appears to be giving you the whole temp in the upper byte, and the fraction in the lower byte. That would be 25.15625 (25 + 40/256).
I don't know where the 0.03125 C/bit comes from. That would mean 16bits = 0.5C. Seems like nonsense.
the other possible explanation is that .03125 * total = degrees C. If MSB is 25 and LSB is 40, the total is 201.25 degrees. so...probably not right.
EDIT: 0.03125 = 1/32
none of your points use the last 5 bits. that is fractional component. Take upper byte, lower byte, and shift right 5 bits then subtract 273 (for kelvin to C conversion).
回答3:
the solution is : if i have a example message: [20][26] and the temperature is 32C° the fomula is : 2620 in decimal is 9760 (9760 * 0.03125)-273.15= 31.85 = 32C°
来源:https://stackoverflow.com/questions/25739937/temperature-conversion-2-byte