aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2009-04-19 11:04:59 -0400
committerKevin O'Connor <kevin@koconnor.net>2009-04-19 11:04:59 -0400
commit2547bb896233fe2c3b9db20a0f0ecf90fad94631 (patch)
tree6faee76b605c4aa93d642fc31c8c0d34cf6e799e /tools
parentff8c141bfd6f2abd8078626dfb76ecca3ed4f42b (diff)
downloadseabios-2547bb896233fe2c3b9db20a0f0ecf90fad94631.tar.gz
Add new tool - tools/readserial.py.
This tool can read from a serial port and provide timing information on each line read. It is useful when analyzing serial output from SeaBIOS.
Diffstat (limited to 'tools')
-rwxr-xr-xtools/readserial.py101
1 files changed, 101 insertions, 0 deletions
diff --git a/tools/readserial.py b/tools/readserial.py
new file mode 100755
index 00000000..8b20fd27
--- /dev/null
+++ b/tools/readserial.py
@@ -0,0 +1,101 @@
+#!/usr/bin/env python
+# Script that can read from a serial device and show timestamps.
+#
+# Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
+#
+# This file may be distributed under the terms of the GNU GPLv3 license.
+
+# Usage:
+# tools/readserial.py /dev/ttyUSB0 115200
+
+import sys
+import time
+import select
+import serial
+
+# Reset time counter after this much idle time.
+RESTARTINTERVAL = 60
+# Alter timing reports based on how much time would be spent writing
+# to serial.
+ADJUSTBAUD = 1
+
+def readserial(infile, logfile, baudrate):
+ starttime = 0
+ isnewline = 1
+ while 1:
+ # Read data
+ try:
+ res = select.select([infile, sys.stdin], [], [])
+ except KeyboardInterrupt:
+ sys.stdout.write("\n")
+ break
+ if sys.stdin in res[0]:
+ # Got keyboard input - force reset on next serial input
+ sys.stdin.read(1)
+ starttime = 0
+ if len(res[0]) == 1:
+ continue
+ curtime = time.time()
+ d = infile.read(4096)
+
+ # Reset start time if no data for some time
+ if curtime - starttime > RESTARTINTERVAL:
+ starttime = curtime
+ charcount = 0
+ isnewline = 1
+ sys.stdout.write("\n")
+ logfile.write("\n")
+
+ # Translate unprintable chars; add timestamps
+ out = ""
+ for c in d:
+ if isnewline:
+ delta = curtime - starttime
+ if ADJUSTBAUD:
+ delta -= float(charcount * 9) / baudrate
+ out += "%06.3f: " % delta
+ isnewline = 0
+ oc = ord(c)
+ charcount += 1
+ if oc == 0x0d:
+ continue
+ if oc == 0x00:
+ out += "<00>\n"
+ isnewline = 1
+ continue
+ if oc == 0x0a:
+ out += "\n"
+ isnewline = 1
+ continue
+ if oc < 0x20 or oc >= 0x7f and oc != 0x09:
+ out += "<%02x>" % oc
+ continue
+ out += c
+
+ sys.stdout.write(out)
+ sys.stdout.flush()
+ logfile.write(out)
+ logfile.flush()
+
+def printUsage():
+ print "Usage:\n %s [<serialdevice> [<baud>]]" % (sys.argv[0],)
+ sys.exit(1)
+
+def main():
+ serialport = 0
+ baud = 115200
+ if len(sys.argv) > 3:
+ printUsage()
+ if len(sys.argv) > 1:
+ serialport = sys.argv[1]
+ if len(sys.argv) > 2:
+ baud = int(sys.argv[2])
+
+ ser = serial.Serial(serialport, baud, timeout=0)
+
+ logname = time.strftime("seriallog-%Y%m%d_%H%M%S.log")
+ f = open(logname, 'wb')
+ readserial(ser, f, baud)
+
+if __name__ == '__main__':
+ main()