kplindegaard / smbus2

A drop-in replacement for smbus-cffi/smbus-python in pure Python
MIT License
243 stars 68 forks source link

Gracefully handling trackbacks? #22

Closed AlexKlimaj closed 6 years ago

AlexKlimaj commented 6 years ago

Is there a way to gracefully handle this in a program?

This happens very rarely in my code, but when it does, it exits a long running test.

Traceback (most recent call last):
  File "battery_test.py", line 89, in <module>
    remaining = bus.read_byte_data(0x0b, 0x0d)
  File "/usr/local/lib/python3.7/site-packages/smbus2/smbus2.py", line 355, in read_byte_data
    ioctl(self.fd, I2C_SMBUS, msg)
OSError: [Errno 121] Remote I/O error
AlexKlimaj commented 6 years ago

Solved by making higher level functions with error handling.

def battery_block_read(address, register, numberofbytes): tmp = None while (tmp == None): with SMBusWrapper(1) as bus: try: tmp = bus.read_i2c_block_data(address, register, numberofbytes) if (tmp != None): return tmp; except: print("SMBUS read block error") return;

kplindegaard commented 6 years ago

Yep, you’ll need to add error handling yourself. The philosophy is to keep the library as small as possible :)