Python serial reading not working as expected

Thread Starter

Gibson486

Joined Jul 20, 2012
355
I am trying to read serial with Python. If I do readline, it always includes the /r/n. To get rid of it, I just do read and I do In_waiting to figure out the number of bytes I have waiting. However, in_waiting always says there are more bytes than are really in there, so I just get garbage after my first few bytes.

python snippet:
    numbytes = ser.in_waiting
    print(numbytes)
    numbytes = numbytes - 4
    #result = ser.read_until('\r\n')
    #result = ser.readline()
    result = ser.read(numbytes)
    print(result)
Maybe I am thinking about this the wrong way?
 

Ya’akov

Joined Jan 27, 2019
9,165
I am trying to read serial with Python. If I do readline, it always includes the /r/n. To get rid of it, I just do read and I do In_waiting to figure out the number of bytes I have waiting. However, in_waiting always says there are more bytes than are really in there, so I just get garbage after my first few bytes.

python snippet:
    numbytes = ser.in_waiting
    print(numbytes)
    numbytes = numbytes - 4
    #result = ser.read_until('\r\n')
    #result = ser.readline()
    result = ser.read(numbytes)
    print(result)
Maybe I am thinking about this the wrong way?
Maybe you want ser.readline() and rstrip().
 
Top