55
55
supported_boards = {
56
56
"UNO-R3" : {"sampling_rate" : 250 , "Num_channels" : 6 },
57
57
"UNO-CLONE" : {"sampling_rate" : 250 , "Num_channels" : 6 }, # Baud Rate 115200
58
+ "GENUINO-UNO" : {"sampling_rate" : 250 , "Num_channels" : 6 },
58
59
"UNO-R4" : {"sampling_rate" : 500 , "Num_channels" : 6 },
59
60
"RPI-PICO-RP2040" : {"sampling_rate" : 500 , "Num_channels" : 3 },
60
61
"NANO-CLONE" : {"sampling_rate" : 250 , "Num_channels" : 8 }, # Baud Rate 115200
62
+ "BLACK-PILL" : {"sampling_rate" : 500 , "Num_channels" : 10 },
61
63
}
62
64
63
65
# Initialize gloabal variables for Incoming Data
@@ -80,14 +82,18 @@ def connect_hardware(port, baudrate, timeout=1):
80
82
ser = serial .Serial (port , baudrate = baudrate , timeout = timeout ) # Try opening the port
81
83
response = None
82
84
retry_counter = 0
83
- while response is None or retry_counter < retry_limit :
85
+ while response not in supported_boards and retry_counter < retry_limit :
84
86
ser .write (b'WHORU\n ' ) # Check board type
85
- response = ser .readline ().strip ().decode () # Try reading from the port
87
+ try :
88
+ response = ser .readline ().strip ().decode () # Attempt to decode the response
89
+ except UnicodeDecodeError as e :
90
+ print (f"Decode error: { e } . Ignoring this response." )
91
+ response = None
86
92
retry_counter += 1
87
93
if response in supported_boards : # If response is received, assume it's the Arduino
88
94
global board , sampling_rate , data , num_channels , packet_length
89
95
board = response # Set board type
90
- print (f"{ response } detected at { port } " ) # Notify the user
96
+ print (f"{ response } detected at { port } with baudrate { baudrate } . " ) # Notify the user
91
97
sampling_rate = supported_boards [board ]["sampling_rate" ]
92
98
num_channels = supported_boards [board ]["Num_channels" ]
93
99
packet_length = (2 * num_channels ) + HEADER_LENGTH + 1
@@ -97,17 +103,19 @@ def connect_hardware(port, baudrate, timeout=1):
97
103
ser .close () # Close the port if no response
98
104
except (OSError , serial .SerialException ): # Handle exceptions if the port can't be opened
99
105
pass
100
- print ("Unable to connect to any hardware! " ) # Notify if no Arduino is found
106
+ print (f "Unable to connect to any hardware at baudrate { baudrate } " ) # Notify if no Arduino is found
101
107
return None # Return None if not found
102
108
103
- # Function to automatically detect the Arduino's serial port
104
- def detect_hardware (baudrate , timeout = 1 ):
109
+ def detect_hardware (baudrate = None , timeout = 1 ):
105
110
ports = serial .tools .list_ports .comports () # List available serial ports
106
111
ser = None
112
+ baudrates = [baudrate ] if baudrate else [230400 , 115200 ]
107
113
for port in ports : # Iterate through each port
108
- ser = connect_hardware (port .device , baudrate )
109
- if ser is not None :
110
- return ser
114
+ for baud_rate in baudrates : # Iterate through all baud rates
115
+ print (f"Trying { port .device } at Baudrate { baud_rate } ..." )
116
+ ser = connect_hardware (port .device , baud_rate , timeout )
117
+ if ser is not None :
118
+ return ser
111
119
print ("Unable to detect hardware!" ) # Notify if no Arduino is found
112
120
return None # Return None if not found
113
121
@@ -319,7 +327,7 @@ def main():
319
327
global verbose ,ser
320
328
parser = argparse .ArgumentParser (description = "Upside Down Labs - Chords-Python Tool" ,allow_abbrev = False ) # Create argument parser
321
329
parser .add_argument ('-p' , '--port' , type = str , help = "Specify the COM port" ) # Port argument
322
- parser .add_argument ('-b' , '--baudrate' , type = int , default = 230400 , help = "Set baud rate for the serial communication" ) # Baud rate
330
+ parser .add_argument ('-b' , '--baudrate' , type = int , help = "Set baud rate for the serial communication" ) # Baud rate
323
331
parser .add_argument ('--csv' , action = 'store_true' , help = "Create and write to a CSV file" ) # CSV logging flag
324
332
parser .add_argument ('--lsl' , action = 'store_true' , help = "Start LSL stream" ) # LSL streaming flag
325
333
parser .add_argument ('-v' , '--verbose' , action = 'store_true' , help = "Enable verbose output with statistical data" ) # Verbose flag
@@ -337,6 +345,8 @@ def main():
337
345
parser .print_help () # Print help if no options are selected
338
346
return
339
347
348
+ baudrate = args .baudrate # Get baud rate from arguments
349
+
340
350
if args .port :
341
351
print ("trying to connect to port:" , args .port )
342
352
ser = connect_hardware (port = args .port , baudrate = args .baudrate )
0 commit comments