0

我有一段代码使用该i2ctransfer命令将命令发送到板。

这个命令在 python 中调用subprocess.Popen,我正在重写代码以使用一些内置的 python 包,比如 smbus,但是我在实现它时遇到了困难,所以它发送相同的数据。

目前,命令生成如下:

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

该命令的一个示例是:

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

基本上是向地址为 0x03 的设备发送 12 个字节。此命令使用一条指令更新两个寄存器:

  • 前两个字节是地址:0x2304 后面是 4 个字节的数据 0x49742400

  • 之后又是 2 个字节的地址:0x2305,然后是 4 个字节的数据:0x49742400

我尝试使用 write_i2c_block_data,但我对参数感到困惑,我不想直接写入寄存器,我只想发送一个字节数组,板子本身会将这些数据解析为寄存器地址和寄存器值。

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])

但我只是得到一个

IOError: [Errno 121] Remote I/O error

如何使用 smbus 将值数组发送到设备 0x03?

4

0 回答 0