I2C: how to read a single byte without register address?



  • Some ICs, such as PCF8574 bus expander, have not any internal registers and accessible using the device address only. So we can write or read data using device address only. If say about Python module, for writing we can use i2c.write(addr, byte) where addr is I2C slave device address. But what about reading data from device where register address is not used? How I can read single byte using Python module if device have not register address?



  • @Valeriy-Yatsenkov

    I have the same question. The current I2C library seems to have been written with a specific device in mind. Has anyone come up with a solution for this?



  • @Valeriy-Yatsenkov I'm used smbus2 library for Python. It's good work with Onion and in Python 2.7 and Python 3.x
    https://pypi.org/project/smbus2/#description
    For read and write without register address used read_byte and write_byte functions.
    By the way I used PCF8574 with this library too.



  • This post is deleted!


  • A note for anyone else that may stumble upon this topic looking for information about python and i2c.

    The link above for SMBus has some examples, here is one of them.

    ──────────────────────────────────────────
    from smbus2 import SMBusWrapper

    with SMBusWrapper(1) as bus:
    # Write a byte to address 80, offset 0
    data = 45
    bus.write_byte_data(80, 0, data)
    ──────────────────────────────────────────

    In this example two things need to be changed to make it work. Well, three really.

    1. Change SMBusWrapper(1) to SMBusWrapper(0) as the i2c bus on the omega 2 is 0 and not 1.
    2. Change the address (80 in the example) to the address of the chip you are using.
    3. Add 0x to the beginning of your address so that it is something like 0x80.

    Without these changes smbus2 will not work on the omega 2. It took me a bit to figure this out and I hope it helps others.


Log in to reply
 

Looks like your connection to Community was lost, please wait while we try to reconnect.