Here’s a simple and clean Python serial monitor (CLI version) using pyserial
. It works across Windows, macOS, and Linux, and reads/writes to your Arduino or other microcontroller just like the Arduino IDE Serial Monitor.
🔧 Step 1: Install pyserial
bashCopyEditpip install pyserial
🐍 Step 2: Basic Serial Monitor Script
pythonCopyEditimport serial
import threading
# === CONFIGURATION ===
PORT = '/dev/ttyUSB0' # Change to your actual port, e.g. COM3 on Windows
BAUDRATE = 9600
def read_from_port(ser):
while True:
try:
if ser.in_waiting:
line = ser.readline().decode('utf-8', errors='ignore').strip()
print(f"[RX] {line}")
except Exception as e:
print(f"[Error] {e}")
break
def main():
try:
ser = serial.Serial(PORT, BAUDRATE, timeout=1)
print(f"[Connected] Listening on {PORT} at {BAUDRATE} baud")
thread = threading.Thread(target=read_from_port, args=(ser,), daemon=True)
thread.start()
while True:
cmd = input("> ")
if cmd.lower() in ['exit', 'quit']:
break
ser.write((cmd + "\n").encode('utf-8'))
except serial.SerialException as e:
print(f"[Failed to connect] {e}")
except KeyboardInterrupt:
print("\n[Stopped by user]")
if __name__ == "__main__":
main()
🧪 Example Output
bashCopyEdit[Connected] Listening on /dev/ttyUSB0 at 9600 baud
> LED ON
[RX] LED is now ON
> LED OFF
[RX] LED is now OFF
> quit
✅ Features
- Real-time read from serial port
- Can send commands by typing
- Works on any serial device (Arduino, ESP32, etc.)
- Graceful exit with
quit
orCtrl+C