2 # Script that can read from a serial device and show timestamps.
4 # Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
6 # This file may be distributed under the terms of the GNU GPLv3 license.
9 # tools/readserial.py /dev/ttyUSB0 115200
16 # Reset time counter after this much idle time.
18 # Alter timing reports based on how much time would be spent writing
22 def readserial(infile, logfile, baudrate):
27 res = select.select([infile, sys.stdin], [], [])
28 except KeyboardInterrupt:
29 sys.stdout.write("\n")
31 if sys.stdin in res[0]:
32 # Got keyboard input - force reset on next serial input
40 # Reset start time if no data for some time
41 if curtime - lasttime > RESTARTINTERVAL:
45 sys.stdout.write("\n")
49 # Translate unprintable chars; add timestamps
53 delta = curtime - starttime
55 delta -= float(charcount * 9) / baudrate
56 out += "%06.3f: " % delta
70 if oc < 0x20 or oc >= 0x7f and oc != 0x09:
81 print "Usage:\n %s [<serialdevice> [<baud>]]" % (sys.argv[0],)
90 serialport = sys.argv[1]
92 baud = int(sys.argv[2])
94 ser = serial.Serial(serialport, baud, timeout=0)
96 logname = time.strftime("seriallog-%Y%m%d_%H%M%S.log")
97 f = open(logname, 'wb')
98 readserial(ser, f, baud)
100 if __name__ == '__main__':