Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 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: |
Kevin O'Connor | ffc0687 | 2014-06-11 15:40:04 -0400 | [diff] [blame] | 9 | # scripts/readserial.py /dev/ttyUSB0 115200 |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 10 | |
Kevin O'Connor | 999567f | 2014-12-31 00:18:05 -0500 | [diff] [blame] | 11 | import sys, os, time, select, optparse |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 12 | |
Johannes Krampf | 19f789b | 2014-01-19 16:03:49 +0100 | [diff] [blame] | 13 | from python23compat import as_bytes |
| 14 | |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 15 | # Reset time counter after this much idle time. |
| 16 | RESTARTINTERVAL = 60 |
Kevin O'Connor | 7744320 | 2009-12-05 14:23:27 -0500 | [diff] [blame] | 17 | # Number of bits in a transmitted byte - 8N1 is 1 start bit + 8 data |
| 18 | # bits + 1 stop bit. |
| 19 | BITSPERBYTE = 10 |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 20 | |
Kevin O'Connor | 8365dee | 2011-07-09 13:16:24 -0400 | [diff] [blame] | 21 | def calibrateserialwrite(outfile, byteadjust): |
| 22 | # Build 4000 bytes of dummy data. |
| 23 | data = "0123456789" * 4 + "012345678" + "\n" |
| 24 | data = data * 80 |
| 25 | while 1: |
| 26 | st = time.time() |
Johannes Krampf | 19f789b | 2014-01-19 16:03:49 +0100 | [diff] [blame] | 27 | outfile.write(as_bytes(data)) |
Kevin O'Connor | 8365dee | 2011-07-09 13:16:24 -0400 | [diff] [blame] | 28 | outfile.flush() |
| 29 | et = time.time() |
| 30 | sys.stdout.write( |
| 31 | "Wrote %d - %.1fus per char (theory states %.1fus)\n" % ( |
| 32 | len(data), (et-st) / len(data) * 1000000, byteadjust * 1000000)) |
| 33 | sys.stdout.flush() |
| 34 | time.sleep(3) |
| 35 | |
| 36 | def calibrateserialread(infile, byteadjust): |
| 37 | starttime = lasttime = 0 |
| 38 | totalchars = 0 |
| 39 | while 1: |
| 40 | select.select([infile], [], []) |
| 41 | d = infile.read(4096) |
| 42 | curtime = time.time() |
| 43 | if curtime - lasttime > 1.0: |
| 44 | if starttime and totalchars: |
| 45 | sys.stdout.write( |
| 46 | "Calibrating on %d bytes - %.1fus per char" |
| 47 | " (theory states %.1fus)\n" % ( |
| 48 | totalchars, |
| 49 | float(lasttime - starttime) * 1000000 / totalchars, |
| 50 | byteadjust * 1000000)) |
| 51 | totalchars = 0 |
| 52 | starttime = curtime |
| 53 | else: |
| 54 | totalchars += len(d) |
| 55 | lasttime = curtime |
| 56 | |
| 57 | def readserial(infile, logfile, byteadjust): |
Kevin O'Connor | a1dadf4 | 2009-04-26 21:24:16 -0400 | [diff] [blame] | 58 | lasttime = 0 |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 59 | while 1: |
| 60 | # Read data |
| 61 | try: |
| 62 | res = select.select([infile, sys.stdin], [], []) |
| 63 | except KeyboardInterrupt: |
| 64 | sys.stdout.write("\n") |
Kevin O'Connor | 999567f | 2014-12-31 00:18:05 -0500 | [diff] [blame] | 65 | return -1 |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 66 | if sys.stdin in res[0]: |
| 67 | # Got keyboard input - force reset on next serial input |
| 68 | sys.stdin.read(1) |
Kevin O'Connor | a1dadf4 | 2009-04-26 21:24:16 -0400 | [diff] [blame] | 69 | lasttime = 0 |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 70 | if len(res[0]) == 1: |
| 71 | continue |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 72 | d = infile.read(4096) |
Kevin O'Connor | 2dcd9fa | 2010-08-28 14:55:32 -0400 | [diff] [blame] | 73 | if not d: |
Kevin O'Connor | 999567f | 2014-12-31 00:18:05 -0500 | [diff] [blame] | 74 | return 0 |
Kevin O'Connor | 58db9c4 | 2009-12-12 13:28:38 -0500 | [diff] [blame] | 75 | datatime = time.time() |
| 76 | |
| 77 | datatime -= len(d) * byteadjust |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 78 | |
| 79 | # Reset start time if no data for some time |
Kevin O'Connor | 58db9c4 | 2009-12-12 13:28:38 -0500 | [diff] [blame] | 80 | if datatime - lasttime > RESTARTINTERVAL: |
| 81 | starttime = datatime |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 82 | charcount = 0 |
| 83 | isnewline = 1 |
Kevin O'Connor | 8365dee | 2011-07-09 13:16:24 -0400 | [diff] [blame] | 84 | msg = "\n\n======= %s (adjust=%.1fus)\n" % ( |
| 85 | time.asctime(time.localtime(datatime)), byteadjust * 1000000) |
Kevin O'Connor | a53ab00 | 2009-12-05 13:44:39 -0500 | [diff] [blame] | 86 | sys.stdout.write(msg) |
Johannes Krampf | 19f789b | 2014-01-19 16:03:49 +0100 | [diff] [blame] | 87 | logfile.write(as_bytes(msg)) |
Kevin O'Connor | 58db9c4 | 2009-12-12 13:28:38 -0500 | [diff] [blame] | 88 | lasttime = datatime |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 89 | |
| 90 | # Translate unprintable chars; add timestamps |
Johannes Krampf | 19f789b | 2014-01-19 16:03:49 +0100 | [diff] [blame] | 91 | out = as_bytes("") |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 92 | for c in d: |
| 93 | if isnewline: |
Kevin O'Connor | 58db9c4 | 2009-12-12 13:28:38 -0500 | [diff] [blame] | 94 | delta = datatime - starttime - (charcount * byteadjust) |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 95 | out += "%06.3f: " % delta |
| 96 | isnewline = 0 |
| 97 | oc = ord(c) |
| 98 | charcount += 1 |
Kevin O'Connor | 58db9c4 | 2009-12-12 13:28:38 -0500 | [diff] [blame] | 99 | datatime += byteadjust |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 100 | if oc == 0x0d: |
| 101 | continue |
| 102 | if oc == 0x00: |
| 103 | out += "<00>\n" |
| 104 | isnewline = 1 |
| 105 | continue |
| 106 | if oc == 0x0a: |
| 107 | out += "\n" |
| 108 | isnewline = 1 |
| 109 | continue |
| 110 | if oc < 0x20 or oc >= 0x7f and oc != 0x09: |
| 111 | out += "<%02x>" % oc |
| 112 | continue |
| 113 | out += c |
| 114 | |
Johannes Krampf | 19f789b | 2014-01-19 16:03:49 +0100 | [diff] [blame] | 115 | if (sys.version_info > (3, 0)): |
| 116 | sys.stdout.buffer.write(out) |
| 117 | else: |
| 118 | sys.stdout.write(out) |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 119 | sys.stdout.flush() |
| 120 | logfile.write(out) |
| 121 | logfile.flush() |
| 122 | |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 123 | def main(): |
Kevin O'Connor | 2dcd9fa | 2010-08-28 14:55:32 -0400 | [diff] [blame] | 124 | usage = "%prog [options] [<serialdevice> [<baud>]]" |
| 125 | opts = optparse.OptionParser(usage) |
| 126 | opts.add_option("-f", "--file", |
| 127 | action="store_false", dest="serial", default=True, |
Kevin O'Connor | 999567f | 2014-12-31 00:18:05 -0500 | [diff] [blame] | 128 | help="read from unix named pipe instead of serialdevice") |
Kevin O'Connor | 2dcd9fa | 2010-08-28 14:55:32 -0400 | [diff] [blame] | 129 | opts.add_option("-n", "--no-adjust", |
| 130 | action="store_false", dest="adjustbaud", default=True, |
| 131 | help="don't adjust times by serial rate") |
Kevin O'Connor | 8365dee | 2011-07-09 13:16:24 -0400 | [diff] [blame] | 132 | opts.add_option("-c", "--calibrate-read", |
| 133 | action="store_true", dest="calibrate_read", default=False, |
| 134 | help="read from serial port to calibrate it") |
| 135 | opts.add_option("-C", "--calibrate-write", |
| 136 | action="store_true", dest="calibrate_write", default=False, |
| 137 | help="write to serial port to calibrate it") |
| 138 | opts.add_option("-t", "--time", |
| 139 | type="float", dest="time", default=None, |
| 140 | help="time to write one byte on serial port (in us)") |
Kevin O'Connor | 2dcd9fa | 2010-08-28 14:55:32 -0400 | [diff] [blame] | 141 | options, args = opts.parse_args() |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 142 | serialport = 0 |
| 143 | baud = 115200 |
Kevin O'Connor | 2dcd9fa | 2010-08-28 14:55:32 -0400 | [diff] [blame] | 144 | if len(args) > 2: |
| 145 | opts.error("Too many arguments") |
| 146 | if len(args) > 0: |
| 147 | serialport = args[0] |
| 148 | if len(args) > 1: |
| 149 | baud = int(args[1]) |
Kevin O'Connor | 8365dee | 2011-07-09 13:16:24 -0400 | [diff] [blame] | 150 | byteadjust = float(BITSPERBYTE) / baud |
| 151 | if options.time is not None: |
| 152 | byteadjust = options.time / 1000000.0 |
| 153 | if not options.adjustbaud: |
| 154 | byteadjust = 0.0 |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 155 | |
Kevin O'Connor | 2dcd9fa | 2010-08-28 14:55:32 -0400 | [diff] [blame] | 156 | if options.serial: |
| 157 | # Read from serial port |
Kevin O'Connor | 9433098 | 2010-10-17 21:38:38 -0400 | [diff] [blame] | 158 | try: |
| 159 | import serial |
| 160 | except ImportError: |
Johannes Krampf | 064fd06 | 2014-01-12 11:14:54 -0500 | [diff] [blame] | 161 | print(""" |
Kevin O'Connor | 9433098 | 2010-10-17 21:38:38 -0400 | [diff] [blame] | 162 | Unable to find pyserial package ( http://pyserial.sourceforge.net/ ). |
| 163 | On Linux machines try: yum install pyserial |
| 164 | Or: apt-get install python-serial |
Johannes Krampf | 064fd06 | 2014-01-12 11:14:54 -0500 | [diff] [blame] | 165 | """) |
Kevin O'Connor | 9433098 | 2010-10-17 21:38:38 -0400 | [diff] [blame] | 166 | sys.exit(1) |
Kevin O'Connor | 2dcd9fa | 2010-08-28 14:55:32 -0400 | [diff] [blame] | 167 | ser = serial.Serial(serialport, baud, timeout=0) |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 168 | |
Kevin O'Connor | 8365dee | 2011-07-09 13:16:24 -0400 | [diff] [blame] | 169 | if options.calibrate_read: |
| 170 | calibrateserialread(ser, byteadjust) |
| 171 | return |
| 172 | if options.calibrate_write: |
| 173 | calibrateserialwrite(ser, byteadjust) |
| 174 | return |
| 175 | |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 176 | logname = time.strftime("seriallog-%Y%m%d_%H%M%S.log") |
| 177 | f = open(logname, 'wb') |
Kevin O'Connor | 999567f | 2014-12-31 00:18:05 -0500 | [diff] [blame] | 178 | if options.serial: |
| 179 | readserial(ser, f, byteadjust) |
| 180 | else: |
| 181 | # Read from a pipe |
| 182 | while 1: |
| 183 | ser = os.fdopen(os.open(serialport, os.O_RDONLY|os.O_NONBLOCK), 'rb') |
| 184 | res = readserial(ser, f, byteadjust) |
| 185 | ser.close() |
| 186 | if res < 0: |
| 187 | break |
Kevin O'Connor | 2547bb8 | 2009-04-19 11:04:59 -0400 | [diff] [blame] | 188 | |
| 189 | if __name__ == '__main__': |
| 190 | main() |