Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: modul/scripts
base: 37689e9c44
...
head fork: modul/scripts
compare: f9dcf43c00
Checking mergeability… Don't worry, you can still create the pull request.
  • 6 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 20, 2012
@modul terminl: tiny fix
print >> port, cmd+eol -- would add another LF
f60e7db
@modul seriallog: build formatter function based on opts d694aa1
@modul seriallog: generating timestamp functions in options
The options will append functions to a list, which is
dynamically evaluated through fmt() to generate the
timestamp strings.
Side-effect: the order of options is mandatory and
determines the order of timestamps in the output.
That’s cool ;)
b45c6b1
@modul seriallog: minor fix --eol: allow lfcr and none c926fa5
@modul seriallog: implemented hex, bin, dec display; v0.3 0612ff6
@modul seriallog: minor printing fixes f9dcf43
Showing with 75 additions and 22 deletions.
  1. +73 −20 seriallog.py
  2. +2 −2 terminal.py
View
93 seriallog.py
@@ -23,7 +23,64 @@
import time
from argparse import FileType, ArgumentParser
-__version__ = "0.2"
+__version__ = "0.3.1"
+
+def mk_date(fmt):
+ ''' Return a wrapper for strftime(fmt) '''
+ def date():
+ return time.strftime(fmt)
+ return date
+
+def mk_tstamp():
+ ''' Return a wrapper for getting a timestamp-string '''
+ def timestamp():
+ return str(int(time.time()))
+ return timestamp
+
+def mk_seconds(start):
+ ''' Return a wrapper to get the str-formatted seconds since 'start' '''
+ def seconds():
+ return "{:6.2f}".format(time.time()-start)
+ return seconds
+
+def hexs(text):
+ ''' Yields hex representation for bytes in text '''
+ for x in bytearray(text):
+ yield "{:02x}".format(x)
+
+def bins(text):
+ ''' Yields binary representation for bytes in text '''
+ for x in bytearray(text):
+ yield "{:08b}".format(x)
+
+def decs(text):
+ ''' Yields decimal representation for bytes in text '''
+ for x in bytearray(text):
+ yield "{: 3d}".format(x)
+
+def formatter(flist=[], conv=None, sep="|"):
+ ''' Build a function that prepends some info strings to a string,
+ which is, if conv is one of hexs, decs or bins, first converted
+ to a hex, decimal or binary dump.
+ Strings to be prepended are retrieved by calling functions in flist and
+ are separated from the base string using 'sep'.
+ '''
+ if not callable(conv):
+ convert = lambda text: text
+ else:
+ convert = lambda text: ' '.join(conv(text))+"\n"
+
+ def pieces(line):
+ for f in flist:
+ if callable(f):
+ yield f()
+ yield sep
+ yield convert(line)
+
+ def _formatter(line):
+ return ' '.join(pieces(line))
+ return _formatter
+
parser = ArgumentParser(description="Continuously read serial data", usage="%(prog)s [options] device")
@@ -36,17 +93,21 @@
parser.add_argument("--logfile", metavar="FILE", type=FileType(mode="w"), help="write output to logfile")
group = parser.add_argument_group(title="Timestamps")
-group.add_argument("--date", action="store_true", help="prepend time and date")
-group.add_argument("--timestamp", action="store_true", help="prepend timestamp")
-group.add_argument("--seconds", action="store_true", help="prepend seconds since start")
+group.add_argument("--date", action="append_const", dest="timestamps", const=mk_date("%Y/%m/%d %H:%M:%S"), help="prepend time and date", default=None)
+group.add_argument("--timestamp", action="append_const", dest="timestamps", const=mk_tstamp(), help="prepend timestamp", default=None)
+group.add_argument("--seconds", action="append_const", dest="timestamps", const=mk_seconds(time.time()), help="prepend seconds since start", default=None)
+
+group = parser.add_argument_group("Format")
+group.add_argument("--hex", dest="converter", action="store_const", const=hexs, default=None, help="display responses as hex")
+group.add_argument("--binary", dest="converter", action="store_const", const=bins, default=None, help="display responses as decimal")
+group.add_argument("--decimal", dest="converter", action="store_const", const=decs, default=None, help="display responses as binary")
group = parser.add_argument_group(title="Sending")
group.add_argument("--send", metavar="CMD", nargs="+", help="send serial commands, then read")
-group.add_argument("--eol", default="lf", choices=["lf", "crlf", "cr"], help="choose end of line characters")
-
+group.add_argument("--eol", default="lf", choices=["lf", "crlf", "cr", "lfcr", "none"], help="choose end of line characters (lf)")
args = parser.parse_args()
-eol = args.eol.replace("lf", "\n").replace("cr", "\r")
+eol = args.eol.replace("lf", "\n").replace("cr", "\r").replace("none", "")
try:
port = serial.Serial(args.device, args.baudrate, timeout=args.interval)
@@ -58,34 +119,26 @@
if args.logfile:
print "opened %s for logging" % (args.logfile.name)
-
-fmt = args.timestamp and "{timestamp} " or ""
-fmt += args.date and "{date} " or ""
-fmt += args.seconds and "{sec: 6.2f} " or ""
-fmt += fmt and "| {line}" or "{line}"
+fmt = formatter(args.timestamps, args.converter)
try:
- tstart = time.time()
while 1:
if args.send:
for cmd in args.send:
- port.write(cmd+eol)
+ print >> port, cmd+eol,
incoming = port.readlines()
if not incoming:
continue
for line in incoming:
- buf = fmt.format(line=line, \
- timestamp=int(time.time()), \
- date=time.strftime("%Y/%m/%d %H:%M:%S"), \
- sec=time.time()-tstart)
+ buf = fmt(line).strip()
if not args.quiet:
- print buf,
+ print buf
if args.logfile:
- print >> args.logfile, buf,
+ print >> args.logfile, buf
args.logfile.flush()
except KeyboardInterrupt:
View
4 terminal.py
@@ -138,7 +138,7 @@ def prompt():
if args.commands:
for cmd in args.commands:
- print >> port, cmd + args.eol
+ print >> port, cmd + args.eol,
incoming = ''.join(port.readlines())
if incoming:
@@ -165,7 +165,7 @@ def prompt():
try:
cmd = prompt()
if cmd:
- print >> port, cmd + args.eol
+ print >> port, cmd + args.eol,
if args.logfile:
print >> args.logfile, '>', cmd
except EOFError:

No commit comments for this range

Something went wrong with that request. Please try again.