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