write_i2c_block_data - 如何将十进制转换为i2c字节

时间:2014-02-27 18:32:07

标签: python-2.7 raspberry-pi i2c

我无法找到解决方案来实现这一目标。

我只想将x秒转换为字节并通过i2c

发送

这是我到目前为止所尝试的:

off_timer_byte = format(5, '#04x'); #convert decimal to i2c bytes
# also tried this
off_timer_byte = hex(5); #convert decimal to i2c bytes


bus.write_i2c_block_data(address, 0x00, [0x10, off_timer_byte])

变量" off_timer_byte"范围为0到256.这些是在x秒后关闭i2c设备的秒数。

我收到此错误:

pi@raspberrypi ~ $ sudo python off.py
Traceback (most recent call last):
  File "off.py", line 48, in <module>
    bus.write_i2c_block_data(address, 0x00, [0x10, off_timer_byte])
TypeError: Third argument must be a list of at least one, but not more than 32 integers

bus.write_i2c_block_data(地址,0x00,[0x10,0x2])

bus.write_i2c_block_data(地址,0x00,[0x10,0x19])

工作正常。

问题出在哪里?

到目前为止感谢! ; - )

1 个答案:

答案 0 :(得分:1)

您使用的format和hex命令将为您提供一个分别包含“0x05”或“0x5”的字符串。

您需要输出到第三个参数的是整数值。

如果您的号码低于255,您只需在列表中输入即可。否则,您将需要使用位移来将较长的数字拆分为两个字节(请注意您的从站首先将其读取为MSB或LSB)。

相关问题