diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-14 09:53:28 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-02-14 10:05:53 +0100 |
commit | a8049bb66f7bc62c9a33488615d0990e0a920520 (patch) | |
tree | 1372e18896897490650203efe72c8244a23204cf /tools/monitor.py | |
parent | 336d968dd2a4473b3acd71cfef86ad47a7901fbc (diff) |
build: add utility scripts
Diffstat (limited to 'tools/monitor.py')
-rw-r--r-- | tools/monitor.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tools/monitor.py b/tools/monitor.py new file mode 100644 index 0000000..f909efb --- /dev/null +++ b/tools/monitor.py @@ -0,0 +1,44 @@ +"""Serial monitor.""" +import argparse +import sys +import asyncio +import os + +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) + ) + + +async def read(port: str, baud_rate: int): + """Reads a serial port.""" + with Serial(port, baud_rate) as serial_port: + while(serial_port.is_open): + sys.stdout.buffer.write(serial_port.read()) + sys.stdout.flush() + + +async def main(): + """Monitors serial output.""" + parser = argparse.ArgumentParser( + description="A tool for monitoring a Arduino") + + parser.add_argument( + "port", help="A serial device port e.g. /dev/ttyACM0 or com3") + parser.add_argument("baud_rate", help="The serial device baud rate") + + args = parser.parse_args() + + await read(args.port, args.baud_rate) + +if __name__ == "__main__": + try: + asyncio.run(main()) + except KeyboardInterrupt: + print("\nReceived keyboard interrupt. Exiting...") + exit(0) |