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 # Number of bits in a transmitted byte - 8N1 is 1 start bit + 8 data
22 def calibrateserialwrite(outfile, byteadjust):
23 # Build 4000 bytes of dummy data.
24 data = "0123456789" * 4 + "012345678" + "\n"
32 "Wrote %d - %.1fus per char (theory states %.1fus)\n" % (
33 len(data), (et-st) / len(data) * 1000000, byteadjust * 1000000))
37 def calibrateserialread(infile, byteadjust):
38 starttime = lasttime = 0
41 select.select([infile], [], [])
44 if curtime - lasttime > 1.0:
45 if starttime and totalchars:
47 "Calibrating on %d bytes - %.1fus per char"
48 " (theory states %.1fus)\n" % (
50 float(lasttime - starttime) * 1000000 / totalchars,
51 byteadjust * 1000000))
58 def readserial(infile, logfile, byteadjust):
63 res = select.select([infile, sys.stdin], [], [])
64 except KeyboardInterrupt:
65 sys.stdout.write("\n")
67 if sys.stdin in res[0]:
68 # Got keyboard input - force reset on next serial input
76 datatime = time.time()
78 datatime -= len(d) * byteadjust
80 # Reset start time if no data for some time
81 if datatime - lasttime > RESTARTINTERVAL:
85 msg = "\n\n======= %s (adjust=%.1fus)\n" % (
86 time.asctime(time.localtime(datatime)), byteadjust * 1000000)
91 # Translate unprintable chars; add timestamps
95 delta = datatime - starttime - (charcount * byteadjust)
96 out += "%06.3f: " % delta
100 datatime += byteadjust
111 if oc < 0x20 or oc >= 0x7f and oc != 0x09:
116 sys.stdout.write(out)
122 usage = "%prog [options] [<serialdevice> [<baud>]]"
123 opts = optparse.OptionParser(usage)
124 opts.add_option("-f", "--file",
125 action="store_false", dest="serial", default=True,
126 help="read from file instead of serialdevice")
127 opts.add_option("-n", "--no-adjust",
128 action="store_false", dest="adjustbaud", default=True,
129 help="don't adjust times by serial rate")
130 opts.add_option("-c", "--calibrate-read",
131 action="store_true", dest="calibrate_read", default=False,
132 help="read from serial port to calibrate it")
133 opts.add_option("-C", "--calibrate-write",
134 action="store_true", dest="calibrate_write", default=False,
135 help="write to serial port to calibrate it")
136 opts.add_option("-t", "--time",
137 type="float", dest="time", default=None,
138 help="time to write one byte on serial port (in us)")
139 options, args = opts.parse_args()
143 opts.error("Too many arguments")
148 byteadjust = float(BITSPERBYTE) / baud
149 if options.time is not None:
150 byteadjust = options.time / 1000000.0
151 if not options.adjustbaud:
155 # Read from serial port
160 Unable to find pyserial package ( http://pyserial.sourceforge.net/ ).
161 On Linux machines try: yum install pyserial
162 Or: apt-get install python-serial
165 ser = serial.Serial(serialport, baud, timeout=0)
168 ser = open(serialport, 'rb')
171 fcntl.fcntl(ser, fcntl.F_SETFL
172 , fcntl.fcntl(ser, fcntl.F_GETFL) | os.O_NONBLOCK)
174 if options.calibrate_read:
175 calibrateserialread(ser, byteadjust)
177 if options.calibrate_write:
178 calibrateserialwrite(ser, byteadjust)
181 logname = time.strftime("seriallog-%Y%m%d_%H%M%S.log")
182 f = open(logname, 'wb')
183 readserial(ser, f, byteadjust)
185 if __name__ == '__main__':