0
votes

got a serial reading problem that drives me nuts. Current setup is an Arduino connected to a RaspberryPi (standard USB connection). Trying to read from serial info that Arduino sends via serial to Raspberry on /dev/ttyACM0

When trying a pretty basic monitoring, raw read:

$ (stty raw; cat) < /dev/ttyACM0
RFBee ready to receive!

: 03  Packet: 80  0F  EA  1D  D9  00  F3  15  FF  FF  RSSI: -60  LQI: 30  Error: 7  Next: 64  Packets: 4
RFBee ready to receive!


Index: 00  Packet: E0  0E  F2  C6  01  02  B1  91  FF  FF  RSSI: -60  LQI: 2F  Error: 7  Next: 64  Packets: 1
Index: 01  Packet: 50  0D  E7  FF  73  04  56  C4  FF  FF  RSSI: -60  LQI: 2F  Error: 7  Next: 64  Packets: 2
Index: 02  Packet: 80  0D  F6  1D  D8  0C  11  BE  FF  FF  RSSI: -61  LQI: 2F  Error: 7  Next: 64  Packets: 3
Index: 03  Packet: 20  0D  E7  D4  C1  86  AC  CA  FF  FF  RSSI: -61  LQI: 2E  Error: 7  Next: 64  Packets: 4
Index: 04  Packet: E0  0D  EF  C6  03  0C  FA  CE  FF  FF  RSSI: -61  LQI: 2F  Error: 7  Next: 64  Packets: 5
Index: 00  Packet: 50  0B  DF  FF  71  06  74  4B  FF  FF  RSSI: -60  LQI: 2E  Error: 0  Next: 64  Packets: 6
Index: 01  Packet: 80  0B  EA  1D  D8  06  29  E4  FF  FF  RSSI: -60  LQI: 2E  Error: 0  Next: 64  Packets: 7
Index: 02  Packet: 70  0A  F8  AC  03  8C  D4  16  FF  FF  RSSI: -61  LQI: 2E  Error: 0  Next: 64  Packets: 8

It all seems to be working more or less fine (except for the double RFBee ready to receive line), but then catches on and prints data line by line. Minicom works the same way, reads data, all fine.

With PySerial, using this code:

    #!/usr/bin/python -u
import serial

while True:
    with serial.Serial('/dev/ttyACM0', 115200) as ser:
            line = ser.readline()
            if line != '':
                print(line)

It just prints out these lines:

$ python davis_collect.py 
B  DF  CA  FF  FF  RSSI: -60  LQI: 2E  Error: 0  Next: 64  Packets: 9

RFBee ready to receive!

RFBee ready to receive!

RFBee ready to receive!

RFBee ready to receive!

RFBee ready to receive!

So it catches whatever part of the buffer that it should at first, but then, for some reason, cannot stop printing that beautiful line.

To add on top, some basic information: Tried this on my notebook, PySerial worked. The serial communication appareently works via RasPi as well, as it is readable via standard utilities.

Python3 output:

python3 davis_collect.py 
b'RFBee ready to receive!\r\n'
b'RFBee ready to receive!\r\n'
b'RFBee ready to receive!\r\n'
b'RFBee ready to receive!\r\n'
b'RFBee ready to receive!\r\n'

Didn't find anything similar so far. Any ideas, links? (First post here, if I need to add something, please, you, the more experienced guys, leave suggestions)

1
Why are you connecting to the serial on every loop? – gre_gor
Heh, good question :-) - now why does that work on all possible platforms? I thought well withing control flow and stop bits, but somehow missed this. I will try this in 48 hours when I get back to my setup. Thanks for pointing this out. – Ventil

1 Answers

0
votes

I am officially a moron, as gre_gor (thanks for the kick in the head) noted above, the loop was called and connected each time to the serial port anew.

This does not pose a problem on most of the machines that you normally come across, but because of default settings on the RasPi - buffer handling - the first line was read in a loop after each connect.

It is enough to say, that using (note the endless loop AFTER I connect):

#!/usr/bin/python -u
import serial

with serial.Serial('/dev/ttyACM0', 115200) as ser:
      while True:
            line = ser.readline()
            if line != '':
                print(line)

instead, produced expected results. And I jsut add: Doh! - oviously!