irrigation_system/software/serial_monitor.py

44 lines
1.2 KiB
Python

#!/usr/bin/env python3
import serial
import time
import sys
import argparse
def monitor_serial(port):
try:
# Open serial connection
ser = serial.Serial(port, 115200, timeout=1)
print(f"Connected to {port}")
# Send reset command
ser.write(b'reset\n')
print("Sent reset command")
# Wait a bit and then read output
time.sleep(0.5)
# Read output for 10 seconds
start_time = time.time()
while 1: #time.time() - start_time < 10:
if ser.in_waiting > 0:
data = ser.read(ser.in_waiting)
try:
text = data.decode('utf-8', errors='ignore')
print(text, end='')
except:
print(f"Raw bytes: {data}")
time.sleep(0.1)
ser.close()
print("\nSerial monitor closed")
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Serial monitor.')
parser.add_argument('-p', '--port', help='Serial port to connect to', required=True)
args = parser.parse_args()
monitor_serial(args.port)