pythonserial-portpyserial

pySerial read_until() reads only the first byte


So I'm receiving data from a serial port using pySerial. I've a very simple code that reads the first byte, check if it's the start byte ( 0x02 in my case) and then read until it finds the end byte ( 0x03 in my case).

Config the serial communciation

port = 'COM3'
baud = 38400

ser = serial.Serial(port, baud, timeout=0)
if ser.isOpen():
    ser.close()
    ser.open()

ser.reset_input_buffer()
ser.reset_output_buffer()

The main loop is inside a while True staement as below.

while True:
    data = ""
    data_raw = ser.read(1)
    if data_raw == b'\x02':
        data_raw = ser.read_until(b'\x03')
        print(str(data_raw))
        ser.reset_input_buffer()
        ser.reset_output_buffer()
        time.sleep(.5)

The issue is that, for some reason, the read_until() actually reads only the first bye while the data I'm receiving from the serial port are actually b'\x02\xff\x9c\x81E1\x03\'

After reading correctly the \x02 the read_until() statement just read only the next \xff and I can't understand why


Solution

  • It seems a bug never fixed by the pySerial module itself https://github.com/pyserial/pyserial/issues/181

    Using timeout=None in serial.Serial() resolved the issue