diff options
Diffstat (limited to 'tools/reset.py')
-rw-r--r-- | tools/reset.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tools/reset.py b/tools/reset.py new file mode 100644 index 0000000..6398c50 --- /dev/null +++ b/tools/reset.py @@ -0,0 +1,73 @@ +"""A tool to reset a Arduino.""" +import os +import sys +from typing import List +import argparse +from time import sleep + +import serial.tools.list_ports +from serial.tools.list_ports_common import ListPortInfo + + +if os.name == "nt": # NT-based operating systems (Windows) + from serial.serialwin32 import Serial +elif os.name == "posix": + from serial.serialposix import Serial +else: + raise NotImplementedError( + "Sorry no implementation for your platform ({}) available." + .format(sys.platform) + ) + + +def get_ports(): + """Returns a list of serial ports attached.""" + serial_ports = serial.tools.list_ports.comports() + + return [port.device for port in serial_ports] + + +def compare_ports( + avail_ports: List[str], + wanted_ports: List[str] +): + return [port for port in avail_ports if port in wanted_ports] + + +class ParsedArguments: + """Parsed command-line arguments.""" + port: str + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Reset an Arduino') + + parser.add_argument('port', help='Serial device e.g. /dev/ttyACM0 or com3') + + args = parser.parse_args(namespace=ParsedArguments) + + ports: List[str] = [] + + while args.port not in ports: + ports = get_ports() + + sys.stderr.write( + f"PORTS {{{', '.join(ports)}}} / {{{args.port}}} " + f"=> {{{', '.join(compare_ports(ports, [args.port]))}}}\n" + ) + sys.stderr.flush() + + sleep(0.5) + + ser = Serial(args.port, 57600) + + ser.baudrate = 1200 + + # ser.setRTS(True) + # ser.setDTR(False) + + ser.close() + + sleep(2) + + print(get_ports()[0]) |