I am new to Python.
The code below writes a byte to an I2C EEPROM and then reads it back (at least this is the intent).
from smbus2 import SMBus, i2c_msg channel = 1 # select I2C-1 address = 0x50 register = 10 datawr = 0x55 datard = 0 flag = False bus = SMBus(channel) #select bus try: b=bus.read_byte(address) flag = True except IOError: print("Address not valid: 0x%2X" % (address)) if(flag): print("Valid Address: 0x%2X" % (address)) bus.write_byte_data(address, register, datawr) write = i2c_msg.write(address, register) read = i2c_msg.read(address, 1) bus.i2c_rdwr(write, read)
I get an error when the code reaches the I2C_MSG.WRITE functions; the error messages is the following:
pi@raspberrypi:~/oim-work $ python i2c-eeprom.py
Valid Address: 0x50
Traceback (most recent call last):
File “i2c-eeprom.py”, line 21, in
write = i2c_msg.write(address, register)
File “/home/pi/.local/lib/python2.7/site-packages/smbus2/smbus2.py”, line 225, in write
buf = ‘’.join([chr(x) for x in buf])
TypeError: ‘int’ object is not iterable
I am not sure how to define the values of ADDRESS and/or REGISTER to fix the error.