Converting 3 bytes from i2c sensor to float #10564
-
|
Hi Here, I use this senos with i2c : https://cfsensor.com/product/piezoresistive-pressure-sensor-xgzp6847d/ I use struct packahe for convert my signed int to float, and the result is not really fitting with reality, but slightly, but when I do it manually like this : I get a tottaly différent result from ustruct method, and I don't understand why ... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
There is an inconsistency in your code. For |
Beta Was this translation helpful? Give feedback.
There is an inconsistency in your code. For
pressure_adcyou take 3 bytes from the buffer, while using ustruct() only the first two bytes are packed into thepressure_value, no matter how long the buffer is, which you supply. You could as well supply self.buf directly. Struct does not know 3 byte quantities. You could use>iwith a 4 byte buffer, where the first byte is set to 0 and the remaining three bytes are from your returned data.Edit: like
pressure_value=ustruct.unpack(">i", b'\x00' + self.buf)[0]