Search code examples
pythonserial-portembedded-linuxpyserial

Why does my pySerial data get corrupted mid-stream?


This works wonderfully on Windows, with Python 3.10. Incoming data has a delimiter, this delivers one message at a time, life is good...until I test on Linux. Specifically Ubuntu 18.04.

On Linux, it receives a handful of messages successfully, and then it gets garbage, non-ascii values. Oddly it often turns to garbage right after the delimeter, which seems like it is a hint, but I am not sure how. I have tried changing the decode to 'utf-8', 'utf-16', and a variety of other things but observe the same behavior.

import serial, time
    incoming= ''
    delim = 'xyz'
    ser = serial.Serial(port=port, baudrate=baud, timeout=timeout)
    while True:
        while ser.in_waiting > 0:
            incoming += ser.read(ser.in_waiting).decode('ascii')
            while delim in incoming:
                deliver, incoming = incoming.split(delim, 1)
                if deliver:
                    print(deliver)
        time.sleep(0.1)

Any suggestions on what the issue could be?


Solution

  • stty -F /dev/ttyUSBx

    This showed the serial settings, which identified that something is changing the BAUD rate. When I printed ser.baudrate the Python indicates there is no change, but the above command showed the actual state of hardware and the problem is.

    Now I need to figure out a way for the BAUD to not change...