Starting with v0.2, the smbus2 library also has support for combined read and write transactions. write_i2c_block_data ( 80, 0, data ) I2C from smbus2 import SMBus with SMBus ( 1 ) as bus : # Write a block of 8 bytes to address 80 from offset 0 data = bus. Write less and add a delay in between if you run into trouble. It is possible to write 32 bytes at the time, but I have found that error-prone. write_byte_data ( 80, 0, data ) Example 4: Write a block of data read_i2c_block_data ( 80, 0, 16 ) # Returned value is a list of 16 bytes print ( block ) Example 3: Write a byte from smbus2 import SMBus with SMBus ( 1 ) as bus : # Write a byte to address 80, offset 0 data = 45 bus. from smbus2 import SMBus with SMBus ( 1 ) as bus : # Read a block of 16 bytes from address 80, offset 0 block = bus. ![]() read_byte_data ( 80, 0 ) print ( b ) Example 2: Read a block of data from smbus2 import SMBus with SMBus ( 1 ) as bus : bus. Same example with Packet Error Checking enabled. read_byte_data ( 80, 0 ) print ( b ) Example 1c: Read a byte with PEC enabled ![]() from smbus2 import SMBus with SMBus ( 1 ) as bus : b = bus. This is the very same example but safer to use since the smbus will be closed automatically when exiting the with block. close () Example 1b: Read a byte using 'with' read_byte_data ( 80, 0 ) print ( b ) bus. Example 1a: Read a byte from smbus2 import SMBus # Open i2c bus 1 and read one byte from address 80, offset 0 bus = SMBus ( 1 ) b = bus. Smbus2 installs next to smbus as the package, so it's not really a 100% replacement. More information about updates and general changes are recorded in the change log. It is developed on Python 2.7 but works without any modifications in Python 3.X too. i2c_rdwr - combined write/read transactions with repeated start.SMBus Packet Error Checking (PEC) support. ![]() By doing so, it will be more feature complete and easier to extend. Use the inherent i2c structs and unions to a greater extent than other pure Python implementations like pysmbus does.It should be a drop-in replacement of smbus.It was designed from the ground up with two goals in mind: Smbus2 is (yet another) pure Python implementation of the python-smbus package. A drop-in replacement for smbus-cffi/smbus-python in pure Python
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |