import json import serial import sys import time from pathlib import Path def main(): # Load config root = Path(__file__).resolve().parent.parent config_file = root / "settings.json" if not config_file.exists(): print(f"Error: settings.json not found at {config_file}") sys.exit(1) with open(config_file) as f: cfg = json.load(f) port = cfg.get("port", "COM3") baud = cfg.get("monitor_baud", 115200) print(f"[Serial Monitor] Connecting to {port} at {baud} baud...") try: with serial.Serial(port, baud, timeout=0.5) as ser: # 🔧 Clear DTR and RTS lines (common for ESP32 reset handling) ser.dtr = False ser.rts = False print("[Serial Monitor] Press Ctrl+C to exit.\n") while True: if ser.in_waiting: line = ser.readline().decode(errors="replace") if line: print(line, end="") time.sleep(0.01) except KeyboardInterrupt: print("\n[Serial Monitor] Disconnected.") except serial.SerialException as e: print(f"[Error] Could not open serial port {port}: {e}") sys.exit(1) if __name__ == "__main__": main()