summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Liechti <cliechti@gmx.net>2021-10-02 04:58:22 +0200
committerChris Liechti <cliechti@gmx.net>2021-10-02 04:58:22 +0200
commitf7e39f06bd8365db331288928ccd9c47e7dd6a60 (patch)
tree3ddf06f2eaba507303447f05fe3d075e62dfbca5
parentbabdfdc5490a636d73195b6118d79c6b29ddd46e (diff)
downloadpyserial-git-f7e39f06bd8365db331288928ccd9c47e7dd6a60.tar.gz
feat(miniterm): handle SIGINT on POSIX
fixes #570
-rw-r--r--serial/tools/miniterm.py32
1 files changed, 21 insertions, 11 deletions
diff --git a/serial/tools/miniterm.py b/serial/tools/miniterm.py
index 236d917..78155fa 100644
--- a/serial/tools/miniterm.py
+++ b/serial/tools/miniterm.py
@@ -43,7 +43,8 @@ def key_description(character):
class ConsoleBase(object):
"""OS abstraction for console (input/output codec, no echo)"""
- def __init__(self):
+ def __init__(self, miniterm):
+ self.miniterm = miniterm
if sys.version_info >= (3, 0):
self.byte_output = sys.stdout.buffer
else:
@@ -124,12 +125,12 @@ if os.name == 'nt': # noqa
'O': '\x1b[F', # END
'R': '\x1b[2~', # INSERT
'S': '\x1b[3~', # DELETE
- 'I': '\x1b[5~', # PGUP
- 'Q': '\x1b[6~', # PGDN
+ 'I': '\x1b[5~', # PAGE UP
+ 'Q': '\x1b[6~', # PAGE DOWN
}
-
- def __init__(self):
- super(Console, self).__init__()
+
+ def __init__(self, miniterm):
+ super(Console, self).__init__(miniterm)
self._saved_ocp = ctypes.windll.kernel32.GetConsoleOutputCP()
self._saved_icp = ctypes.windll.kernel32.GetConsoleCP()
ctypes.windll.kernel32.SetConsoleOutputCP(65001)
@@ -139,7 +140,7 @@ if os.name == 'nt': # noqa
if platform.release() == '10' and int(platform.version().split('.')[2]) > 10586:
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
import ctypes.wintypes as wintypes
- if not hasattr(wintypes, 'LPDWORD'): # PY2
+ if not hasattr(wintypes, 'LPDWORD'): # PY2
wintypes.LPDWORD = ctypes.POINTER(wintypes.DWORD)
SetConsoleMode = ctypes.windll.kernel32.SetConsoleMode
GetConsoleMode = ctypes.windll.kernel32.GetConsoleMode
@@ -160,7 +161,7 @@ if os.name == 'nt': # noqa
ctypes.windll.kernel32.SetConsoleCP(self._saved_icp)
try:
ctypes.windll.kernel32.SetConsoleMode(ctypes.windll.kernel32.GetStdHandle(-11), self._saved_cm)
- except AttributeError: # in case no _saved_cm
+ except AttributeError: # in case no _saved_cm
pass
def getkey(self):
@@ -190,13 +191,15 @@ elif os.name == 'posix':
import atexit
import termios
import fcntl
+ import signal
class Console(ConsoleBase):
- def __init__(self):
- super(Console, self).__init__()
+ def __init__(self, miniterm):
+ super(Console, self).__init__(miniterm)
self.fd = sys.stdin.fileno()
self.old = termios.tcgetattr(self.fd)
atexit.register(self.cleanup)
+ signal.signal(signal.SIGINT, self.sigint)
if sys.version_info < (3, 0):
self.enc_stdin = codecs.getreader(sys.stdin.encoding)(sys.stdin)
else:
@@ -221,6 +224,11 @@ elif os.name == 'posix':
def cleanup(self):
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old)
+ def sigint(self, sig, frame):
+ """signal handler for a clean exit on SIGINT"""
+ self.miniterm.stop()
+ self.cancel()
+
else:
raise NotImplementedError(
'Sorry no implementation for your platform ({}) available.'.format(sys.platform))
@@ -393,7 +401,7 @@ class Miniterm(object):
"""
def __init__(self, serial_instance, echo=False, eol='crlf', filters=()):
- self.console = Console()
+ self.console = Console(self)
self.serial = serial_instance
self.echo = echo
self.raw = False
@@ -409,6 +417,7 @@ class Miniterm(object):
self.receiver_thread = None
self.rx_decoder = None
self.tx_decoder = None
+ self.tx_encoder = None
def _start_reader(self):
"""Start reader thread"""
@@ -1038,6 +1047,7 @@ def main(default_port=None, default_baudrate=9600, default_rts=None, default_dtr
miniterm.join()
miniterm.close()
+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if __name__ == '__main__':
main()