Elisabeth Strunk
Elisabeth Strunk

Reputation: 566

Multiline serial read in Python 3

I am playing around with bidirectional serial communication between my PC and a STM32 development board. I am using Python 3.7 with PySerial 3.4 to open the serial port and receive/transceive messages from/to the dev board. Everything is working as expected except when I try to read and print multiline messages. In this case I only get the first line of the message.

The program on the microcontroller is such, that I get a multiline help-message back, if I send 'H' via serial to the controller board. The multiline message the dev board is sending back looks like this:

"HELP\r\nList of commands:\r\nH:  Display list of commands\r\nS:  Start the application"

So I am expecting to see the following printout:

HELP
List of commands:
H:  Display list of commands
S:  Start the application

But instead I only get:

HELP

If I connect to the port with PuTTY and send 'H' manually, I get the full message; so I know that it is not a problem of my microcontroller program.

My python code looks like this:

import serial
import io

class mySerial:
    def __init__(self, port, baud_rate):
        self.serial = serial.Serial(port, baud_rate, timeout=1)
        self.serial_io_wrapped = io.TextIOWrapper(io.BufferedRWPair(self.serial, self.serial))

    # receive message via serial
    def read(self):
        read_out = None
        if self.serial.in_waiting > 0:
            read_out = self.serial_io_wrapped.readline()
        return read_out

    # send message via serial
    def write(self, message):
        self.serial.write(message)

    # flush the buffer
    def flush(self):
        self.serial.flush()


commandToSend = 'H'
ser = mySerial('COM9', 115200)

ser.flush()
ser.write(str(commandToSend).encode() + b"\n")

while True:
    incomingMessage = ser.read()
    if incomingMessage is not None:
        print(incomingMessage)

Any help would be much appreciated.

Upvotes: 0

Views: 1606

Answers (1)

ozturkib
ozturkib

Reputation: 1653

You need to wait for new data after each line. Based on that, your read method need to be modified as below:

def read(self):
    read_out = None
    timeout = time.time() + 0.1
    while ((self.serial.in_waiting > 0) and (timeout > time.time())):
        pass
    if self.serial.in_waiting > 0:
        read_out = self.serial_io_wrapped.readline()
    return read_out

Upvotes: 0

Related Questions