From 2e2526ecf130de5fe754f8f65c0513663b389a44 Mon Sep 17 00:00:00 2001 From: cliechti Date: Sun, 13 Oct 2013 03:08:19 +0000 Subject: improve --help message of miniterm. fix console setup on posix, so that the question for the port name is made before the echo is switched off. git-svn-id: http://svn.code.sf.net/p/pyserial/code/trunk/pyserial@474 f19166aa-fa4f-0410-85c2-fa1106f25c8a --- documentation/examples.rst | 39 ++++++++++------- serial/tools/miniterm.py | 102 +++++++++++++++++++++++++++------------------ 2 files changed, 86 insertions(+), 55 deletions(-) diff --git a/documentation/examples.rst b/documentation/examples.rst index 39f238c..4ea35ed 100644 --- a/documentation/examples.rst +++ b/documentation/examples.rst @@ -35,31 +35,40 @@ Command line options ``python -m serial.tools.miniterm -h``:: Options: -h, --help show this help message and exit - -p PORT, --port=PORT port, a number (default 0) or a device name - (deprecated option) - -b BAUDRATE, --baud=BAUDRATE + + Port settings: + -p PORT, --port=PORT + port, a number or a device name. (deprecated option, + use parameter instead) + -b BAUDRATE, --baud=BAUDRATE set baud rate, default 9600 - --parity=PARITY set parity, one of [N, E, O, S, M], default=N - -e, --echo enable local echo (default off) - --rtscts enable RTS/CTS flow control (default off) - --xonxoff enable software flow control (default off) - --cr do not send CR+LF, send CR only - --lf do not send CR+LF, send LF only - -D, --debug debug received data (escape non-printable chars) + --parity=PARITY set parity, one of [N, E, O, S, M], default=N + --rtscts enable RTS/CTS flow control (default off) + --xonxoff enable software flow control (default off) + --rts=RTS_STATE set initial RTS line state (possible values: 0, 1) + --dtr=DTR_STATE set initial DTR line state (possible values: 0, 1) + + Data handling: + -e, --echo enable local echo (default off) + --cr do not send CR+LF, send CR only + --lf do not send CR+LF, send LF only + -D, --debug 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 unusual 2: escape non-printable characters, newlines too 3: hex dump everything - --rts=RTS_STATE set initial RTS line state (possible values: 0, 1) - --dtr=DTR_STATE set initial DTR line state (possible values: 0, 1) - -q, --quiet suppress non error messages - --exit-char=EXIT_CHAR + + Hotkeys: + --exit-char=EXIT_CHAR ASCII code of special character that is used to exit the application - --menu-char=MENU_CHAR + --menu-char=MENU_CHAR ASCII code of special character that is used to control miniterm (menu) + Diagnostics: + -q, --quiet suppress non-error messages + Miniterm supports some control functions. Typing :kbd:`Ctrl+T Ctrl+H` when it is running shows the help text:: diff --git a/serial/tools/miniterm.py b/serial/tools/miniterm.py index 626c5aa..6d27ba6 100644 --- a/serial/tools/miniterm.py +++ b/serial/tools/miniterm.py @@ -113,6 +113,7 @@ elif os.name == 'posix': class Console(object): def __init__(self): self.fd = sys.stdin.fileno() + self.old = None def setup(self): self.old = termios.tcgetattr(self.fd) @@ -127,14 +128,14 @@ elif os.name == 'posix': return c def cleanup(self): - termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old) + if self.old is not None: + termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old) console = Console() def cleanup_console(): console.cleanup() - console.setup() sys.exitfunc = cleanup_console # terminal modes have to be restored on exit... else: @@ -462,13 +463,15 @@ def main(): description = "Miniterm - A simple terminal program for the serial port." ) - parser.add_option("-p", "--port", + group = optparse.OptionGroup(parser, "Port settings") + + group.add_option("-p", "--port", dest = "port", help = "port, a number or a device name. (deprecated option, use parameter instead)", default = DEFAULT_PORT ) - parser.add_option("-b", "--baud", + group.add_option("-b", "--baud", dest = "baudrate", action = "store", type = 'int', @@ -476,49 +479,69 @@ def main(): default = DEFAULT_BAUDRATE ) - parser.add_option("--parity", + group.add_option("--parity", dest = "parity", action = "store", help = "set parity, one of [N, E, O, S, M], default=N", default = 'N' ) - parser.add_option("-e", "--echo", - dest = "echo", - action = "store_true", - help = "enable local echo (default off)", - default = False - ) - - parser.add_option("--rtscts", + group.add_option("--rtscts", dest = "rtscts", action = "store_true", help = "enable RTS/CTS flow control (default off)", default = False ) - parser.add_option("--xonxoff", + group.add_option("--xonxoff", dest = "xonxoff", action = "store_true", help = "enable software flow control (default off)", default = False ) - parser.add_option("--cr", + group.add_option("--rts", + dest = "rts_state", + action = "store", + type = 'int', + help = "set initial RTS line state (possible values: 0, 1)", + default = DEFAULT_RTS + ) + + group.add_option("--dtr", + dest = "dtr_state", + action = "store", + type = 'int', + help = "set initial DTR line state (possible values: 0, 1)", + default = DEFAULT_DTR + ) + + parser.add_option_group(group) + + group = optparse.OptionGroup(parser, "Data handling") + + group.add_option("-e", "--echo", + dest = "echo", + action = "store_true", + help = "enable local echo (default off)", + default = False + ) + + group.add_option("--cr", dest = "cr", action = "store_true", help = "do not send CR+LF, send CR only", default = False ) - parser.add_option("--lf", + group.add_option("--lf", dest = "lf", action = "store_true", help = "do not send CR+LF, send LF only", default = False ) - parser.add_option("-D", "--debug", + group.add_option("-D", "--debug", dest = "repr_mode", action = "count", help = """debug received data (escape non-printable chars) @@ -530,30 +553,12 @@ def main(): default = 0 ) - parser.add_option("--rts", - dest = "rts_state", - action = "store", - type = 'int', - help = "set initial RTS line state (possible values: 0, 1)", - default = DEFAULT_RTS - ) + parser.add_option_group(group) - parser.add_option("--dtr", - dest = "dtr_state", - action = "store", - type = 'int', - help = "set initial DTR line state (possible values: 0, 1)", - default = DEFAULT_DTR - ) - parser.add_option("-q", "--quiet", - dest = "quiet", - action = "store_true", - help = "suppress non error messages", - default = False - ) + group = optparse.OptionGroup(parser, "Hotkeys") - parser.add_option("--exit-char", + group.add_option("--exit-char", dest = "exit_char", action = "store", type = 'int', @@ -561,7 +566,7 @@ def main(): default = 0x1d ) - parser.add_option("--menu-char", + group.add_option("--menu-char", dest = "menu_char", action = "store", type = 'int', @@ -569,6 +574,20 @@ def main(): default = 0x14 ) + parser.add_option_group(group) + + group = optparse.OptionGroup(parser, "Diagnostics") + + group.add_option("-q", "--quiet", + dest = "quiet", + action = "store_true", + help = "suppress non-error messages", + default = False + ) + + parser.add_option_group(group) + + (options, args) = parser.parse_args() options.parity = options.parity.upper() @@ -600,6 +619,7 @@ def main(): if args: parser.error("too many arguments") else: + # noport given on command line -> ask user now if port is None: dump_port_list() port = raw_input('Enter port name:') @@ -651,6 +671,7 @@ def main(): miniterm.serial.setRTS(options.rts_state) miniterm.rts_state = options.rts_state + console.setup() miniterm.start() try: miniterm.join(True) @@ -659,7 +680,8 @@ def main(): if not options.quiet: sys.stderr.write("\n--- exit ---\n") miniterm.join() + #~ console.cleanup() - +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if __name__ == '__main__': main() -- cgit v1.2.1