summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2006-04-04 21:48:56 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2006-04-04 21:48:56 +0000
commit9743e22c588bbf0a0cf9df22c1d7eac9e15abff7 (patch)
tree9482eb8e9dc022aa0791307966e3675327c196ca
parentbf6bb7d0cd2a870a8baee956108cf7b76cd8f5b3 (diff)
downloadpyserial-git-9743e22c588bbf0a0cf9df22c1d7eac9e15abff7.tar.gz
- cleanup of repr_mode
- allow port, baudrate as parameter instead of options
-rw-r--r--pyserial/examples/miniterm.py74
1 files changed, 60 insertions, 14 deletions
diff --git a/pyserial/examples/miniterm.py b/pyserial/examples/miniterm.py
index 7dd3362..c158a05 100644
--- a/pyserial/examples/miniterm.py
+++ b/pyserial/examples/miniterm.py
@@ -50,7 +50,7 @@ CONVERT_LF = 0
NEWLINE_CONVERISON_MAP = ('\n', '\r', '\r\n')
class Miniterm:
- def __init__(self, port, baudrate, parity, rtscts, xonxoff, echo=False, convert_outgoing=CONVERT_CRLF, repr_mode=False):
+ def __init__(self, port, baudrate, parity, rtscts, xonxoff, echo=False, convert_outgoing=CONVERT_CRLF, repr_mode=0):
self.serial = serial.Serial(port, baudrate, parity=parity, rtscts=rtscts, xonxoff=xonxoff, timeout=0.7)
self.echo = echo
self.repr_mode = repr_mode
@@ -80,13 +80,33 @@ class Miniterm:
"""loop and copy serial->console"""
while self.alive:
data = self.serial.read(1)
- if self.repr_mode:
- sys.stdout.write(repr(data)[1:-1])
- else:
+
+ if self.repr_mode == 0:
+ # direct output, just have to care about newline setting
if data == '\r' and self.convert_outgoing == CONVERT_CR:
sys.stdout.write('\n')
else:
sys.stdout.write(data)
+ elif self.repr_mode == 1:
+ # escape non-printable, let pass newlines
+ if self.convert_outgoing == CONVERT_CRLF and data in '\r\n':
+ if data == '\n':
+ sys.stdout.write('\n')
+ elif data == '\r':
+ pass
+ elif data == '\n' and self.convert_outgoing == CONVERT_LF:
+ sys.stdout.write('\n')
+ elif data == '\r' and self.convert_outgoing == CONVERT_CR:
+ sys.stdout.write('\n')
+ else:
+ sys.stdout.write(repr(data)[1:-1])
+ elif self.repr_mode == 2:
+ # escape all non-printable, including newline
+ sys.stdout.write(repr(data)[1:-1])
+ elif self.repr_mode == 3:
+ # escape everything (hexdump)
+ for character in data:
+ sys.stdout.write("%s " % character.encode('hex'))
sys.stdout.flush()
@@ -115,15 +135,16 @@ def main():
import optparse
parser = optparse.OptionParser(usage="""\
-%prog [options]
+%prog [options] [port [baudrate]]
Miniterm - A simple terminal program for the serial port.""")
parser.add_option("-p", "--port", dest="port",
- help="port, a number (default = 0) or a device name", default=0)
+ help="port, a number (default 0) or a device name (deprecated option)",
+ default=None)
parser.add_option("-b", "--baud", dest="baudrate", action="store", type='int',
- help="set baudrate, default=9600", default=9600)
+ help="set baudrate, default 9600", default=9600)
parser.add_option("", "--parity", dest="parity", action="store",
help="set parity, one of [N, E, O], default=N", default='N')
@@ -143,8 +164,13 @@ Miniterm - A simple terminal program for the serial port.""")
parser.add_option("", "--lf", dest="lf", action="store_true",
help="do not send CR+LF, send LF only", default=False)
- parser.add_option("-D", "--debug", dest="repr_mode", action="store_true",
- help="debug received data (escape nonprintable chars)", default=False)
+ parser.add_option("-D", "--debug", dest="repr_mode", action="count",
+ help="""debug received data (escape non-printable chars)
+--debug can be given multiple times:
+0: just print what is received
+1: escape non-printable characters, do newlines as ususal
+2: escape non-printable characters, newlines too
+3: hex dump everything""", default=0)
parser.add_option("", "--rts", dest="rts_state", action="store", type='int',
help="set initial RTS line state (possible values: 0, 1)", default=None)
@@ -161,8 +187,22 @@ Miniterm - A simple terminal program for the serial port.""")
if options.cr and options.lf:
parser.error("ony one of --cr or --lf can be specified")
+ port = options.port
+ baudrate = options.baudrate
if args:
- parser.error("no arguments are allowed, options only")
+ if options.port is not None:
+ parser.error("no arguments are allowed, options only when --port is given")
+ port = args.pop(0)
+ if args:
+ try:
+ baudrate = int(args[0])
+ except ValueError:
+ parser.error("baudrate must be a number, not %r" % args[0])
+ args.pop(0)
+ if args:
+ parser.error("too many arguments")
+ else:
+ if port is None: port = 0
convert_outgoing = CONVERT_CRLF
if options.cr:
@@ -172,8 +212,8 @@ Miniterm - A simple terminal program for the serial port.""")
try:
miniterm = Miniterm(
- options.port,
- options.baudrate,
+ port,
+ baudrate,
options.parity,
rtscts=options.rtscts,
xonxoff=options.xonxoff,
@@ -182,11 +222,17 @@ Miniterm - A simple terminal program for the serial port.""")
repr_mode=options.repr_mode,
)
except serial.SerialException:
- sys.stderr.write("could not open port %r" % options.port)
+ sys.stderr.write("could not open port %r" % port)
sys.exit(1)
if not options.quiet:
- sys.stderr.write('--- Miniterm on "%s". Type Ctrl-] to quit. ---\n' % miniterm.serial.portstr)
+ sys.stderr.write('--- Miniterm on %s: %d,%s,%s,%s. Type Ctrl-] to quit. ---\n' % (
+ miniterm.serial.portstr,
+ miniterm.serial.baudrate,
+ miniterm.serial.bytesize,
+ miniterm.serial.parity,
+ miniterm.serial.stopbits,
+ ))
if options.dtr_state is not None:
if not options.quiet:
sys.stderr.write('--- forcing DTR %s\n' % (options.dtr_state and 'active' or 'inactive'))