summaryrefslogtreecommitdiff
path: root/tools/monitor.py
blob: f909efb6fa502324ee14d12880804482d34faba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)