summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Liechti <cliechti@gmx.net>2015-09-01 02:33:13 +0200
committerChris Liechti <cliechti@gmx.net>2015-09-01 02:33:13 +0200
commit89313c9c1e14db11beb244dbd10e16015674c729 (patch)
tree2ab118ce7a31c4fbfcb02b7f7e8505545250dab0
parent1c3249fc5183d59be07fcc4bc3f548733fcd6e15 (diff)
downloadpyserial-git-89313c9c1e14db11beb244dbd10e16015674c729.tar.gz
miniterm: improve port list, allow selection by index, fix port switching
-rw-r--r--serial/tools/miniterm.py56
1 files changed, 41 insertions, 15 deletions
diff --git a/serial/tools/miniterm.py b/serial/tools/miniterm.py
index 711edaf..20615e3 100644
--- a/serial/tools/miniterm.py
+++ b/serial/tools/miniterm.py
@@ -278,15 +278,38 @@ TRANSFORMATIONS = {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-def dump_port_list():
- if comports:
- sys.stderr.write('\n--- Available ports:\n')
- for port, desc, hwid in sorted(comports()):
- #~ sys.stderr.write('--- %-20s %s [%s]\n' % (port, desc, hwid))
- sys.stderr.write('--- %-20s %s\n' % (port, desc))
+def ask_for_port():
+ """\
+ Show a list of ports and ask the user for a choice. To make selection
+ easier on systems with long device names, also allow the input of an
+ index.
+ """
+ sys.stderr.write('\n--- Available ports:\n')
+ ports = []
+ for n, (port, desc, hwid) in enumerate(sorted(comports()), 1):
+ #~ sys.stderr.write('--- %-20s %s [%s]\n' % (port, desc, hwid))
+ sys.stderr.write('--- {:2}: {:20} {}\n'.format(n, port, desc))
+ ports.append(port)
+ while True:
+ port = raw_input('--- Enter port index or full name: ')
+ try:
+ index = int(port) - 1
+ if not 0 <= index < len(ports):
+ sys.stderr.write('--- Invalid index!\n')
+ continue
+ except ValueError:
+ pass
+ else:
+ port = ports[index]
+ return port
class Miniterm(object):
+ """\
+ Terminal application. Copy data from serial port to console and vice versa.
+ Handle special keys from the console to show menu etc.
+ """
+
def __init__(self, serial_instance, echo=False, eol='crlf', filters=()):
self.console = Console()
self.serial = serial_instance
@@ -511,12 +534,9 @@ class Miniterm(object):
#~ elif c == '\x01': # CTRL+A -> cycle escape mode
#~ elif c == '\x0c': # CTRL+L -> cycle linefeed mode
elif c in 'pP': # P -> change port
- dump_port_list()
- sys.stderr.write('--- Enter port name: ')
- sys.stderr.flush()
with self.console:
try:
- port = sys.stdin.readline().strip()
+ port = ask_for_port()
except KeyboardInterrupt:
port = None
if port and port != self.serial.port:
@@ -528,10 +548,10 @@ class Miniterm(object):
new_serial = serial.serial_for_url(port, do_not_open=True)
# restore settings and open
new_serial.applySettingsDict(settings)
+ new_serial.rts = self.serial.rts
+ new_serial.dtr = self.serial.dtr
new_serial.open()
- new_serial.setRTS(self.rts_state)
- new_serial.setDTR(self.dtr_state)
- new_serial.setBreak(self.break_state)
+ new_serial.break_condition = self.serial.break_condition
except Exception as e:
sys.stderr.write('--- ERROR opening new port: {} ---\n'.format(e))
new_serial.close()
@@ -763,8 +783,14 @@ def main(default_port=None, default_baudrate=9600, default_rts=None, default_dtr
# no port given on command line -> ask user now
if args.port is None:
- dump_port_list()
- args.port = raw_input('Enter port name:')
+ try:
+ args.port = ask_for_port()
+ except KeyboardInterrupt:
+ sys.stderr.write('\n')
+ parser.error('user aborted and port is not given')
+ else:
+ if not args.port:
+ parser.error('port is not given')
if args.filter:
if 'help' in args.filter: