是否有一个Python函数会像Arduino中的Wire.available函数那样响应以获取线路上的所有数据,而不必指定要占用多少字节?Raspberrypi Python bus.read_byte
这就是我现在所拥有的,它工作正常,但我必须知道有多少数据传到电线上,否则会提供意想不到的结果。
for i in range(0, 13):
data += chr(bus.read_byte(address));
谢谢!
是否有一个Python函数会像Arduino中的Wire.available函数那样响应以获取线路上的所有数据,而不必指定要占用多少字节?Raspberrypi Python bus.read_byte
这就是我现在所拥有的,它工作正常,但我必须知道有多少数据传到电线上,否则会提供意想不到的结果。
for i in range(0, 13):
data += chr(bus.read_byte(address));
谢谢!
不是一个完美的解决方案,但我找到了一种方法,确切知道有多少字节在路上。
在Arduino上,我指定了缓冲区的最大尺寸(128),添加我的数据,然后将剩下的数据清零,然后发送整个事物。在Pi上,我收到整个缓冲区,然后发生的第一件事是过滤\x00
字符。这不是完美的,但它现在起作用。
for i in range(0, 128):
data += chr(bus.read_byte(address))
print repr(data)
#prints the whole string as it is received
data = filter(lambda a: a != '\x00')
print repr(data)
#prints the string without any '\x00' characters.
我在raspberrypi上使用PIGPIO库作为i2c命令,它的功能更接近导线。
http://abyz.co.uk/rpi/pigpio/python.html#i2c_read_device
我想这就是你要找的功能。