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) try: while True: response = ser.read() if response: print(response.decode("iso-8859-1"), end="") finally: ser.close()
By using iso-8859-1
decoding, we ensure that even binary bytes are decoded in some way and do not cause an exception.