blob: 4b47b1434ed4bb7fdacba6610bc087cc8ccce934 [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
Kevin O'Connor77443202009-12-05 14:23:27 -050021# Number of bits in a transmitted byte - 8N1 is 1 start bit + 8 data
22# bits + 1 stop bit.
23BITSPERBYTE = 10
Kevin O'Connor2547bb82009-04-19 11:04:59 -040024
25def readserial(infile, logfile, baudrate):
Kevin O'Connora1dadf42009-04-26 21:24:16 -040026 lasttime = 0
Kevin O'Connor2547bb82009-04-19 11:04:59 -040027 while 1:
28 # Read data
29 try:
30 res = select.select([infile, sys.stdin], [], [])
31 except KeyboardInterrupt:
32 sys.stdout.write("\n")
33 break
34 if sys.stdin in res[0]:
35 # Got keyboard input - force reset on next serial input
36 sys.stdin.read(1)
Kevin O'Connora1dadf42009-04-26 21:24:16 -040037 lasttime = 0
Kevin O'Connor2547bb82009-04-19 11:04:59 -040038 if len(res[0]) == 1:
39 continue
40 curtime = time.time()
41 d = infile.read(4096)
42
43 # Reset start time if no data for some time
Kevin O'Connora1dadf42009-04-26 21:24:16 -040044 if curtime - lasttime > RESTARTINTERVAL:
Kevin O'Connor2547bb82009-04-19 11:04:59 -040045 starttime = curtime
46 charcount = 0
47 isnewline = 1
Kevin O'Connora53ab002009-12-05 13:44:39 -050048 msg = "\n\n======= %s (adjust=%d)\n" % (
49 time.asctime(time.localtime(curtime)), ADJUSTBAUD)
50 sys.stdout.write(msg)
51 logfile.write(msg)
Kevin O'Connora1dadf42009-04-26 21:24:16 -040052 lasttime = curtime
Kevin O'Connor2547bb82009-04-19 11:04:59 -040053
54 # Translate unprintable chars; add timestamps
55 out = ""
56 for c in d:
57 if isnewline:
58 delta = curtime - starttime
59 if ADJUSTBAUD:
Kevin O'Connor77443202009-12-05 14:23:27 -050060 delta -= float(charcount * BITSPERBYTE) / baudrate
Kevin O'Connor2547bb82009-04-19 11:04:59 -040061 out += "%06.3f: " % delta
62 isnewline = 0
63 oc = ord(c)
64 charcount += 1
65 if oc == 0x0d:
66 continue
67 if oc == 0x00:
68 out += "<00>\n"
69 isnewline = 1
70 continue
71 if oc == 0x0a:
72 out += "\n"
73 isnewline = 1
74 continue
75 if oc < 0x20 or oc >= 0x7f and oc != 0x09:
76 out += "<%02x>" % oc
77 continue
78 out += c
79
80 sys.stdout.write(out)
81 sys.stdout.flush()
82 logfile.write(out)
83 logfile.flush()
84
85def printUsage():
86 print "Usage:\n %s [<serialdevice> [<baud>]]" % (sys.argv[0],)
87 sys.exit(1)
88
89def main():
90 serialport = 0
91 baud = 115200
92 if len(sys.argv) > 3:
93 printUsage()
94 if len(sys.argv) > 1:
95 serialport = sys.argv[1]
96 if len(sys.argv) > 2:
97 baud = int(sys.argv[2])
98
99 ser = serial.Serial(serialport, baud, timeout=0)
100
101 logname = time.strftime("seriallog-%Y%m%d_%H%M%S.log")
102 f = open(logname, 'wb')
103 readserial(ser, f, baud)
104
105if __name__ == '__main__':
106 main()