cliechti | 576de25 | 2002-02-28 23:54:44 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 2 | # |
cliechti | a128a70 | 2004-07-21 22:13:31 +0000 | [diff] [blame] | 3 | # Very simple serial terminal |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 4 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 5 | # This file is part of pySerial. https://github.com/pyserial/pyserial |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 6 | # (C)2002-2017 Chris Liechti <cliechti@gmx.net> |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 7 | # |
| 8 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | fc9eb38 | 2002-03-05 01:12:29 +0000 | [diff] [blame] | 9 | |
Kurt McKee | 057387c | 2018-02-07 22:10:38 -0600 | [diff] [blame] | 10 | from __future__ import absolute_import |
| 11 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 12 | import codecs |
Chris Liechti | a1d5c6d | 2015-08-07 14:41:24 +0200 | [diff] [blame] | 13 | import os |
| 14 | import sys |
| 15 | import threading |
cliechti | 576de25 | 2002-02-28 23:54:44 +0000 | [diff] [blame] | 16 | |
Chris Liechti | a1d5c6d | 2015-08-07 14:41:24 +0200 | [diff] [blame] | 17 | import serial |
Chris Liechti | 55ba7d9 | 2015-08-15 16:33:51 +0200 | [diff] [blame] | 18 | from serial.tools.list_ports import comports |
Chris Liechti | 168704f | 2015-09-30 16:50:29 +0200 | [diff] [blame] | 19 | from serial.tools import hexlify_codec |
| 20 | |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 21 | # pylint: disable=wrong-import-order,wrong-import-position |
| 22 | |
Chris Liechti | 168704f | 2015-09-30 16:50:29 +0200 | [diff] [blame] | 23 | codecs.register(lambda c: hexlify_codec.getregentry() if c == 'hexlify' else None) |
Chris Liechti | a1d5c6d | 2015-08-07 14:41:24 +0200 | [diff] [blame] | 24 | |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 25 | try: |
| 26 | raw_input |
| 27 | except NameError: |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 28 | # pylint: disable=redefined-builtin,invalid-name |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 29 | raw_input = input # in python3 it's "raw" |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 30 | unichr = chr |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 31 | |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 32 | |
| 33 | def key_description(character): |
| 34 | """generate a readable description for a key""" |
| 35 | ascii_code = ord(character) |
| 36 | if ascii_code < 32: |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame] | 37 | return 'Ctrl+{:c}'.format(ord('@') + ascii_code) |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 38 | else: |
| 39 | return repr(character) |
| 40 | |
cliechti | 9116553 | 2011-03-18 02:02:52 +0000 | [diff] [blame] | 41 | |
Chris Liechti | 9a72085 | 2015-08-25 00:20:38 +0200 | [diff] [blame] | 42 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 43 | class ConsoleBase(object): |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 44 | """OS abstraction for console (input/output codec, no echo)""" |
| 45 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 46 | def __init__(self): |
| 47 | if sys.version_info >= (3, 0): |
| 48 | self.byte_output = sys.stdout.buffer |
| 49 | else: |
| 50 | self.byte_output = sys.stdout |
| 51 | self.output = sys.stdout |
cliechti | f467aa8 | 2013-10-13 21:36:49 +0000 | [diff] [blame] | 52 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 53 | def setup(self): |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 54 | """Set console to read single characters, no echo""" |
cliechti | f467aa8 | 2013-10-13 21:36:49 +0000 | [diff] [blame] | 55 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 56 | def cleanup(self): |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 57 | """Restore default console settings""" |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 58 | |
| 59 | def getkey(self): |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 60 | """Read a single key from the console""" |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 61 | return None |
| 62 | |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 63 | def write_bytes(self, byte_string): |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 64 | """Write bytes (already encoded)""" |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 65 | self.byte_output.write(byte_string) |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 66 | self.byte_output.flush() |
| 67 | |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 68 | def write(self, text): |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 69 | """Write string""" |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 70 | self.output.write(text) |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 71 | self.output.flush() |
| 72 | |
Chris Liechti | 1eb3f6b | 2016-04-27 02:12:50 +0200 | [diff] [blame] | 73 | def cancel(self): |
| 74 | """Cancel getkey operation""" |
| 75 | |
Chris Liechti | 269f77b | 2015-08-24 01:31:42 +0200 | [diff] [blame] | 76 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 77 | # context manager: |
| 78 | # switch terminal temporary to normal mode (e.g. to get user input) |
| 79 | |
| 80 | def __enter__(self): |
| 81 | self.cleanup() |
| 82 | return self |
| 83 | |
| 84 | def __exit__(self, *args, **kwargs): |
| 85 | self.setup() |
| 86 | |
cliechti | 9c592b3 | 2008-06-16 22:00:14 +0000 | [diff] [blame] | 87 | |
Chris Liechti | ba45c52 | 2016-02-06 23:53:23 +0100 | [diff] [blame] | 88 | if os.name == 'nt': # noqa |
cliechti | 576de25 | 2002-02-28 23:54:44 +0000 | [diff] [blame] | 89 | import msvcrt |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 90 | import ctypes |
Cefn Hoile | d64fb60 | 2018-06-08 09:46:28 +0100 | [diff] [blame^] | 91 | import ctypes.wintypes as wintypes |
| 92 | import platform |
Chris Liechti | 9cc696b | 2015-08-28 00:54:22 +0200 | [diff] [blame] | 93 | |
| 94 | class Out(object): |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 95 | """file-like wrapper that uses os.write""" |
| 96 | |
Chris Liechti | 9cc696b | 2015-08-28 00:54:22 +0200 | [diff] [blame] | 97 | def __init__(self, fd): |
| 98 | self.fd = fd |
| 99 | |
| 100 | def flush(self): |
| 101 | pass |
| 102 | |
| 103 | def write(self, s): |
| 104 | os.write(self.fd, s) |
| 105 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 106 | class Console(ConsoleBase): |
Cefn Hoile | d64fb60 | 2018-06-08 09:46:28 +0100 | [diff] [blame^] | 107 | nav = { |
| 108 | 'H': '\x1b[A', # UP |
| 109 | 'P': '\x1b[B', # DOWN |
| 110 | 'K': '\x1b[D', # LEFT |
| 111 | 'M': '\x1b[C', # RIGHT |
| 112 | 'G': '\x1b[H', # HOME |
| 113 | 'O': '\x1b[F', # END |
| 114 | } |
| 115 | |
Chris Liechti | cbb00b2 | 2015-08-13 22:58:49 +0200 | [diff] [blame] | 116 | def __init__(self): |
| 117 | super(Console, self).__init__() |
Cefn Hoile | d64fb60 | 2018-06-08 09:46:28 +0100 | [diff] [blame^] | 118 | if not hasattr(wintypes, 'LPDWORD'): # PY2 |
| 119 | wintypes.LPDWORD = ctypes.POINTER(wintypes.DWORD) |
| 120 | self._saved_cm = mode = wintypes.DWORD() |
| 121 | ctypes.windll.kernel32.GetConsoleMode(ctypes.windll.kernel32.GetStdHandle(-11), ctypes.byref(self._saved_cm)) |
Chris Liechti | 1df2827 | 2015-08-27 23:37:38 +0200 | [diff] [blame] | 122 | self._saved_ocp = ctypes.windll.kernel32.GetConsoleOutputCP() |
| 123 | self._saved_icp = ctypes.windll.kernel32.GetConsoleCP() |
Chris Liechti | cbb00b2 | 2015-08-13 22:58:49 +0200 | [diff] [blame] | 124 | ctypes.windll.kernel32.SetConsoleOutputCP(65001) |
| 125 | ctypes.windll.kernel32.SetConsoleCP(65001) |
Cefn Hoile | d64fb60 | 2018-06-08 09:46:28 +0100 | [diff] [blame^] | 126 | # ANSI handling available through SetConsoleMode since v1511 |
| 127 | if platform.release() == '10' and int(platform.version().split('.')[2]) > 10586: |
| 128 | ctypes.windll.kernel32.SetConsoleMode(ctypes.windll.kernel32.GetStdHandle(-11), 0x7) |
Chris Liechti | 9cc696b | 2015-08-28 00:54:22 +0200 | [diff] [blame] | 129 | self.output = codecs.getwriter('UTF-8')(Out(sys.stdout.fileno()), 'replace') |
| 130 | # the change of the code page is not propagated to Python, manually fix it |
| 131 | sys.stderr = codecs.getwriter('UTF-8')(Out(sys.stderr.fileno()), 'replace') |
| 132 | sys.stdout = self.output |
Chris Liechti | 168704f | 2015-09-30 16:50:29 +0200 | [diff] [blame] | 133 | self.output.encoding = 'UTF-8' # needed for input |
Chris Liechti | cbb00b2 | 2015-08-13 22:58:49 +0200 | [diff] [blame] | 134 | |
Chris Liechti | 1df2827 | 2015-08-27 23:37:38 +0200 | [diff] [blame] | 135 | def __del__(self): |
| 136 | ctypes.windll.kernel32.SetConsoleOutputCP(self._saved_ocp) |
| 137 | ctypes.windll.kernel32.SetConsoleCP(self._saved_icp) |
Cefn Hoile | d64fb60 | 2018-06-08 09:46:28 +0100 | [diff] [blame^] | 138 | ctypes.windll.kernel32.SetConsoleMode(ctypes.windll.kernel32.GetStdHandle(-11), self._saved_cm) |
Chris Liechti | 1df2827 | 2015-08-27 23:37:38 +0200 | [diff] [blame] | 139 | |
cliechti | 3a8bf09 | 2008-09-17 11:26:53 +0000 | [diff] [blame] | 140 | def getkey(self): |
cliechti | 9116553 | 2011-03-18 02:02:52 +0000 | [diff] [blame] | 141 | while True: |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 142 | z = msvcrt.getwch() |
Chris Liechti | 9f39881 | 2015-09-13 18:50:44 +0200 | [diff] [blame] | 143 | if z == unichr(13): |
| 144 | return unichr(10) |
Cefn Hoile | d64fb60 | 2018-06-08 09:46:28 +0100 | [diff] [blame^] | 145 | elif z in unichr(0): # functions keys, ignore |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 146 | msvcrt.getwch() |
Cefn Hoile | d64fb60 | 2018-06-08 09:46:28 +0100 | [diff] [blame^] | 147 | elif z in unichr(0xe0): # map special keys |
| 148 | code = msvcrt.getwch() |
| 149 | try: |
| 150 | return self.nav[code] |
| 151 | except KeyError: |
| 152 | pass |
cliechti | 9c592b3 | 2008-06-16 22:00:14 +0000 | [diff] [blame] | 153 | else: |
cliechti | 9c592b3 | 2008-06-16 22:00:14 +0000 | [diff] [blame] | 154 | return z |
cliechti | 53edb47 | 2009-02-06 21:18:46 +0000 | [diff] [blame] | 155 | |
Chris Liechti | 1eb3f6b | 2016-04-27 02:12:50 +0200 | [diff] [blame] | 156 | def cancel(self): |
Chris Liechti | c20c373 | 2016-05-14 02:25:13 +0200 | [diff] [blame] | 157 | # CancelIo, CancelSynchronousIo do not seem to work when using |
| 158 | # getwch, so instead, send a key to the window with the console |
Chris Liechti | 1eb3f6b | 2016-04-27 02:12:50 +0200 | [diff] [blame] | 159 | hwnd = ctypes.windll.kernel32.GetConsoleWindow() |
| 160 | ctypes.windll.user32.PostMessageA(hwnd, 0x100, 0x0d, 0) |
| 161 | |
cliechti | 576de25 | 2002-02-28 23:54:44 +0000 | [diff] [blame] | 162 | elif os.name == 'posix': |
Chris Liechti | a1d5c6d | 2015-08-07 14:41:24 +0200 | [diff] [blame] | 163 | import atexit |
| 164 | import termios |
Chris Liechti | cab3dab | 2016-12-07 01:27:41 +0100 | [diff] [blame] | 165 | import fcntl |
Chris Liechti | 9cc696b | 2015-08-28 00:54:22 +0200 | [diff] [blame] | 166 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 167 | class Console(ConsoleBase): |
cliechti | 9c592b3 | 2008-06-16 22:00:14 +0000 | [diff] [blame] | 168 | def __init__(self): |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 169 | super(Console, self).__init__() |
cliechti | 9c592b3 | 2008-06-16 22:00:14 +0000 | [diff] [blame] | 170 | self.fd = sys.stdin.fileno() |
Chris Liechti | 4d989c2 | 2015-08-24 00:24:49 +0200 | [diff] [blame] | 171 | self.old = termios.tcgetattr(self.fd) |
Chris Liechti | 89eb247 | 2015-08-08 17:06:25 +0200 | [diff] [blame] | 172 | atexit.register(self.cleanup) |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 173 | if sys.version_info < (3, 0): |
Chris Liechti | a7e7b69 | 2015-08-25 21:10:28 +0200 | [diff] [blame] | 174 | self.enc_stdin = codecs.getreader(sys.stdin.encoding)(sys.stdin) |
| 175 | else: |
| 176 | self.enc_stdin = sys.stdin |
cliechti | 9c592b3 | 2008-06-16 22:00:14 +0000 | [diff] [blame] | 177 | |
| 178 | def setup(self): |
cliechti | 9c592b3 | 2008-06-16 22:00:14 +0000 | [diff] [blame] | 179 | new = termios.tcgetattr(self.fd) |
| 180 | new[3] = new[3] & ~termios.ICANON & ~termios.ECHO & ~termios.ISIG |
| 181 | new[6][termios.VMIN] = 1 |
| 182 | new[6][termios.VTIME] = 0 |
| 183 | termios.tcsetattr(self.fd, termios.TCSANOW, new) |
cliechti | 53edb47 | 2009-02-06 21:18:46 +0000 | [diff] [blame] | 184 | |
cliechti | 9c592b3 | 2008-06-16 22:00:14 +0000 | [diff] [blame] | 185 | def getkey(self): |
Chris Liechti | a7e7b69 | 2015-08-25 21:10:28 +0200 | [diff] [blame] | 186 | c = self.enc_stdin.read(1) |
Chris Liechti | 9f39881 | 2015-09-13 18:50:44 +0200 | [diff] [blame] | 187 | if c == unichr(0x7f): |
| 188 | c = unichr(8) # map the BS key (which yields DEL) to backspace |
Chris Liechti | 9a72085 | 2015-08-25 00:20:38 +0200 | [diff] [blame] | 189 | return c |
cliechti | 53edb47 | 2009-02-06 21:18:46 +0000 | [diff] [blame] | 190 | |
Chris Liechti | 16a8b5e | 2016-05-09 22:46:06 +0200 | [diff] [blame] | 191 | def cancel(self): |
Chris Liechti | cab3dab | 2016-12-07 01:27:41 +0100 | [diff] [blame] | 192 | fcntl.ioctl(self.fd, termios.TIOCSTI, b'\0') |
Chris Liechti | 16a8b5e | 2016-05-09 22:46:06 +0200 | [diff] [blame] | 193 | |
cliechti | 9c592b3 | 2008-06-16 22:00:14 +0000 | [diff] [blame] | 194 | def cleanup(self): |
Chris Liechti | 4d989c2 | 2015-08-24 00:24:49 +0200 | [diff] [blame] | 195 | termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old) |
cliechti | 9c592b3 | 2008-06-16 22:00:14 +0000 | [diff] [blame] | 196 | |
cliechti | 576de25 | 2002-02-28 23:54:44 +0000 | [diff] [blame] | 197 | else: |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 198 | raise NotImplementedError( |
| 199 | 'Sorry no implementation for your platform ({}) available.'.format(sys.platform)) |
cliechti | 576de25 | 2002-02-28 23:54:44 +0000 | [diff] [blame] | 200 | |
cliechti | 6fa76fb | 2009-07-08 23:53:39 +0000 | [diff] [blame] | 201 | |
Chris Liechti | 9a72085 | 2015-08-25 00:20:38 +0200 | [diff] [blame] | 202 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 203 | |
| 204 | class Transform(object): |
Chris Liechti | cbb00b2 | 2015-08-13 22:58:49 +0200 | [diff] [blame] | 205 | """do-nothing: forward all data unchanged""" |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 206 | def rx(self, text): |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 207 | """text received from serial port""" |
| 208 | return text |
| 209 | |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 210 | def tx(self, text): |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 211 | """text to be sent to serial port""" |
| 212 | return text |
| 213 | |
| 214 | def echo(self, text): |
| 215 | """text to be sent but displayed on console""" |
| 216 | return text |
| 217 | |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 218 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 219 | class CRLF(Transform): |
| 220 | """ENTER sends CR+LF""" |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 221 | |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 222 | def tx(self, text): |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 223 | return text.replace('\n', '\r\n') |
| 224 | |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 225 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 226 | class CR(Transform): |
| 227 | """ENTER sends CR""" |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 228 | |
| 229 | def rx(self, text): |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 230 | return text.replace('\r', '\n') |
| 231 | |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 232 | def tx(self, text): |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 233 | return text.replace('\n', '\r') |
| 234 | |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 235 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 236 | class LF(Transform): |
| 237 | """ENTER sends LF""" |
| 238 | |
| 239 | |
| 240 | class NoTerminal(Transform): |
| 241 | """remove typical terminal control codes from input""" |
Chris Liechti | 9a72085 | 2015-08-25 00:20:38 +0200 | [diff] [blame] | 242 | |
| 243 | REPLACEMENT_MAP = dict((x, 0x2400 + x) for x in range(32) if unichr(x) not in '\r\n\b\t') |
Chris Liechti | ba45c52 | 2016-02-06 23:53:23 +0100 | [diff] [blame] | 244 | REPLACEMENT_MAP.update( |
| 245 | { |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 246 | 0x7F: 0x2421, # DEL |
| 247 | 0x9B: 0x2425, # CSI |
Chris Liechti | ba45c52 | 2016-02-06 23:53:23 +0100 | [diff] [blame] | 248 | }) |
Chris Liechti | 9a72085 | 2015-08-25 00:20:38 +0200 | [diff] [blame] | 249 | |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 250 | def rx(self, text): |
Chris Liechti | 9a72085 | 2015-08-25 00:20:38 +0200 | [diff] [blame] | 251 | return text.translate(self.REPLACEMENT_MAP) |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 252 | |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 253 | echo = rx |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 254 | |
| 255 | |
Chris Liechti | 9a72085 | 2015-08-25 00:20:38 +0200 | [diff] [blame] | 256 | class NoControls(NoTerminal): |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 257 | """Remove all control codes, incl. CR+LF""" |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 258 | |
Chris Liechti | 9a72085 | 2015-08-25 00:20:38 +0200 | [diff] [blame] | 259 | REPLACEMENT_MAP = dict((x, 0x2400 + x) for x in range(32)) |
Chris Liechti | ba45c52 | 2016-02-06 23:53:23 +0100 | [diff] [blame] | 260 | REPLACEMENT_MAP.update( |
| 261 | { |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 262 | 0x20: 0x2423, # visual space |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 263 | 0x7F: 0x2421, # DEL |
| 264 | 0x9B: 0x2425, # CSI |
Chris Liechti | ba45c52 | 2016-02-06 23:53:23 +0100 | [diff] [blame] | 265 | }) |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 266 | |
| 267 | |
| 268 | class Printable(Transform): |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 269 | """Show decimal code for all non-ASCII characters and replace most control codes""" |
Chris Liechti | c0c660a | 2015-08-25 00:55:51 +0200 | [diff] [blame] | 270 | |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 271 | def rx(self, text): |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 272 | r = [] |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 273 | for c in text: |
| 274 | if ' ' <= c < '\x7f' or c in '\r\n\b\t': |
| 275 | r.append(c) |
| 276 | elif c < ' ': |
| 277 | r.append(unichr(0x2400 + ord(c))) |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 278 | else: |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 279 | r.extend(unichr(0x2080 + ord(d) - 48) for d in '{:d}'.format(ord(c))) |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 280 | r.append(' ') |
| 281 | return ''.join(r) |
| 282 | |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 283 | echo = rx |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 284 | |
| 285 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 286 | class Colorize(Transform): |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 287 | """Apply different colors for received and echo""" |
Chris Liechti | c0c660a | 2015-08-25 00:55:51 +0200 | [diff] [blame] | 288 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 289 | def __init__(self): |
| 290 | # XXX make it configurable, use colorama? |
| 291 | self.input_color = '\x1b[37m' |
| 292 | self.echo_color = '\x1b[31m' |
| 293 | |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 294 | def rx(self, text): |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 295 | return self.input_color + text |
| 296 | |
| 297 | def echo(self, text): |
| 298 | return self.echo_color + text |
| 299 | |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 300 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 301 | class DebugIO(Transform): |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 302 | """Print what is sent and received""" |
Chris Liechti | c0c660a | 2015-08-25 00:55:51 +0200 | [diff] [blame] | 303 | |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 304 | def rx(self, text): |
Chris Liechti | fac1c13 | 2017-08-27 23:35:55 +0200 | [diff] [blame] | 305 | sys.stderr.write(' [RX:{!r}] '.format(text)) |
Chris Liechti | e138438 | 2015-08-15 17:06:05 +0200 | [diff] [blame] | 306 | sys.stderr.flush() |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 307 | return text |
| 308 | |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 309 | def tx(self, text): |
Chris Liechti | fac1c13 | 2017-08-27 23:35:55 +0200 | [diff] [blame] | 310 | sys.stderr.write(' [TX:{!r}] '.format(text)) |
Chris Liechti | e138438 | 2015-08-15 17:06:05 +0200 | [diff] [blame] | 311 | sys.stderr.flush() |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 312 | return text |
| 313 | |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 314 | |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 315 | # other ideas: |
| 316 | # - add date/time for each newline |
| 317 | # - insert newline after: a) timeout b) packet end character |
| 318 | |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 319 | EOL_TRANSFORMATIONS = { |
Chris Liechti | ba45c52 | 2016-02-06 23:53:23 +0100 | [diff] [blame] | 320 | 'crlf': CRLF, |
| 321 | 'cr': CR, |
| 322 | 'lf': LF, |
| 323 | } |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 324 | |
| 325 | TRANSFORMATIONS = { |
Chris Liechti | ba45c52 | 2016-02-06 23:53:23 +0100 | [diff] [blame] | 326 | 'direct': Transform, # no transformation |
| 327 | 'default': NoTerminal, |
| 328 | 'nocontrol': NoControls, |
| 329 | 'printable': Printable, |
| 330 | 'colorize': Colorize, |
| 331 | 'debug': DebugIO, |
| 332 | } |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 333 | |
| 334 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 335 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
Chris Liechti | 89313c9 | 2015-09-01 02:33:13 +0200 | [diff] [blame] | 336 | def ask_for_port(): |
| 337 | """\ |
| 338 | Show a list of ports and ask the user for a choice. To make selection |
| 339 | easier on systems with long device names, also allow the input of an |
| 340 | index. |
| 341 | """ |
| 342 | sys.stderr.write('\n--- Available ports:\n') |
| 343 | ports = [] |
| 344 | for n, (port, desc, hwid) in enumerate(sorted(comports()), 1): |
Chris Liechti | 8b0eaf2 | 2017-07-19 22:59:57 +0200 | [diff] [blame] | 345 | sys.stderr.write('--- {:2}: {:20} {!r}\n'.format(n, port, desc)) |
Chris Liechti | 89313c9 | 2015-09-01 02:33:13 +0200 | [diff] [blame] | 346 | ports.append(port) |
| 347 | while True: |
| 348 | port = raw_input('--- Enter port index or full name: ') |
| 349 | try: |
| 350 | index = int(port) - 1 |
| 351 | if not 0 <= index < len(ports): |
| 352 | sys.stderr.write('--- Invalid index!\n') |
| 353 | continue |
| 354 | except ValueError: |
| 355 | pass |
| 356 | else: |
| 357 | port = ports[index] |
| 358 | return port |
cliechti | 1351dde | 2012-04-12 16:47:47 +0000 | [diff] [blame] | 359 | |
| 360 | |
cliechti | 8c2ea84 | 2011-03-18 01:51:46 +0000 | [diff] [blame] | 361 | class Miniterm(object): |
Chris Liechti | 89313c9 | 2015-09-01 02:33:13 +0200 | [diff] [blame] | 362 | """\ |
| 363 | Terminal application. Copy data from serial port to console and vice versa. |
| 364 | Handle special keys from the console to show menu etc. |
| 365 | """ |
| 366 | |
Chris Liechti | 3b45480 | 2015-08-26 23:39:59 +0200 | [diff] [blame] | 367 | def __init__(self, serial_instance, echo=False, eol='crlf', filters=()): |
Chris Liechti | 89eb247 | 2015-08-08 17:06:25 +0200 | [diff] [blame] | 368 | self.console = Console() |
Chris Liechti | 3b45480 | 2015-08-26 23:39:59 +0200 | [diff] [blame] | 369 | self.serial = serial_instance |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 370 | self.echo = echo |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 371 | self.raw = False |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 372 | self.input_encoding = 'UTF-8' |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 373 | self.output_encoding = 'UTF-8' |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 374 | self.eol = eol |
| 375 | self.filters = filters |
| 376 | self.update_transformations() |
Carlos | 52bfe35 | 2018-03-16 13:27:19 +0000 | [diff] [blame] | 377 | self.exit_character = unichr(0x1d) # GS/CTRL+] |
| 378 | self.menu_character = unichr(0x14) # Menu: CTRL+T |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 379 | self.alive = None |
| 380 | self._reader_alive = None |
| 381 | self.receiver_thread = None |
| 382 | self.rx_decoder = None |
| 383 | self.tx_decoder = None |
cliechti | 576de25 | 2002-02-28 23:54:44 +0000 | [diff] [blame] | 384 | |
cliechti | 8c2ea84 | 2011-03-18 01:51:46 +0000 | [diff] [blame] | 385 | def _start_reader(self): |
| 386 | """Start reader thread""" |
| 387 | self._reader_alive = True |
cliechti | 6fa76fb | 2009-07-08 23:53:39 +0000 | [diff] [blame] | 388 | # start serial->console thread |
Chris Liechti | 55ba7d9 | 2015-08-15 16:33:51 +0200 | [diff] [blame] | 389 | self.receiver_thread = threading.Thread(target=self.reader, name='rx') |
| 390 | self.receiver_thread.daemon = True |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 391 | self.receiver_thread.start() |
cliechti | 8c2ea84 | 2011-03-18 01:51:46 +0000 | [diff] [blame] | 392 | |
| 393 | def _stop_reader(self): |
| 394 | """Stop reader thread only, wait for clean exit of thread""" |
| 395 | self._reader_alive = False |
Chris Liechti | 933a517 | 2016-05-04 16:12:15 +0200 | [diff] [blame] | 396 | if hasattr(self.serial, 'cancel_read'): |
| 397 | self.serial.cancel_read() |
cliechti | 8c2ea84 | 2011-03-18 01:51:46 +0000 | [diff] [blame] | 398 | self.receiver_thread.join() |
| 399 | |
cliechti | 8c2ea84 | 2011-03-18 01:51:46 +0000 | [diff] [blame] | 400 | def start(self): |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 401 | """start worker threads""" |
cliechti | 8c2ea84 | 2011-03-18 01:51:46 +0000 | [diff] [blame] | 402 | self.alive = True |
| 403 | self._start_reader() |
cliechti | 6fa76fb | 2009-07-08 23:53:39 +0000 | [diff] [blame] | 404 | # enter console->serial loop |
Chris Liechti | 55ba7d9 | 2015-08-15 16:33:51 +0200 | [diff] [blame] | 405 | self.transmitter_thread = threading.Thread(target=self.writer, name='tx') |
| 406 | self.transmitter_thread.daemon = True |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 407 | self.transmitter_thread.start() |
Chris Liechti | 89eb247 | 2015-08-08 17:06:25 +0200 | [diff] [blame] | 408 | self.console.setup() |
cliechti | 53edb47 | 2009-02-06 21:18:46 +0000 | [diff] [blame] | 409 | |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 410 | def stop(self): |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 411 | """set flag to stop worker threads""" |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 412 | self.alive = False |
cliechti | 53edb47 | 2009-02-06 21:18:46 +0000 | [diff] [blame] | 413 | |
cliechti | bf6bb7d | 2006-03-30 00:28:18 +0000 | [diff] [blame] | 414 | def join(self, transmit_only=False): |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 415 | """wait for worker threads to terminate""" |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 416 | self.transmitter_thread.join() |
cliechti | bf6bb7d | 2006-03-30 00:28:18 +0000 | [diff] [blame] | 417 | if not transmit_only: |
Chris Liechti | 933a517 | 2016-05-04 16:12:15 +0200 | [diff] [blame] | 418 | if hasattr(self.serial, 'cancel_read'): |
| 419 | self.serial.cancel_read() |
cliechti | bf6bb7d | 2006-03-30 00:28:18 +0000 | [diff] [blame] | 420 | self.receiver_thread.join() |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 421 | |
Chris Liechti | 933a517 | 2016-05-04 16:12:15 +0200 | [diff] [blame] | 422 | def close(self): |
| 423 | self.serial.close() |
| 424 | |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 425 | def update_transformations(self): |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 426 | """take list of transformation classes and instantiate them for rx and tx""" |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 427 | transformations = [EOL_TRANSFORMATIONS[self.eol]] + [TRANSFORMATIONS[f] |
| 428 | for f in self.filters] |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 429 | self.tx_transformations = [t() for t in transformations] |
| 430 | self.rx_transformations = list(reversed(self.tx_transformations)) |
| 431 | |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 432 | def set_rx_encoding(self, encoding, errors='replace'): |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 433 | """set encoding for received data""" |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 434 | self.input_encoding = encoding |
| 435 | self.rx_decoder = codecs.getincrementaldecoder(encoding)(errors) |
| 436 | |
| 437 | def set_tx_encoding(self, encoding, errors='replace'): |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 438 | """set encoding for transmitted data""" |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 439 | self.output_encoding = encoding |
| 440 | self.tx_encoder = codecs.getincrementalencoder(encoding)(errors) |
| 441 | |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 442 | def dump_port_settings(self): |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 443 | """Write current settings to sys.stderr""" |
Chris Liechti | 1f7ac6c | 2015-08-15 15:16:37 +0200 | [diff] [blame] | 444 | sys.stderr.write("\n--- Settings: {p.name} {p.baudrate},{p.bytesize},{p.parity},{p.stopbits}\n".format( |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 445 | p=self.serial)) |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 446 | sys.stderr.write('--- RTS: {:8} DTR: {:8} BREAK: {:8}\n'.format( |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 447 | ('active' if self.serial.rts else 'inactive'), |
| 448 | ('active' if self.serial.dtr else 'inactive'), |
| 449 | ('active' if self.serial.break_condition else 'inactive'))) |
cliechti | 1011457 | 2009-08-05 23:40:50 +0000 | [diff] [blame] | 450 | try: |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 451 | sys.stderr.write('--- CTS: {:8} DSR: {:8} RI: {:8} CD: {:8}\n'.format( |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 452 | ('active' if self.serial.cts else 'inactive'), |
| 453 | ('active' if self.serial.dsr else 'inactive'), |
| 454 | ('active' if self.serial.ri else 'inactive'), |
| 455 | ('active' if self.serial.cd else 'inactive'))) |
cliechti | 1011457 | 2009-08-05 23:40:50 +0000 | [diff] [blame] | 456 | except serial.SerialException: |
Chris Liechti | 55ba7d9 | 2015-08-15 16:33:51 +0200 | [diff] [blame] | 457 | # on RFC 2217 ports, it can happen if no modem state notification was |
cliechti | 1011457 | 2009-08-05 23:40:50 +0000 | [diff] [blame] | 458 | # yet received. ignore this error. |
| 459 | pass |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 460 | sys.stderr.write('--- software flow control: {}\n'.format('active' if self.serial.xonxoff else 'inactive')) |
| 461 | sys.stderr.write('--- hardware flow control: {}\n'.format('active' if self.serial.rtscts else 'inactive')) |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 462 | sys.stderr.write('--- serial input encoding: {}\n'.format(self.input_encoding)) |
| 463 | sys.stderr.write('--- serial output encoding: {}\n'.format(self.output_encoding)) |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 464 | sys.stderr.write('--- EOL: {}\n'.format(self.eol.upper())) |
| 465 | sys.stderr.write('--- filters: {}\n'.format(' '.join(self.filters))) |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 466 | |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 467 | def reader(self): |
| 468 | """loop and copy serial->console""" |
cliechti | 6963b26 | 2010-01-02 03:01:21 +0000 | [diff] [blame] | 469 | try: |
cliechti | 8c2ea84 | 2011-03-18 01:51:46 +0000 | [diff] [blame] | 470 | while self.alive and self._reader_alive: |
Chris Liechti | 188cf59 | 2015-08-22 00:28:19 +0200 | [diff] [blame] | 471 | # read all that is there or wait for one byte |
Chris Liechti | 3b45480 | 2015-08-26 23:39:59 +0200 | [diff] [blame] | 472 | data = self.serial.read(self.serial.in_waiting or 1) |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 473 | if data: |
| 474 | if self.raw: |
| 475 | self.console.write_bytes(data) |
cliechti | 6963b26 | 2010-01-02 03:01:21 +0000 | [diff] [blame] | 476 | else: |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 477 | text = self.rx_decoder.decode(data) |
Chris Liechti | e138438 | 2015-08-15 17:06:05 +0200 | [diff] [blame] | 478 | for transformation in self.rx_transformations: |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 479 | text = transformation.rx(text) |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 480 | self.console.write(text) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 481 | except serial.SerialException: |
cliechti | 6963b26 | 2010-01-02 03:01:21 +0000 | [diff] [blame] | 482 | self.alive = False |
Chris Liechti | 1eb3f6b | 2016-04-27 02:12:50 +0200 | [diff] [blame] | 483 | self.console.cancel() |
| 484 | raise # XXX handle instead of re-raise? |
cliechti | 576de25 | 2002-02-28 23:54:44 +0000 | [diff] [blame] | 485 | |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 486 | def writer(self): |
cliechti | 8c2ea84 | 2011-03-18 01:51:46 +0000 | [diff] [blame] | 487 | """\ |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 488 | Loop and copy console->serial until self.exit_character character is |
| 489 | found. When self.menu_character is found, interpret the next key |
cliechti | 8c2ea84 | 2011-03-18 01:51:46 +0000 | [diff] [blame] | 490 | locally. |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 491 | """ |
| 492 | menu_active = False |
| 493 | try: |
| 494 | while self.alive: |
| 495 | try: |
Chris Liechti | 89eb247 | 2015-08-08 17:06:25 +0200 | [diff] [blame] | 496 | c = self.console.getkey() |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 497 | except KeyboardInterrupt: |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 498 | c = '\x03' |
Chris Liechti | 1eb3f6b | 2016-04-27 02:12:50 +0200 | [diff] [blame] | 499 | if not self.alive: |
| 500 | break |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 501 | if menu_active: |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 502 | self.handle_menu_key(c) |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 503 | menu_active = False |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 504 | elif c == self.menu_character: |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 505 | menu_active = True # next char will be for menu |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 506 | elif c == self.exit_character: |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 507 | self.stop() # exit app |
| 508 | break |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 509 | else: |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 510 | #~ if self.raw: |
| 511 | text = c |
Chris Liechti | e138438 | 2015-08-15 17:06:05 +0200 | [diff] [blame] | 512 | for transformation in self.tx_transformations: |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 513 | text = transformation.tx(text) |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 514 | self.serial.write(self.tx_encoder.encode(text)) |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 515 | if self.echo: |
Chris Liechti | 3b45480 | 2015-08-26 23:39:59 +0200 | [diff] [blame] | 516 | echo_text = c |
| 517 | for transformation in self.tx_transformations: |
| 518 | echo_text = transformation.echo(echo_text) |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 519 | self.console.write(echo_text) |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 520 | except: |
| 521 | self.alive = False |
| 522 | raise |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 523 | |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 524 | def handle_menu_key(self, c): |
| 525 | """Implement a simple menu / settings""" |
Chris Liechti | 55ba7d9 | 2015-08-15 16:33:51 +0200 | [diff] [blame] | 526 | if c == self.menu_character or c == self.exit_character: |
| 527 | # Menu/exit character again -> send itself |
Chris Liechti | d698af7 | 2015-08-24 20:24:55 +0200 | [diff] [blame] | 528 | self.serial.write(self.tx_encoder.encode(c)) |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 529 | if self.echo: |
| 530 | self.console.write(c) |
Chris Liechti | b7550bd | 2015-08-15 04:09:10 +0200 | [diff] [blame] | 531 | elif c == '\x15': # CTRL+U -> upload file |
Chris Liechti | 45c6f22 | 2017-07-17 23:56:24 +0200 | [diff] [blame] | 532 | self.upload_file() |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 533 | elif c in '\x08hH?': # CTRL+H, h, H, ? -> Show help |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 534 | sys.stderr.write(self.get_help_text()) |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 535 | elif c == '\x12': # CTRL+R -> Toggle RTS |
Chris Liechti | 3b45480 | 2015-08-26 23:39:59 +0200 | [diff] [blame] | 536 | self.serial.rts = not self.serial.rts |
| 537 | sys.stderr.write('--- RTS {} ---\n'.format('active' if self.serial.rts else 'inactive')) |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 538 | elif c == '\x04': # CTRL+D -> Toggle DTR |
Chris Liechti | 3b45480 | 2015-08-26 23:39:59 +0200 | [diff] [blame] | 539 | self.serial.dtr = not self.serial.dtr |
| 540 | sys.stderr.write('--- DTR {} ---\n'.format('active' if self.serial.dtr else 'inactive')) |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 541 | elif c == '\x02': # CTRL+B -> toggle BREAK condition |
Chris Liechti | 3b45480 | 2015-08-26 23:39:59 +0200 | [diff] [blame] | 542 | self.serial.break_condition = not self.serial.break_condition |
| 543 | sys.stderr.write('--- BREAK {} ---\n'.format('active' if self.serial.break_condition else 'inactive')) |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 544 | elif c == '\x05': # CTRL+E -> toggle local echo |
| 545 | self.echo = not self.echo |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 546 | sys.stderr.write('--- local echo {} ---\n'.format('active' if self.echo else 'inactive')) |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 547 | elif c == '\x06': # CTRL+F -> edit filters |
Chris Liechti | 45c6f22 | 2017-07-17 23:56:24 +0200 | [diff] [blame] | 548 | self.change_filter() |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 549 | elif c == '\x0c': # CTRL+L -> EOL mode |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 550 | modes = list(EOL_TRANSFORMATIONS) # keys |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 551 | eol = modes.index(self.eol) + 1 |
| 552 | if eol >= len(modes): |
| 553 | eol = 0 |
| 554 | self.eol = modes[eol] |
| 555 | sys.stderr.write('--- EOL: {} ---\n'.format(self.eol.upper())) |
| 556 | self.update_transformations() |
| 557 | elif c == '\x01': # CTRL+A -> set encoding |
Chris Liechti | 45c6f22 | 2017-07-17 23:56:24 +0200 | [diff] [blame] | 558 | self.change_encoding() |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 559 | elif c == '\x09': # CTRL+I -> info |
| 560 | self.dump_port_settings() |
| 561 | #~ elif c == '\x01': # CTRL+A -> cycle escape mode |
| 562 | #~ elif c == '\x0c': # CTRL+L -> cycle linefeed mode |
| 563 | elif c in 'pP': # P -> change port |
Chris Liechti | 45c6f22 | 2017-07-17 23:56:24 +0200 | [diff] [blame] | 564 | self.change_port() |
Chris Liechti | a73b96b | 2017-07-13 23:32:24 +0200 | [diff] [blame] | 565 | elif c in 'sS': # S -> suspend / open port temporarily |
Chris Liechti | 45c6f22 | 2017-07-17 23:56:24 +0200 | [diff] [blame] | 566 | self.suspend_port() |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 567 | elif c in 'bB': # B -> change baudrate |
Chris Liechti | 45c6f22 | 2017-07-17 23:56:24 +0200 | [diff] [blame] | 568 | self.change_baudrate() |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 569 | elif c == '8': # 8 -> change to 8 bits |
| 570 | self.serial.bytesize = serial.EIGHTBITS |
| 571 | self.dump_port_settings() |
| 572 | elif c == '7': # 7 -> change to 8 bits |
| 573 | self.serial.bytesize = serial.SEVENBITS |
| 574 | self.dump_port_settings() |
| 575 | elif c in 'eE': # E -> change to even parity |
| 576 | self.serial.parity = serial.PARITY_EVEN |
| 577 | self.dump_port_settings() |
| 578 | elif c in 'oO': # O -> change to odd parity |
| 579 | self.serial.parity = serial.PARITY_ODD |
| 580 | self.dump_port_settings() |
| 581 | elif c in 'mM': # M -> change to mark parity |
| 582 | self.serial.parity = serial.PARITY_MARK |
| 583 | self.dump_port_settings() |
| 584 | elif c in 'sS': # S -> change to space parity |
| 585 | self.serial.parity = serial.PARITY_SPACE |
| 586 | self.dump_port_settings() |
| 587 | elif c in 'nN': # N -> change to no parity |
| 588 | self.serial.parity = serial.PARITY_NONE |
| 589 | self.dump_port_settings() |
| 590 | elif c == '1': # 1 -> change to 1 stop bits |
| 591 | self.serial.stopbits = serial.STOPBITS_ONE |
| 592 | self.dump_port_settings() |
| 593 | elif c == '2': # 2 -> change to 2 stop bits |
| 594 | self.serial.stopbits = serial.STOPBITS_TWO |
| 595 | self.dump_port_settings() |
| 596 | elif c == '3': # 3 -> change to 1.5 stop bits |
| 597 | self.serial.stopbits = serial.STOPBITS_ONE_POINT_FIVE |
| 598 | self.dump_port_settings() |
| 599 | elif c in 'xX': # X -> change software flow control |
| 600 | self.serial.xonxoff = (c == 'X') |
| 601 | self.dump_port_settings() |
| 602 | elif c in 'rR': # R -> change hardware flow control |
| 603 | self.serial.rtscts = (c == 'R') |
| 604 | self.dump_port_settings() |
| 605 | else: |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 606 | sys.stderr.write('--- unknown menu character {} --\n'.format(key_description(c))) |
| 607 | |
Chris Liechti | 45c6f22 | 2017-07-17 23:56:24 +0200 | [diff] [blame] | 608 | def upload_file(self): |
| 609 | """Ask user for filenname and send its contents""" |
| 610 | sys.stderr.write('\n--- File to upload: ') |
| 611 | sys.stderr.flush() |
| 612 | with self.console: |
| 613 | filename = sys.stdin.readline().rstrip('\r\n') |
| 614 | if filename: |
| 615 | try: |
| 616 | with open(filename, 'rb') as f: |
| 617 | sys.stderr.write('--- Sending file {} ---\n'.format(filename)) |
| 618 | while True: |
| 619 | block = f.read(1024) |
| 620 | if not block: |
| 621 | break |
| 622 | self.serial.write(block) |
| 623 | # Wait for output buffer to drain. |
| 624 | self.serial.flush() |
| 625 | sys.stderr.write('.') # Progress indicator. |
| 626 | sys.stderr.write('\n--- File {} sent ---\n'.format(filename)) |
| 627 | except IOError as e: |
| 628 | sys.stderr.write('--- ERROR opening file {}: {} ---\n'.format(filename, e)) |
| 629 | |
| 630 | def change_filter(self): |
| 631 | """change the i/o transformations""" |
| 632 | sys.stderr.write('\n--- Available Filters:\n') |
| 633 | sys.stderr.write('\n'.join( |
| 634 | '--- {:<10} = {.__doc__}'.format(k, v) |
| 635 | for k, v in sorted(TRANSFORMATIONS.items()))) |
| 636 | sys.stderr.write('\n--- Enter new filter name(s) [{}]: '.format(' '.join(self.filters))) |
| 637 | with self.console: |
| 638 | new_filters = sys.stdin.readline().lower().split() |
| 639 | if new_filters: |
| 640 | for f in new_filters: |
| 641 | if f not in TRANSFORMATIONS: |
Chris Liechti | fac1c13 | 2017-08-27 23:35:55 +0200 | [diff] [blame] | 642 | sys.stderr.write('--- unknown filter: {!r}\n'.format(f)) |
Chris Liechti | 45c6f22 | 2017-07-17 23:56:24 +0200 | [diff] [blame] | 643 | break |
| 644 | else: |
| 645 | self.filters = new_filters |
| 646 | self.update_transformations() |
| 647 | sys.stderr.write('--- filters: {}\n'.format(' '.join(self.filters))) |
| 648 | |
| 649 | def change_encoding(self): |
| 650 | """change encoding on the serial port""" |
| 651 | sys.stderr.write('\n--- Enter new encoding name [{}]: '.format(self.input_encoding)) |
| 652 | with self.console: |
| 653 | new_encoding = sys.stdin.readline().strip() |
| 654 | if new_encoding: |
| 655 | try: |
| 656 | codecs.lookup(new_encoding) |
| 657 | except LookupError: |
| 658 | sys.stderr.write('--- invalid encoding name: {}\n'.format(new_encoding)) |
| 659 | else: |
| 660 | self.set_rx_encoding(new_encoding) |
| 661 | self.set_tx_encoding(new_encoding) |
| 662 | sys.stderr.write('--- serial input encoding: {}\n'.format(self.input_encoding)) |
| 663 | sys.stderr.write('--- serial output encoding: {}\n'.format(self.output_encoding)) |
| 664 | |
| 665 | def change_baudrate(self): |
| 666 | """change the baudrate""" |
| 667 | sys.stderr.write('\n--- Baudrate: ') |
| 668 | sys.stderr.flush() |
| 669 | with self.console: |
| 670 | backup = self.serial.baudrate |
| 671 | try: |
| 672 | self.serial.baudrate = int(sys.stdin.readline().strip()) |
| 673 | except ValueError as e: |
| 674 | sys.stderr.write('--- ERROR setting baudrate: {} ---\n'.format(e)) |
| 675 | self.serial.baudrate = backup |
| 676 | else: |
| 677 | self.dump_port_settings() |
| 678 | |
| 679 | def change_port(self): |
| 680 | """Have a conversation with the user to change the serial port""" |
| 681 | with self.console: |
| 682 | try: |
| 683 | port = ask_for_port() |
| 684 | except KeyboardInterrupt: |
| 685 | port = None |
| 686 | if port and port != self.serial.port: |
| 687 | # reader thread needs to be shut down |
| 688 | self._stop_reader() |
| 689 | # save settings |
| 690 | settings = self.serial.getSettingsDict() |
| 691 | try: |
| 692 | new_serial = serial.serial_for_url(port, do_not_open=True) |
| 693 | # restore settings and open |
| 694 | new_serial.applySettingsDict(settings) |
| 695 | new_serial.rts = self.serial.rts |
| 696 | new_serial.dtr = self.serial.dtr |
| 697 | new_serial.open() |
| 698 | new_serial.break_condition = self.serial.break_condition |
| 699 | except Exception as e: |
| 700 | sys.stderr.write('--- ERROR opening new port: {} ---\n'.format(e)) |
| 701 | new_serial.close() |
| 702 | else: |
| 703 | self.serial.close() |
| 704 | self.serial = new_serial |
| 705 | sys.stderr.write('--- Port changed to: {} ---\n'.format(self.serial.port)) |
| 706 | # and restart the reader thread |
| 707 | self._start_reader() |
| 708 | |
| 709 | def suspend_port(self): |
| 710 | """\ |
| 711 | open port temporarily, allow reconnect, exit and port change to get |
| 712 | out of the loop |
| 713 | """ |
| 714 | # reader thread needs to be shut down |
| 715 | self._stop_reader() |
| 716 | self.serial.close() |
| 717 | sys.stderr.write('\n--- Port closed: {} ---\n'.format(self.serial.port)) |
| 718 | do_change_port = False |
| 719 | while not self.serial.is_open: |
| 720 | sys.stderr.write('--- Quit: {exit} | p: port change | any other key to reconnect ---\n'.format( |
| 721 | exit=key_description(self.exit_character))) |
| 722 | k = self.console.getkey() |
| 723 | if k == self.exit_character: |
| 724 | self.stop() # exit app |
| 725 | break |
| 726 | elif k in 'pP': |
| 727 | do_change_port = True |
| 728 | break |
| 729 | try: |
| 730 | self.serial.open() |
| 731 | except Exception as e: |
| 732 | sys.stderr.write('--- ERROR opening port: {} ---\n'.format(e)) |
| 733 | if do_change_port: |
| 734 | self.change_port() |
| 735 | else: |
| 736 | # and restart the reader thread |
| 737 | self._start_reader() |
| 738 | sys.stderr.write('--- Port opened: {} ---\n'.format(self.serial.port)) |
| 739 | |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 740 | def get_help_text(self): |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 741 | """return the help text""" |
Chris Liechti | 55ba7d9 | 2015-08-15 16:33:51 +0200 | [diff] [blame] | 742 | # help text, starts with blank line! |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 743 | return """ |
| 744 | --- pySerial ({version}) - miniterm - help |
| 745 | --- |
| 746 | --- {exit:8} Exit program |
| 747 | --- {menu:8} Menu escape key, followed by: |
| 748 | --- Menu keys: |
| 749 | --- {menu:7} Send the menu character itself to remote |
| 750 | --- {exit:7} Send the exit character itself to remote |
| 751 | --- {info:7} Show info |
| 752 | --- {upload:7} Upload file (prompt will be shown) |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 753 | --- {repr:7} encoding |
| 754 | --- {filter:7} edit filters |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 755 | --- Toggles: |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 756 | --- {rts:7} RTS {dtr:7} DTR {brk:7} BREAK |
| 757 | --- {echo:7} echo {eol:7} EOL |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 758 | --- |
Chris Liechti | 55ba7d9 | 2015-08-15 16:33:51 +0200 | [diff] [blame] | 759 | --- Port settings ({menu} followed by the following): |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 760 | --- p change port |
| 761 | --- 7 8 set data bits |
Chris Liechti | b7550bd | 2015-08-15 04:09:10 +0200 | [diff] [blame] | 762 | --- N E O S M change parity (None, Even, Odd, Space, Mark) |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 763 | --- 1 2 3 set stop bits (1, 2, 1.5) |
| 764 | --- b change baud rate |
| 765 | --- x X disable/enable software flow control |
| 766 | --- r R disable/enable hardware flow control |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 767 | """.format(version=getattr(serial, 'VERSION', 'unknown version'), |
| 768 | exit=key_description(self.exit_character), |
| 769 | menu=key_description(self.menu_character), |
| 770 | rts=key_description('\x12'), |
| 771 | dtr=key_description('\x04'), |
| 772 | brk=key_description('\x02'), |
| 773 | echo=key_description('\x05'), |
| 774 | info=key_description('\x09'), |
| 775 | upload=key_description('\x15'), |
| 776 | repr=key_description('\x01'), |
| 777 | filter=key_description('\x06'), |
| 778 | eol=key_description('\x0c')) |
Chris Liechti | 7af7c75 | 2015-08-12 15:45:19 +0200 | [diff] [blame] | 779 | |
| 780 | |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 781 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
Chris Liechti | 55ba7d9 | 2015-08-15 16:33:51 +0200 | [diff] [blame] | 782 | # default args can be used to override when calling main() from an other script |
| 783 | # e.g to create a miniterm-my-device.py |
| 784 | def main(default_port=None, default_baudrate=9600, default_rts=None, default_dtr=None): |
Chris Liechti | a887c93 | 2016-02-13 23:10:14 +0100 | [diff] [blame] | 785 | """Command line tool, entry point""" |
| 786 | |
Chris Liechti | b7550bd | 2015-08-15 04:09:10 +0200 | [diff] [blame] | 787 | import argparse |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 788 | |
Chris Liechti | b7550bd | 2015-08-15 04:09:10 +0200 | [diff] [blame] | 789 | parser = argparse.ArgumentParser( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 790 | description='Miniterm - A simple terminal program for the serial port.') |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 791 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 792 | parser.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 793 | 'port', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 794 | nargs='?', |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 795 | help='serial port name ("-" to show port list)', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 796 | default=default_port) |
cliechti | 5370cee | 2013-10-13 03:08:19 +0000 | [diff] [blame] | 797 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 798 | parser.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 799 | 'baudrate', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 800 | nargs='?', |
| 801 | type=int, |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 802 | help='set baud rate, default: %(default)s', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 803 | default=default_baudrate) |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 804 | |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 805 | group = parser.add_argument_group('port settings') |
cliechti | 53edb47 | 2009-02-06 21:18:46 +0000 | [diff] [blame] | 806 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 807 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 808 | '--parity', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 809 | choices=['N', 'E', 'O', 'S', 'M'], |
| 810 | type=lambda c: c.upper(), |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 811 | help='set parity, one of {N E O S M}, default: N', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 812 | default='N') |
cliechti | 53edb47 | 2009-02-06 21:18:46 +0000 | [diff] [blame] | 813 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 814 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 815 | '--rtscts', |
| 816 | action='store_true', |
| 817 | help='enable RTS/CTS flow control (default off)', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 818 | default=False) |
cliechti | 53edb47 | 2009-02-06 21:18:46 +0000 | [diff] [blame] | 819 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 820 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 821 | '--xonxoff', |
| 822 | action='store_true', |
| 823 | help='enable software flow control (default off)', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 824 | default=False) |
cliechti | 53edb47 | 2009-02-06 21:18:46 +0000 | [diff] [blame] | 825 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 826 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 827 | '--rts', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 828 | type=int, |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 829 | help='set initial RTS line state (possible values: 0, 1)', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 830 | default=default_rts) |
cliechti | 5370cee | 2013-10-13 03:08:19 +0000 | [diff] [blame] | 831 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 832 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 833 | '--dtr', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 834 | type=int, |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 835 | help='set initial DTR line state (possible values: 0, 1)', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 836 | default=default_dtr) |
cliechti | 5370cee | 2013-10-13 03:08:19 +0000 | [diff] [blame] | 837 | |
Chris Liechti | 00f8428 | 2015-12-24 23:40:34 +0100 | [diff] [blame] | 838 | group.add_argument( |
zsquareplusc | b178d12 | 2018-05-07 20:12:59 +0200 | [diff] [blame] | 839 | '--non-exclusive', |
| 840 | dest='exclusive', |
| 841 | action='store_false', |
| 842 | help='disable locking for native ports', |
Sascha Silbe | 9c05535 | 2018-03-19 20:10:26 +0100 | [diff] [blame] | 843 | default=True) |
| 844 | |
| 845 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 846 | '--ask', |
| 847 | action='store_true', |
| 848 | help='ask again for port when open fails', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 849 | default=False) |
Chris Liechti | 00f8428 | 2015-12-24 23:40:34 +0100 | [diff] [blame] | 850 | |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 851 | group = parser.add_argument_group('data handling') |
cliechti | 5370cee | 2013-10-13 03:08:19 +0000 | [diff] [blame] | 852 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 853 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 854 | '-e', '--echo', |
| 855 | action='store_true', |
| 856 | help='enable local echo (default off)', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 857 | default=False) |
cliechti | 5370cee | 2013-10-13 03:08:19 +0000 | [diff] [blame] | 858 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 859 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 860 | '--encoding', |
| 861 | dest='serial_port_encoding', |
| 862 | metavar='CODEC', |
| 863 | help='set the encoding for the serial port (e.g. hexlify, Latin1, UTF-8), default: %(default)s', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 864 | default='UTF-8') |
cliechti | 5370cee | 2013-10-13 03:08:19 +0000 | [diff] [blame] | 865 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 866 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 867 | '-f', '--filter', |
| 868 | action='append', |
| 869 | metavar='NAME', |
| 870 | help='add text transformation', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 871 | default=[]) |
Chris Liechti | 2b1b355 | 2015-08-12 15:35:33 +0200 | [diff] [blame] | 872 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 873 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 874 | '--eol', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 875 | choices=['CR', 'LF', 'CRLF'], |
| 876 | type=lambda c: c.upper(), |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 877 | help='end of line mode', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 878 | default='CRLF') |
cliechti | 53edb47 | 2009-02-06 21:18:46 +0000 | [diff] [blame] | 879 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 880 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 881 | '--raw', |
| 882 | action='store_true', |
| 883 | help='Do no apply any encodings/transformations', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 884 | default=False) |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 885 | |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 886 | group = parser.add_argument_group('hotkeys') |
cliechti | b7d746d | 2006-03-28 22:44:30 +0000 | [diff] [blame] | 887 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 888 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 889 | '--exit-char', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 890 | type=int, |
| 891 | metavar='NUM', |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 892 | help='Unicode of special character that is used to exit the application, default: %(default)s', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 893 | default=0x1d) # GS/CTRL+] |
cliechti | bf6bb7d | 2006-03-30 00:28:18 +0000 | [diff] [blame] | 894 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 895 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 896 | '--menu-char', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 897 | type=int, |
| 898 | metavar='NUM', |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 899 | help='Unicode code of special character that is used to control miniterm (menu), default: %(default)s', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 900 | default=0x14) # Menu: CTRL+T |
cliechti | 9c592b3 | 2008-06-16 22:00:14 +0000 | [diff] [blame] | 901 | |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 902 | group = parser.add_argument_group('diagnostics') |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 903 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 904 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 905 | '-q', '--quiet', |
| 906 | action='store_true', |
| 907 | help='suppress non-error messages', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 908 | default=False) |
cliechti | 5370cee | 2013-10-13 03:08:19 +0000 | [diff] [blame] | 909 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 910 | group.add_argument( |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 911 | '--develop', |
| 912 | action='store_true', |
| 913 | help='show Python traceback on error', |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 914 | default=False) |
cliechti | 5370cee | 2013-10-13 03:08:19 +0000 | [diff] [blame] | 915 | |
Chris Liechti | b7550bd | 2015-08-15 04:09:10 +0200 | [diff] [blame] | 916 | args = parser.parse_args() |
cliechti | 5370cee | 2013-10-13 03:08:19 +0000 | [diff] [blame] | 917 | |
Chris Liechti | b7550bd | 2015-08-15 04:09:10 +0200 | [diff] [blame] | 918 | if args.menu_char == args.exit_char: |
cliechti | 6c8eb2f | 2009-07-08 02:10:46 +0000 | [diff] [blame] | 919 | parser.error('--exit-char can not be the same as --menu-char') |
| 920 | |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 921 | if args.filter: |
| 922 | if 'help' in args.filter: |
| 923 | sys.stderr.write('Available filters:\n') |
Chris Liechti | 442bf51 | 2015-08-15 01:42:24 +0200 | [diff] [blame] | 924 | sys.stderr.write('\n'.join( |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 925 | '{:<10} = {.__doc__}'.format(k, v) |
| 926 | for k, v in sorted(TRANSFORMATIONS.items()))) |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 927 | sys.stderr.write('\n') |
| 928 | sys.exit(1) |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 929 | filters = args.filter |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 930 | else: |
Chris Liechti | b3df13e | 2015-08-25 02:20:09 +0200 | [diff] [blame] | 931 | filters = ['default'] |
Chris Liechti | c7a5d4c | 2015-08-11 23:32:20 +0200 | [diff] [blame] | 932 | |
Chris Liechti | 00f8428 | 2015-12-24 23:40:34 +0100 | [diff] [blame] | 933 | while True: |
| 934 | # no port given on command line -> ask user now |
| 935 | if args.port is None or args.port == '-': |
| 936 | try: |
| 937 | args.port = ask_for_port() |
| 938 | except KeyboardInterrupt: |
| 939 | sys.stderr.write('\n') |
| 940 | parser.error('user aborted and port is not given') |
| 941 | else: |
| 942 | if not args.port: |
| 943 | parser.error('port is not given') |
| 944 | try: |
| 945 | serial_instance = serial.serial_for_url( |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 946 | args.port, |
| 947 | args.baudrate, |
| 948 | parity=args.parity, |
| 949 | rtscts=args.rtscts, |
| 950 | xonxoff=args.xonxoff, |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 951 | do_not_open=True) |
Chris Liechti | 3b45480 | 2015-08-26 23:39:59 +0200 | [diff] [blame] | 952 | |
Chris Liechti | f542fca | 2016-05-13 00:20:14 +0200 | [diff] [blame] | 953 | if not hasattr(serial_instance, 'cancel_read'): |
| 954 | # enable timeout for alive flag polling if cancel_read is not available |
| 955 | serial_instance.timeout = 1 |
| 956 | |
Chris Liechti | 00f8428 | 2015-12-24 23:40:34 +0100 | [diff] [blame] | 957 | if args.dtr is not None: |
| 958 | if not args.quiet: |
| 959 | sys.stderr.write('--- forcing DTR {}\n'.format('active' if args.dtr else 'inactive')) |
| 960 | serial_instance.dtr = args.dtr |
| 961 | if args.rts is not None: |
| 962 | if not args.quiet: |
| 963 | sys.stderr.write('--- forcing RTS {}\n'.format('active' if args.rts else 'inactive')) |
| 964 | serial_instance.rts = args.rts |
Chris Liechti | 3b45480 | 2015-08-26 23:39:59 +0200 | [diff] [blame] | 965 | |
Sascha Silbe | 9c05535 | 2018-03-19 20:10:26 +0100 | [diff] [blame] | 966 | if isinstance(serial_instance, serial.Serial): |
| 967 | serial_instance.exclusive = args.exclusive |
| 968 | |
Chris Liechti | 00f8428 | 2015-12-24 23:40:34 +0100 | [diff] [blame] | 969 | serial_instance.open() |
| 970 | except serial.SerialException as e: |
Chris Liechti | fac1c13 | 2017-08-27 23:35:55 +0200 | [diff] [blame] | 971 | sys.stderr.write('could not open port {!r}: {}\n'.format(args.port, e)) |
Chris Liechti | 00f8428 | 2015-12-24 23:40:34 +0100 | [diff] [blame] | 972 | if args.develop: |
| 973 | raise |
| 974 | if not args.ask: |
| 975 | sys.exit(1) |
| 976 | else: |
| 977 | args.port = '-' |
| 978 | else: |
| 979 | break |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 980 | |
Chris Liechti | 3b45480 | 2015-08-26 23:39:59 +0200 | [diff] [blame] | 981 | miniterm = Miniterm( |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 982 | serial_instance, |
| 983 | echo=args.echo, |
| 984 | eol=args.eol.lower(), |
| 985 | filters=filters) |
Chris Liechti | 3b45480 | 2015-08-26 23:39:59 +0200 | [diff] [blame] | 986 | miniterm.exit_character = unichr(args.exit_char) |
| 987 | miniterm.menu_character = unichr(args.menu_char) |
| 988 | miniterm.raw = args.raw |
| 989 | miniterm.set_rx_encoding(args.serial_port_encoding) |
| 990 | miniterm.set_tx_encoding(args.serial_port_encoding) |
| 991 | |
Chris Liechti | b7550bd | 2015-08-15 04:09:10 +0200 | [diff] [blame] | 992 | if not args.quiet: |
Chris Liechti | 1f7ac6c | 2015-08-15 15:16:37 +0200 | [diff] [blame] | 993 | sys.stderr.write('--- Miniterm on {p.name} {p.baudrate},{p.bytesize},{p.parity},{p.stopbits} ---\n'.format( |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 994 | p=miniterm.serial)) |
Chris Liechti | b7550bd | 2015-08-15 04:09:10 +0200 | [diff] [blame] | 995 | sys.stderr.write('--- Quit: {} | Menu: {} | Help: {} followed by {} ---\n'.format( |
Chris Liechti | 397cf41 | 2016-02-11 00:11:48 +0100 | [diff] [blame] | 996 | key_description(miniterm.exit_character), |
| 997 | key_description(miniterm.menu_character), |
| 998 | key_description(miniterm.menu_character), |
| 999 | key_description('\x08'))) |
cliechti | 6fa76fb | 2009-07-08 23:53:39 +0000 | [diff] [blame] | 1000 | |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 1001 | miniterm.start() |
cliechti | 258ab0a | 2011-03-21 23:03:45 +0000 | [diff] [blame] | 1002 | try: |
| 1003 | miniterm.join(True) |
| 1004 | except KeyboardInterrupt: |
| 1005 | pass |
Chris Liechti | b7550bd | 2015-08-15 04:09:10 +0200 | [diff] [blame] | 1006 | if not args.quiet: |
Chris Liechti | 49f1993 | 2017-08-30 17:55:39 +0200 | [diff] [blame] | 1007 | sys.stderr.write('\n--- exit ---\n') |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 1008 | miniterm.join() |
Chris Liechti | 933a517 | 2016-05-04 16:12:15 +0200 | [diff] [blame] | 1009 | miniterm.close() |
cliechti | bf6bb7d | 2006-03-30 00:28:18 +0000 | [diff] [blame] | 1010 | |
cliechti | 5370cee | 2013-10-13 03:08:19 +0000 | [diff] [blame] | 1011 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
cliechti | 8b3ad39 | 2002-03-03 20:12:21 +0000 | [diff] [blame] | 1012 | if __name__ == '__main__': |
cliechti | 6385f2c | 2005-09-21 19:51:19 +0000 | [diff] [blame] | 1013 | main() |
Cefn Hoile | d64fb60 | 2018-06-08 09:46:28 +0100 | [diff] [blame^] | 1014 | |