PySerial minimal example: Copy data received from serial port to stdout

This example does not send data to the serial port but only copies data received from the serial port to stdout. Newlines received from the serial port are preserved.

#!/usr/bin/env python3
import serial
ser = serial.Serial("/dev/ttyUSB0", baudrate=115200)

    while True:
        response =
        if response:
            print(response.decode("iso-8859-1"), end="")

By using iso-8859-1   decoding, we ensure that even binary bytes are decoded in some way and do not cause an exception.