Fix timing in readserial.py - use 10 bits per byte.
[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 # Number of bits in a transmitted byte - 8N1 is 1 start bit + 8 data
22 # bits + 1 stop bit.
23 BITSPERBYTE = 10
24
25 def readserial(infile, logfile, baudrate):
26     lasttime = 0
27     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)
37             lasttime = 0
38             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
44         if curtime - lasttime > RESTARTINTERVAL:
45             starttime = curtime
46             charcount = 0
47             isnewline = 1
48             msg = "\n\n======= %s (adjust=%d)\n" % (
49                 time.asctime(time.localtime(curtime)), ADJUSTBAUD)
50             sys.stdout.write(msg)
51             logfile.write(msg)
52         lasttime = curtime
53
54         # Translate unprintable chars; add timestamps
55         out = ""
56         for c in d:
57             if isnewline:
58                 delta = curtime - starttime
59                 if ADJUSTBAUD:
60                     delta -= float(charcount * BITSPERBYTE) / baudrate
61                 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
85 def printUsage():
86     print "Usage:\n   %s [<serialdevice> [<baud>]]" % (sys.argv[0],)
87     sys.exit(1)
88
89 def 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
105 if __name__ == '__main__':
106     main()