Fix auto-restart timeout in tools/readserial.py
[seabios.git] / tools / readserial.py
1 #!/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
11 import sys
12 import time
13 import select
14 import serial
15
16 # Reset time counter after this much idle time.
17 RESTARTINTERVAL = 60
18 # Alter timing reports based on how much time would be spent writing
19 # to serial.
20 ADJUSTBAUD = 1
21
22 def readserial(infile, logfile, baudrate):
23     lasttime = 0
24     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)
34             lasttime = 0
35             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
41         if curtime - lasttime > RESTARTINTERVAL:
42             starttime = curtime
43             charcount = 0
44             isnewline = 1
45             sys.stdout.write("\n")
46             logfile.write("\n")
47         lasttime = curtime
48
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
80 def printUsage():
81     print "Usage:\n   %s [<serialdevice> [<baud>]]" % (sys.argv[0],)
82     sys.exit(1)
83
84 def 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
100 if __name__ == '__main__':
101     main()