46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
import json
|
|
import serial
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
# import colorama
|
|
# colorama.just_fix_windows_console()
|
|
|
|
print("\x1b[0;32mThis should be green\x1b[0m")
|
|
print("\x1b[0;31mThis should be red\x1b[0m")
|
|
|
|
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:
|
|
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()
|