Frage

Is there a Python function that will respond like the Wire.available function in arduino to get all the data on the wire rather than having to specify how many bytes to grab?

This is what I have now, and it works fine, but I have to know how much data is coming down the wire, or it will provide unexpected results.

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

Thanks!

War es hilfreich?

Lösung

Not a perfect solution, but I found a way around knowing exactly how many bytes are on the way.

On the Arduino, I specified the max size of the buffer, (128), add my data, then zero out the rest, and then send the whole thing. On the Pi, I receive the whole buffer, and then the first thing that happens is to filter the \x00 characters. It's not perfect, but it works for now.

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.

Andere Tipps

I use the PIGPIO library for i2c commands on the raspberrypi, it has functions much closer to wire.

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

I think that's the function you're looking for.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top