0

I have a piece of code that sends commands to a board using the i2ctransfer command.

This command is called inside python using subprocess.Popen and I'm rewriting the code to use some builtin python package like smbus, however I'm having difficulties implementing it so it sends the same data.

Currently the commands are generated like this:

command = ['i2ctransfer', '-y', self.i2cbus, 'w12@' + device_address] + data
rx = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
rx_buffer = rx.communicate()

And a example of the command is:

['i2ctransfer', '-y', '1', 'w12@0x03', '0x23', '0x04', '0x49', '0x74', '0x24', '0x00', '0x23', '0x05', '0x49', '0x74', '0x24', '0x00']

Basically is sending to the device with address 0x03 twelve bytes. This command updates two registers with a single instruction:

  • The two first bytes is the address: 0x2304 followed by 4 bytes of data 0x49742400

  • After that again 2 bytes of address: 0x2305 followed by 4 bytes of data: 0x49742400

I tried using write_i2c_block_data, but I'm confused about the parameters, I dont want to directly write into a register, I just want to send an array of bytes and the board itself will parse this data into register address and register value.

import smbus
bus = smbus.SMBus(1)
device_address = 0x03
bus.write_i2c_block_data(device_address, 0, [0x23, 0x04, 0x49, 0x74, 0x24, 0x00, 0x23, 0x05, 0x49, 0x74, 0x24, 0x00])

But I'm just getting a

IOError: [Errno 121] Remote I/O error

How can I just send my array of values to device 0x03 using smbus?

  • You can use 0 for the start register on write_i2c_block_data and that should get you what you're after. – meshtron Sep 05 '19 at 13:25
  • @MikahBarnett could you elaborate a bit in an answer please? –  Sep 05 '19 at 13:39
  • Sorry read it too quickly. When you say you're sending 2, 2-byte addresses (0x2304/5) in your package, are those register addresses you're expecting to write to? – meshtron Sep 05 '19 at 13:50
  • Yes, The message is constructed so it can update 2 registers in a single command by providing the addresses and the data –  Sep 05 '19 at 14:06
  • Then I think what you're after is this: `bus.write_i2c_block_data(device_address, 0x2304, [0x49, 0x74, 0x24, 0x00])` Then run again for the 2nd address. – meshtron Sep 05 '19 at 20:49

0 Answers0