Enhance readserial.py to note when resetting timer.
[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             msg = "\n\n======= %s (adjust=%d)\n" % (
46                 time.asctime(time.localtime(curtime)), ADJUSTBAUD)
47             sys.stdout.write(msg)
48             logfile.write(msg)
49         lasttime = curtime
50
51         # Translate unprintable chars; add timestamps
52         out = ""
53         for c in d:
54             if isnewline:
55                 delta = curtime - starttime
56                 if ADJUSTBAUD:
57                     delta -= float(charcount * 9) / baudrate
58                 out += "%06.3f: " % delta
59                 isnewline = 0
60             oc = ord(c)
61             charcount += 1
62             if oc == 0x0d:
63                 continue
64             if oc == 0x00:
65                 out += "<00>\n"
66                 isnewline = 1
67                 continue
68             if oc == 0x0a:
69                 out += "\n"
70                 isnewline = 1
71                 continue
72             if oc < 0x20 or oc >= 0x7f and oc != 0x09:
73                 out += "<%02x>" % oc
74                 continue
75             out += c
76
77         sys.stdout.write(out)
78         sys.stdout.flush()
79         logfile.write(out)
80         logfile.flush()
81
82 def printUsage():
83     print "Usage:\n   %s [<serialdevice> [<baud>]]" % (sys.argv[0],)
84     sys.exit(1)
85
86 def main():
87     serialport = 0
88     baud = 115200
89     if len(sys.argv) > 3:
90         printUsage()
91     if len(sys.argv) > 1:
92         serialport = sys.argv[1]
93     if len(sys.argv) > 2:
94         baud = int(sys.argv[2])
95
96     ser = serial.Serial(serialport, baud, timeout=0)
97
98     logname = time.strftime("seriallog-%Y%m%d_%H%M%S.log")
99     f = open(logname, 'wb')
100     readserial(ser, f, baud)
101
102 if __name__ == '__main__':
103     main()