I am streaming data from an arduino back to a raspberry pi through the /dev/ttyUSB0 port. I am using python on the pi side for reading the incoming serial data. For debug purposes I am currently printing out each serial.readLine(). Two lines of debug can be seen below:
['A', '337', '731', '88\r\n']
['A', '33\x009', '1931', '8\r\n']
Every once in a while a random \x00 appears in the data and I can't figure out where it is coming from. I haven't streamlined the serial communication from the arduino so I believe it is sending each digit as a separate byte like 3 3 \x00 9 , so that can obviously be improved.
Also this only seems to happen when I run the serial communication in a seperate thread in python... I'm not too familiar with multithreading in python but this seemed too repeatable to just be coincidental.
Some of the relevent code Arduino:
Serial2.print(F("A,"));
Serial2.print(startingAngle + ix);
Serial2.print(F(","));
Serial2.print(int(aryDist[ix]));
Serial2.print(F(","));
Serial2.println(aryQuality[ix]);
Python:
queue = Queue()
lidar_thread = Thread( target=lidar.process, args=(queue, ))
lidar_thread.start()
In the thread function:
with serial.Serial('/dev/ttyUSB0', 115200, timeout=1) as ser:
ser.write(b'ShowDist\n')
while True:
s = ser.readline()
vals = s.split(',')
Again, if I just call the function outside of a thread it seems to work fine. Any ideas on where this '\x00' comes from and why it seems to only happen when multi threaded? In the end I want to cast each value as an int which breaks when that extra byte gets included... I would also be interested in a computationally inexpensive way of checking if the byte is okay before parsing... from what I have read the try catch approach can slow things down.
Thanks