Skip to content

Commit e5a59a8

Browse files
committed
Updated script to prioritize baud rate from argparse; defaults to trying 230400 first, then 115200 if not provided.
1 parent 355769e commit e5a59a8

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

chords.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@
5555
supported_boards = {
5656
"UNO-R3": {"sampling_rate": 250, "Num_channels": 6},
5757
"UNO-CLONE": {"sampling_rate": 250, "Num_channels": 6}, # Baud Rate 115200
58+
"GENUINO-UNO": {"sampling_rate": 250, "Num_channels": 6},
5859
"UNO-R4": {"sampling_rate": 500, "Num_channels": 6},
5960
"RPI-PICO-RP2040": {"sampling_rate": 500, "Num_channels": 3},
6061
"NANO-CLONE": {"sampling_rate": 250, "Num_channels": 8}, # Baud Rate 115200
62+
"BLACK-PILL": {"sampling_rate": 500, "Num_channels": 10},
6163
}
6264

6365
# Initialize gloabal variables for Incoming Data
@@ -80,14 +82,18 @@ def connect_hardware(port, baudrate, timeout=1):
8082
ser = serial.Serial(port, baudrate=baudrate, timeout=timeout) # Try opening the port
8183
response = None
8284
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:
8486
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
8692
retry_counter += 1
8793
if response in supported_boards: # If response is received, assume it's the Arduino
8894
global board, sampling_rate, data, num_channels, packet_length
8995
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
9197
sampling_rate = supported_boards[board]["sampling_rate"]
9298
num_channels = supported_boards[board]["Num_channels"]
9399
packet_length = (2 * num_channels) + HEADER_LENGTH + 1
@@ -97,17 +103,19 @@ def connect_hardware(port, baudrate, timeout=1):
97103
ser.close() # Close the port if no response
98104
except (OSError, serial.SerialException): # Handle exceptions if the port can't be opened
99105
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
101107
return None # Return None if not found
102108

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):
105110
ports = serial.tools.list_ports.comports() # List available serial ports
106111
ser = None
112+
baudrates = [baudrate] if baudrate else [230400, 115200]
107113
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
111119
print("Unable to detect hardware!") # Notify if no Arduino is found
112120
return None # Return None if not found
113121

@@ -319,7 +327,7 @@ def main():
319327
global verbose,ser
320328
parser = argparse.ArgumentParser(description="Upside Down Labs - Chords-Python Tool",allow_abbrev = False) # Create argument parser
321329
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
323331
parser.add_argument('--csv', action='store_true', help="Create and write to a CSV file") # CSV logging flag
324332
parser.add_argument('--lsl', action='store_true', help="Start LSL stream") # LSL streaming flag
325333
parser.add_argument('-v', '--verbose', action='store_true', help="Enable verbose output with statistical data") # Verbose flag
@@ -337,6 +345,8 @@ def main():
337345
parser.print_help() # Print help if no options are selected
338346
return
339347

348+
baudrate = args.baudrate # Get baud rate from arguments
349+
340350
if args.port:
341351
print("trying to connect to port:", args.port)
342352
ser = connect_hardware(port=args.port, baudrate=args.baudrate)

0 commit comments

Comments
 (0)