2

I'm trying to implement reading from I2C communication on Python. The problem that I've faced is that the sensor which I'm reading from answers with two bytes in one transaction. Is there a Python library which provides such a reading.

Here is the description of the sensor's answer: Sensor's I2c answer

Swinders
  • 2,261
  • 4
  • 29
  • 43

1 Answers1

1

The library you're looking for is the smbus library. If you're looking to read just two bytes as you suggest, use read_word_data(int addr,char cmd). If you're using some sensor that returns more than 2 bytes, you'll have to use read_block_data(int addr,char cmd) and do some byte arithmetic.

Information on the library: http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2

samuelnj
  • 1,627
  • 1
  • 10
  • 19
  • Thank you for the answer. I have already solved the problem with using SMBusWrapper from smbus2 library. The function that I use is read_i2c_block_data(addr, offset, number_of_bytes). – Георги Няголов Jun 08 '18 at 08:58