Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1584

Python • i2c device read fails in python, CLI i2cget works

$
0
0
I have a DAC device with I2C address 0x48 and I want to read and write to register 0x20.
i2cget 1 0x48 0x20 returns:
i2cget 1 0x48 0x20
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will read from device file /dev/i2c-1, chip address 0x48, data address
0x20, using read byte data.
Continue? [Y/n]
0x44
I2cdump also works and displays the correct value.

Writing to this register in a python script works, and the DAC respond as expected.
Write success: Register 0x20 = 85
This is the script that works:

Code:

import sysimport smbusimport timechannel = 1address = 0x48bus = smbus.SMBus(channel)try:    reg = 0x20  # replace with a writeably register from docs.    val = 85    # replace with the desired value to write.    bus.write_byte_data(address, reg, 100 - val)    print(f"Write success: Register 0x{reg:02X} = {val}")except OSError as e:    print(f"Write failed: {e}")
Reading from this register in a python script fails.
Read failed: [Errno 5] Input/output error
It also makes the device address disappear from the i2c bus, needs a power restart to reappear.
This is the script that fails:

Code:

import smbusimport timechannel = 1address = 0x48bus = smbus.SMBus(channel)try:    reg = 0x20  # replace with a read-only register from docs    val = bus.read_byte_data(address, reg)    print(f"Read success: Register 0x{reg:02X} = {val}") except OSError as e:    print(f"Read failed: {e}")
Can someone explain this and suggest a working solution?

Statistics: Posted by bnilsson — Thu May 15, 2025 9:45 am



Viewing all articles
Browse latest Browse all 1584

Trending Articles