I have a python program that reads data from a USB serial port connected to a computer. The computer sends data packets from a simulation program (I didn't write this).
The data is made of strings of no more than 200 bytes serially with 21 data fields separated by commas at a baud rate of 115200 with 8N1.
In the python program, I have:
ser = serial.Serial(port=PORTNAME, baudrate=115200, timeout=.02) while True: # OTHER STUFF IS IN THE LOOP AS WELL THAT DOESN'T HAVE TO DO WITH THE SERIAL time.sleep(0.01) data = ser.read(200)
Most of the time, the
data will be exactly 21 fields, but sometimes the
data variable will be a string that has one entire data packet as well as the beginning of the next one appended to it, and the function that parses each individual data field will obviously return an exception.
Something I tried just for fun is adding a lot of print statements in the loop, which made the exception in the program happen significantly faster. I think it might have to do with the time.
Does anyone know how I can fix this? If not, I can just hard code a hack in the function that parses the