-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_serial.py
More file actions
executable file
·124 lines (106 loc) · 3.69 KB
/
monitor_serial.py
File metadata and controls
executable file
·124 lines (106 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
"""
Serial monitor for Daisy Seed
Usage: python3 monitor_serial.py [port]
If no port specified, will try to auto-detect
"""
import sys
import serial
import serial.tools.list_ports
import time
BAUD_RATE = 115200
def list_available_ports():
"""List all available serial ports"""
ports = serial.tools.list_ports.comports()
print("\nAvailable serial ports:")
print("-" * 60)
for port in ports:
print(f" {port.device:20} - {port.description}")
print("-" * 60)
return [p.device for p in ports]
def find_daisy_port():
"""Try to auto-detect Daisy Seed port"""
ports = serial.tools.list_ports.comports()
# Common Daisy Seed identifiers
daisy_keywords = ['daisy', 'seed', 'stm', 'usbmodem', 'usbserial', 'SLAB']
for port in ports:
desc_lower = port.description.lower()
device_lower = port.device.lower()
for keyword in daisy_keywords:
if keyword in desc_lower or keyword in device_lower:
return port.device
return None
def monitor_serial(port_name):
"""Monitor serial output from specified port"""
try:
ser = serial.Serial(
port=port_name,
baudrate=BAUD_RATE,
timeout=1,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE
)
print("=" * 60)
print("Daisy Seed Serial Monitor")
print("=" * 60)
print(f"Port: {port_name}")
print(f"Baud Rate: {BAUD_RATE}")
print("Press Ctrl+C to exit")
print("=" * 60)
print()
# Clear any existing data
ser.reset_input_buffer()
# Monitor loop
while True:
if ser.in_waiting > 0:
try:
line = ser.readline().decode('utf-8', errors='ignore').strip()
if line:
print(line)
except UnicodeDecodeError:
# Skip invalid characters
pass
time.sleep(0.01) # Small delay to prevent CPU spinning
except serial.SerialException as e:
print(f"Error opening serial port: {e}")
print("\nMake sure:")
print(" 1. Daisy Seed is connected via USB")
print(" 2. Daisy Seed is NOT in bootloader/DFU mode")
print(" 3. No other program is using the serial port")
return 1
except KeyboardInterrupt:
print("\n\nMonitoring stopped.")
if ser.is_open:
ser.close()
return 0
except Exception as e:
print(f"Unexpected error: {e}")
return 1
def main():
if len(sys.argv) > 1:
port = sys.argv[1]
else:
print("Searching for Daisy Seed...")
port = find_daisy_port()
if not port:
print("Could not auto-detect Daisy Seed port.")
available = list_available_ports()
if not available:
print("\nNo serial ports found!")
print("\nMake sure:")
print(" 1. Daisy Seed is connected via USB")
print(" 2. Daisy Seed is NOT in bootloader/DFU mode")
print(" 3. Daisy Seed firmware is running")
return 1
else:
print("\nPlease specify the port manually:")
print(f" python3 {sys.argv[0]} <port_name>")
print("\nExample:")
print(f" python3 {sys.argv[0]} {available[0]}")
return 1
else:
print(f"Found port: {port}")
return monitor_serial(port)
if __name__ == "__main__":
sys.exit(main())