Package: smbus2
smbus2A drop-in replacement for smbus-cffi/smbus-python in pure PythonIntroductionsmbus2 is (yet another) pure Python implementation of thepython-smbuspackage.It was designed from the ground up with two goals in mind:It should be a drop-in replacement of smbus. The syntax shall be the same.Use the inherent i2c structs and unions to a greater extent than other pure Python implementations likepysmbusdoes. By doing so, it will be more feature complete and easier to extend.Currently supported features are:Get i2c capabilities (I2C_FUNCS)SMBus Packet Error Checking (PEC) supportread_bytewrite_byteread_byte_datawrite_byte_dataread_word_datawrite_word_dataread_i2c_block_datawrite_i2c_block_datawrite_quickprocess_callread_block_datawrite_block_datablock_process_calli2c_rdwr -combined write/read transactions with repeated startIt is developed on Python 2.7 but works without any modifications in Python 3.X too.More information about updates and general changes are recorded in thechange log.SMBus code examplessmbus2 installs next to smbus as the package, so it's not really a 100% replacement. You must change the module name.Example 1a: Read a bytefromsmbus2importSMBus# Open i2c bus 1 and read one byte from address 80, offset 0bus=SMBus(1)b=bus.read_byte_data(80,0)print(b)bus.close()Example 1b: Read a byte using 'with'This is the very same example but safer to use since the smbus will be closed automatically when exiting the with block.fromsmbus2importSMBuswithSMBus(1)asbus:b=bus.read_byte_data(80,0)print(b)Example 1c: Read a byte with PEC enabledSame example with Packet Error Checking enabled.fromsmbus2importSMBuswithSMBus(1)asbus:bus.pec=1# Enable PECb=bus.read_byte_data(80,0)print(b)Example 2: Read a block of dataYou can read up to 32 bytes at once.fromsmbus2importSMBuswithSMBus(1)asbus:# Read a block of 16 bytes from address 80, offset 0block=bus.read_i2c_block_data(80,0,16)# Returned value is a list of 16 bytesprint(block)Example 3: Write a bytefromsmbus2importSMBuswithSMBus(1)asbus:# Write a byte to address 80, offset 0data=45bus.write_byte_data(80,0,data)Example 4: Write a block of dataIt is possible to write 32 bytes at the time, but I have found that error-prone. Write less and add a delay in between if you run into trouble.fromsmbus2importSMBuswithSMBus(1)asbus:# Write a block of 8 bytes to address 80 from offset 0data=[1,2,3,4,5,6,7,8]bus.write_i2c_block_data(80,0,data)I2CStarting with v0.2, the smbus2 library also has support for combined read and write transactions.i2c_rdwris not really a SMBus feature but comes in handy when the master needs to:read or write bulks of data larger than SMBus' 32 bytes limit.write some data and then read from the slave with a repeated start and no stop bit between.Each operation is represented by ai2c_msgmessage object.Example 5: Single i2c_rdwrfromsmbus2importSMBus,i2c_msgwithSMBus(1)asbus:# Read 64 bytes from address 80msg=i2c_msg.read(80,64)bus.i2c_rdwr(msg)# Write a single byte to address 80msg=i2c_msg.write(80,[65])bus.i2c_rdwr(msg)# Write some bytes to address 80msg=i2c_msg.write(80,[65,66,67,68])bus.i2c_rdwr(msg)Example 6: Dual i2c_rdwrTo perform dual operations just add more i2c_msg instances to the bus call:fromsmbus2importSMBus,i2c_msg# Single transaction writing two bytes then read two at address 80write=i2c_msg.write(80,[40,50])read=i2c_msg.read(80,2)withSMBus(1)asbus:bus.i2c_rdwr(write,read)Example 7: Access i2c_msg dataAll data is contained in the i2c_msg instances. Here are some data access alternatives.# 1: Convert message content to listmsg=i2c_msg.write(60,[1,2,3,4,5,6,7,8,9,10])data=list(msg)# data = [1, 2, 3, ...]print(len(data))# => 10# 2: i2c_msg is iterableforvalueinmsg:print(value)# 3: Through i2c_msg propertiesforkinrange(msg.len):print(msg.buf[k])Installation instructionsFromPyPiwithpip:pip install smbus2Fromconda-forgeusingconda:conda install -c conda-forge smbus2Installation from source code is straight forward:python setup.py install