PySerial
Read from and write to a serial (UART/USB) port from Python.
Easy
import serial
with serial.Serial("COM3", 115200, timeout=1) as port: # "/dev/ttyUSB0" on Linux
port.write(b"LED ON\n")
line = port.readline().decode().strip()
print(line)
Both sides must use the same baud rate. readline() waits until a newline or the timeout. Use this to log sensor data to CSV, build a test jig, or control a device from a GUI.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.