Last active
April 3, 2023 03:36
-
-
Save kellertk/72a070eab6221dea69454cbafc02403c to your computer and use it in GitHub Desktop.
Python autobaud script for serial devices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This program was entirely written with ChatGPT 4, is untested, and should be | |
# considered alpha-quality software. | |
# | |
# Tom Keller, [email protected], 2023 | |
# To the maximum extent possible under law, I waive all copyright and related or | |
# neighboring rights to this work. This work is licensed under Creative Commons | |
# CC0, available at https://creativecommons.org/publicdomain/zero/1.0/legalcode | |
import sys | |
import argparse | |
import serial | |
import serial.tools.list_ports | |
from time import sleep | |
def read_serial(port, baudrate, bytesize, parity, stopbits, timeout=0.5, debug=False): | |
if debug: print(f"Opening serial port {port} at {baudrate} baud") | |
ser = serial.Serial(port, baudrate, bytesize, parity, stopbits, timeout=timeout) | |
sleep(0.1) | |
if debug: print(f"Sending newline character to {port}") | |
ser.write(b'\n') | |
sleep(0.1) | |
data = ser.read(ser.in_waiting) | |
if debug: | |
print(f"Received {len(data)} bytes: {data}") | |
print(f"Closing serial port {port}") | |
ser.close() | |
return data.decode('ascii', errors='strict') | |
def autobaud(port, baudrates, bytesize, parity, stopbits, retries=5, debug=False): | |
for baudrate in baudrates: | |
for retry in range(retries): | |
try: | |
response = read_serial(port, baudrate, bytesize, parity, stopbits, | |
debug=debug) | |
if response: return baudrate, response | |
except (serial.SerialException, UnicodeDecodeError) as e: | |
if debug or retry == retries - 1: | |
print(f"Error at {baudrate}: {str(e)}") | |
return None, None | |
def main(args): | |
parser = argparse.ArgumentParser(description="Autobaud script using pyserial") | |
parser.add_argument("port", help="Serial port name") | |
parser.add_argument("-b", "--baudrates", nargs="+", type=int, | |
default=[9600, 19200, 38400, 57600, 115200], | |
help="List of baud rates to try (default: [9600, 19200, 38400, 57600, 115200])") | |
parser.add_argument("--bytesize", type=int, default=8, | |
help="Number of data bits (default: 8)") | |
parser.add_argument("--parity", default="N", help="Parity (default: N)") | |
parser.add_argument("--stopbits", type=int, default=1, | |
help="Number of stop bits (default: 1)") | |
parser.add_argument("--debug", action="store_true", | |
help="Enable debug mode for additional logging") | |
args = parser.parse_args(args) | |
baudrate, response = autobaud(args.port, args.baudrates, args.bytesize, | |
args.parity, args.stopbits, debug=args.debug) | |
if baudrate: | |
if args.debug: print(f"Found intelligible response at {baudrate} baud: {response}\n\n") | |
print(f"{baudrate}") | |
else: print("Failed to find an intelligible response at the given baud rates") | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment