blob: 4524ca831fb7fe7a51ec2695e452bc7ca5ebe6cf [file] [log] [blame]
Kevin O'Connor2547bb82009-04-19 11:04:59 -04001#!/usr/bin/env python
2# Script that can read from a serial device and show timestamps.
3#
4# Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
5#
6# This file may be distributed under the terms of the GNU GPLv3 license.
7
8# Usage:
9# tools/readserial.py /dev/ttyUSB0 115200
10
11import sys
12import time
13import select
14import serial
15
16# Reset time counter after this much idle time.
17RESTARTINTERVAL = 60
18# Alter timing reports based on how much time would be spent writing
19# to serial.
20ADJUSTBAUD = 1
21
22def readserial(infile, logfile, baudrate):
Kevin O'Connora1dadf42009-04-26 21:24:16 -040023 lasttime = 0
Kevin O'Connor2547bb82009-04-19 11:04:59 -040024 while 1:
25 # Read data
26 try:
27 res = select.select([infile, sys.stdin], [], [])
28 except KeyboardInterrupt:
29 sys.stdout.write("\n")
30 break
31 if sys.stdin in res[0]:
32 # Got keyboard input - force reset on next serial input
33 sys.stdin.read(1)
Kevin O'Connora1dadf42009-04-26 21:24:16 -040034 lasttime = 0
Kevin O'Connor2547bb82009-04-19 11:04:59 -040035 if len(res[0]) == 1:
36 continue
37 curtime = time.time()
38 d = infile.read(4096)
39
40 # Reset start time if no data for some time
Kevin O'Connora1dadf42009-04-26 21:24:16 -040041 if curtime - lasttime > RESTARTINTERVAL:
Kevin O'Connor2547bb82009-04-19 11:04:59 -040042 starttime = curtime
43 charcount = 0
44 isnewline = 1
45 sys.stdout.write("\n")
46 logfile.write("\n")
Kevin O'Connora1dadf42009-04-26 21:24:16 -040047 lasttime = curtime
Kevin O'Connor2547bb82009-04-19 11:04:59 -040048
49 # Translate unprintable chars; add timestamps
50 out = ""
51 for c in d:
52 if isnewline:
53 delta = curtime - starttime
54 if ADJUSTBAUD:
55 delta -= float(charcount * 9) / baudrate
56 out += "%06.3f: " % delta
57 isnewline = 0
58 oc = ord(c)
59 charcount += 1
60 if oc == 0x0d:
61 continue
62 if oc == 0x00:
63 out += "<00>\n"
64 isnewline = 1
65 continue
66 if oc == 0x0a:
67 out += "\n"
68 isnewline = 1
69 continue
70 if oc < 0x20 or oc >= 0x7f and oc != 0x09:
71 out += "<%02x>" % oc
72 continue
73 out += c
74
75 sys.stdout.write(out)
76 sys.stdout.flush()
77 logfile.write(out)
78 logfile.flush()
79
80def printUsage():
81 print "Usage:\n %s [<serialdevice> [<baud>]]" % (sys.argv[0],)
82 sys.exit(1)
83
84def main():
85 serialport = 0
86 baud = 115200
87 if len(sys.argv) > 3:
88 printUsage()
89 if len(sys.argv) > 1:
90 serialport = sys.argv[1]
91 if len(sys.argv) > 2:
92 baud = int(sys.argv[2])
93
94 ser = serial.Serial(serialport, baud, timeout=0)
95
96 logname = time.strftime("seriallog-%Y%m%d_%H%M%S.log")
97 f = open(logname, 'wb')
98 readserial(ser, f, baud)
99
100if __name__ == '__main__':
101 main()