【问题标题】:Raspberrypi Python bus.read_byte树莓派 Python bus.write_byte
【发布时间】:2013-04-20 09:09:36
【问题描述】:

是否有一个 Python 函数会像 arduino 中的 Wire.available 函数一样响应以获取网络上的所有数据,而不必指定要抓取多少字节?

这就是我现在所拥有的,它工作正常,但我必须知道有多少数据通过网络传输,否则会产生意想不到的结果。

for i in range(0, 13):
 data += chr(bus.read_byte(address));

谢谢!

【问题讨论】:

    标签: python raspberry-pi i2c


    【解决方案1】:

    不是一个完美的解决方案,但我找到了一种方法来确切地知道有多少字节在路上。

    在 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.
    

    【讨论】:

      【解决方案2】:

      我将 PIGPIO 库用于 raspberrypi 上的 i2c 命令,它的功能更接近有线。

      http://abyz.co.uk/rpi/pigpio/python.html#i2c_read_device

      我认为这就是您正在寻找的功能。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-14
        • 2014-09-20
        • 1970-01-01
        • 2019-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多