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
21 # Number of bits in a transmitted byte - 8N1 is 1 start bit + 8 data
25 def readserial(infile, logfile, baudrate):
30 res = select.select([infile, sys.stdin], [], [])
31 except KeyboardInterrupt:
32 sys.stdout.write("\n")
34 if sys.stdin in res[0]:
35 # Got keyboard input - force reset on next serial input
43 # Reset start time if no data for some time
44 if curtime - lasttime > RESTARTINTERVAL:
48 msg = "\n\n======= %s (adjust=%d)\n" % (
49 time.asctime(time.localtime(curtime)), ADJUSTBAUD)
54 # Translate unprintable chars; add timestamps
58 delta = curtime - starttime
60 delta -= float(charcount * BITSPERBYTE) / baudrate
61 out += "%06.3f: " % delta
75 if oc < 0x20 or oc >= 0x7f and oc != 0x09:
86 print "Usage:\n %s [<serialdevice> [<baud>]]" % (sys.argv[0],)
95 serialport = sys.argv[1]
97 baud = int(sys.argv[2])
99 ser = serial.Serial(serialport, baud, timeout=0)
101 logname = time.strftime("seriallog-%Y%m%d_%H%M%S.log")
102 f = open(logname, 'wb')
103 readserial(ser, f, baud)
105 if __name__ == '__main__':