-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserial_reader.py
More file actions
executable file
·29 lines (26 loc) · 983 Bytes
/
serial_reader.py
File metadata and controls
executable file
·29 lines (26 loc) · 983 Bytes
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
#!/usr/bin/env python
from argparse import ArgumentParser
import sys
import serial
from datetime import datetime
def run(device, baud, prefix=None):
with serial.Serial(device, baud, timeout=0.1) as ser:
while True:
line = ser.readline()
if not line:
continue
if prefix:
line = prefix() + line
sys.stdout.write(line)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('device',
help='serial device, typically /dev/tty.usbserial-*')
parser.add_argument('--baud', dest='baud', type=int, default=74880)
parser.add_argument('-t', '--timestamp', dest='timestamp', action='store_true',
help="Add timestamp to start of each line")
args = parser.parse_args()
prefix = None
if args.timestamp:
prefix = lambda: datetime.now().strftime("[%H:%M:%S.%f] ")
run(args.device, args.baud, prefix)