Also see the same example for a local serial port: PySerial minimal example: Copy data received from serial port to stdout
This example connects to the RFC2217 remote serial port on 10.1.2.3
port 1234
. It does not send data to the serial port but only copies data received from the serial port to stdout.
#!/usr/bin/env python3 import serial with serial.serial_for_url("rfc2217://10.1.2.3:1234", baudrate=115200) as ser: 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.