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