Description
I am using this library with RPi3 but encounter runtime errors when reading the temperature or reference temperature. The temperature functions use "unpack" when returning the results of register reads. The read_register function returns 3 bytes and although the comments say the unpack will adjust it to the right size I get a runtime error instead saying 4 bytes are expected.
A bit of diagnostic work showed that the bytearray was there and contained 3 bytes. I don't understand the unpack syntax to the point where it should be adding a byte :
raw_temp = unpack(">i", self._read_register(_MAX31856_LTCBH_REG, 3))[0]
There just seems to be a null byte declaration stuck on the end with no link symbol such as '+' as you might expect. It doesn't throw a syntax error but also doesn't seem to work.
Researching similar unpack errors I came across "int.from_bytes()" as a newer Python feature. I gave it a try instead of the "unpack" code and it worked.
`def temperature(self):
self._perform_one_shot_measurement()
# unpack the 3-byte temperature as 4 bytes
# raw_temp = unpack(">i", self._read_register(_MAX31856_LTCBH_REG, 3))[0]
raw_temp = int.from_bytes(self._read_register(_MAX31856_LTCBH_REG, 3),'big')
# shift to remove extra byte from unpack needing 4 bytes
# raw_temp >>= 8
# effectively shift raw_read >> 12 to convert pseudo-float
temp_float = (raw_temp / 4096.0)
return temp_float`
This gave me what look like accurate temperatures, pending further calibration.
Any idea why the original code failed for me? It seems the code isn't as portable as it could be?