I see a couple of issues.

First:

ser.read() is only going to return 1 byte at a time.

If you specify a count

ser.read(5)

it will read 5 bytes (less if timeout occurrs before 5 bytes arrive.)

If you know that your input is always properly terminated with EOL characters, better way is to use

ser.readline()

That will continue to read characters until an EOL is received.

Second:

Even if you get ser.read() or ser.readline() to return multiple bytes, since you are iterating over the return value, you will still be handling it one byte at a time.

Get rid of the

for line in ser.read():

and just say:

line = ser.readline()
Answer from jwygralak67 on Stack Overflow
🌐
pySerial
pyserial.readthedocs.io › en › latest › pyserial_api.html
pySerial API — pySerial 3.5 documentation
Read size bytes from the serial port. If a timeout is set it may return fewer characters than requested. With no timeout it will block until the requested number of bytes is read. Changed in version 2.5: Returns an instance of bytes when available (Python 2.6 and newer) and str otherwise.
Discussions

Python Serial: How to use the read or readline function to read more than 1 character at a time - Stack Overflow
Serial sends data 8 bits at a time, that translates to 1 byte and 1 byte means 1 character. You need to implement your own method that can read characters into a buffer until some sentinel is reached. More on stackoverflow.com
🌐 stackoverflow.com
Serial read with python
Hi, Firstly I’m new with python. I’m trying to read values on the serial line to plot a graph. In my serial monitor putty everything is in the right way printed on the serial line. But when I’m trying to read it with python it adds an character and because of this the graph can not be plotted. More on discuss.python.org
🌐 discuss.python.org
12
0
June 29, 2022
python - Serial.readString() - how does it work exactly? - Arduino Stack Exchange
It is more appropriate to use readStringUntil() which will read characters from the serial device until either it times out, or it receives a certain character. It is most common to use either the line feed (\n) or carriage return (\r) characters as the end of string marker. More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
September 24, 2015
arduino - Pyserial doesn't read entire line - Electrical Engineering Stack Exchange
Every line is terminated by a new line character. But when I try to read this data into Python using pySerial, it doesn't read the entire line. PySerial starts to read lines from the middle, sometimes giving weird values. But when I open the Arduino serial monitor, all the data is displayed ... More on electronics.stackexchange.com
🌐 electronics.stackexchange.com
🌐
pySerial
pyserial.readthedocs.io › en › latest › shortintro.html
Short introduction — pySerial 3.5 documentation
line = ser.readline() # read a '\n' terminated line · Open port at “38400,8,E,1”, non blocking HW handshaking: >>> ser = serial.Serial('COM3', 38400, timeout=0, ... parity=serial.PARITY_EVEN, rtscts=1) >>> s = ser.read(100) # read up to one hundred bytes ...
🌐
Inductive Automation
docs.inductiveautomation.com › appendix › system functions › system.serial › readuntil
system.serial.readUntil | Ignition User Manual
September 25, 2024 - system.serial.readUntil(port, delimiter, includeDelimiter, [timeout]) String - Returns a String containing all 8-bit ASCII characters read until the delimiter was reached, and including the delimiter if the "includeDelimiter" parameter was true.
🌐
Python.org
discuss.python.org › python help
Serial read with python - Python Help - Discussions on Python.org
June 29, 2022 - Hi, Firstly I’m new with python. I’m trying to read values on the serial line to plot a graph. In my serial monitor putty everything is in the right way printed on the serial line. But when I’m trying to read it with python it adds an character and because of this the graph can not be plotted.
🌐
SourceForge
sourceforge.net › home › browse › python serial port extension › feature requests
Python Serial Port Extension / Feature Requests / #36 Add serial.Serial.readUntil() to read until there is a packet termination character
February 12, 2014 - ser = serial.Serial(1, 38400, timeout=0, parity=serial.PARITY_EVEN, rtscts=1) s = ser.readUntil(0, max_chars=254) which would read up to 254 characters or until there is a 0 byte, whichever comes first.
Find elsewhere
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
reading serial port - specify start char and end char - Raspberry Pi Forums
ser.readlines() define start and end character to read.. This is my output: ['\r\n', 'OK\r\n', '\r\n', 'OK\r\n', '\r\n'] or: ['\r\n', 'ERROR\r\n', '\r\n', 'OK\r\n', '\r\n'] I want only "OK" or "ERROR" as a output.. is possible to cut that somehow? (I want use it with if statement..
Top answer
1 of 1
3

PySerial (and serial libraries in general) have no idea of the concept of a "line".

When you port.read(), you get what is in the serial buffer right then, so if you opened the serial port when the arduino was partway through sending a serial message, you're going to get just the latter half of that message. If you call read() when the arduino has only sent half of the message, you're going to get the first half of the message.

The solution here is to read until you see the new-line character. Since you know that each message is ASCII text followed by a newline, you can use the position of newline characters to divide the input stream.

Conveniently, pySerial has calls for this already: port.readline().

There are things to be aware of when using readline(), though. From the PySerial documentation (which you should be reading):

Be carefully when using readline(). Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received. Also note that readlines() only works with a timeout. readlines() depends on having a timeout and interprets that as EOF (end of file). It raises an exception if the port is not opened correctly.


Serial is a stream interface, not a message interface. It has no concept of any unit of data larger then a byte. As such, it is implicit on the user (in this case, you) to implement something that provides message delimiters. With ASCII, this is fairly easy since you aren't using every possible value that each byte can hold, but for systems that use binary messaging protocols, it can be quite involved.

Further note: You really should have asked this question on https://arduino.stackexchange.com/

🌐
Reddit
reddit.com › r/learnpython › issues reading from serial port using read_until()
r/learnpython on Reddit: Issues reading from serial port using read_until()
December 17, 2020 -

First time posting here, I think I posted the "code" part correctly but didn't know about the output part.

I created a class to help me read and write into an instrument using serial port. However, recently I found an issue that I think it's due to sending multiple write commands and I only need to read when I query the instrument. When I send regular write commands, the instrument outputs text indicating the changes made as shown below:

    self.id.write(str.encode("send write command\r"))
    time.sleep(1)
    self.id.write(str.encode("get status\r"))
    output=self.id.read_until(str.encode(">>\r"))

When sending the write command, it will output a status as:

>>write command:

Configured state to: 0A

>>

>>get status

status is configured to: BB

What I think it's happening is that when I use the read_until() , it will start reading from the "send write" command instead of the "get status" because when I look at the text captured it corresponds to the "Configured state to: 0A" line.

My question is: How do I get the code to read only from the time I send the "get status" command? I have thought about reading the line when I send the "write command" and not doing anything with it, but I think there might be another option.

🌐
Microsoft MakeCode
makecode.microbit.org › reference › serial › read-until
read Until
Read a text from the serial port until a delimiter is found.
🌐
GitHub
github.com › pyserial › pyserial › issues › 181
read_until: only works when no timeout is set · Issue #181 · pyserial/pyserial
November 18, 2016 - Working on a raspberry pi 1 the following line does not work as soon as I set a timeout: callback = ser.read_until(terminator, 1000), where terminator is set to b'\x03' as long as ser.timeout is set to None, my script reads all messages received over the serial port, but as soon as ser.timeout is set to anything other than None callback never contains more than 8 bytes.
Author   pyserial
🌐
Python Forum
python-forum.io › thread-10133.html
pyserial char by char io
pyserial is a nifty little module. I'm using it to communicate with 2 serial ports on my ARM eveluation board. It even allows me to send DSR which is essential for me. However I want to receive from serial port character by character, not wait until...
🌐
Python
mail.python.org › pipermail › tutor › 2001-April › 005157.html
[Tutor] Parsing control characters from serial port
April 26, 2001 - Do I need to somehow tell > readlines() ... machine and lunch is short!) You can read character by character using read(). If there's nothing on the serial line atm (EOF), it returns '', but you still have to keep reading until the rest is there, until the 0x03....
🌐
Delft Stack
delftstack.com › home › howto › python › pyserial readline
Python Pyserial Readline | Delft Stack
March 11, 2025 - In this example, we again create a Serial object and specify the port and baud rate. The readline() function reads data from the serial port until it encounters a newline character.
🌐
Arduino Forum
forum.arduino.cc › projects › interfacing w/ software on the computer
Force PySerial to wait for next character after newline before begin read? - Interfacing w/ Software on the Computer - Arduino Forum
May 4, 2017 - Is there any way to force PySerial to wait for the next character after a /n newline character before beginning to read data from an Arduino sensor? My Arduino sketch is set to write a string of 200 comma separated valu…
🌐
Aposteriori
tutorials.aposteriori.com.sg › 40-Python-Specialized-Topics › 30-Others › 10-Serial.html
Python Specialized Topics
This is a blocking function, which means that the next line of code will not run until serial receives the newline character. print(data.decode(), end='') : This prints the data that was received. Note that the data is a bytes object, and not a string. To print it properly, we'll need to decode it into a string first. The end='' indicates to the print function that it should not add a newline to the end when printing (...as the serial data already contains a newline).
🌐
writed0wnload
writed0wnload.weebly.com › python-serial-read-example.html
Python Serial Read Example - writed0wnload
That will continue to read characters until an EOL is received. ... Even if you get ser.read() or ser.readline() to return multiple bytes,since you are iterating over the return value, you willstill be handling it one byte at a time. ... Serial sends data 8 bits at a time, that translates to 1 byte and 1 byte means 1 character.