diff options
author | Chris Liechti <cliechti@gmx.net> | 2015-08-18 01:01:38 +0200 |
---|---|---|
committer | Chris Liechti <cliechti@gmx.net> | 2015-08-18 01:01:38 +0200 |
commit | f8d46c61199fbff78e0c371275e440b70aac38a4 (patch) | |
tree | f3e3248f971ac31a67ebb7c0391f236defc6391f /examples | |
parent | 9cbd070717bdd94075f910b56de867408f44b7dd (diff) | |
download | pyserial-git-f8d46c61199fbff78e0c371275e440b70aac38a4.tar.gz |
tcp_serial_redirect: remove spy option, cleanup code
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/tcp_serial_redirect.py | 42 |
1 files changed, 6 insertions, 36 deletions
diff --git a/examples/tcp_serial_redirect.py b/examples/tcp_serial_redirect.py index 5498d00..9f042c0 100755 --- a/examples/tcp_serial_redirect.py +++ b/examples/tcp_serial_redirect.py @@ -2,7 +2,7 @@ # # Redirect data from a TCP/IP connection to a serial port and vice versa. # -# (C) 2002-2009 Chris Liechti <cliechti@gmx.net> +# (C) 2002-2015 Chris Liechti <cliechti@gmx.net> # # SPDX-License-Identifier: BSD-3-Clause @@ -11,17 +11,14 @@ import os import time import threading import socket -import codecs import serial class Redirector(object): - def __init__(self, serial_instance, socket, spy=False): + def __init__(self, serial_instance, socket): self.serial = serial_instance self.socket = socket - self.spy = spy - self._write_lock = threading.Lock() - def shortcut(self): + def shortcircuit(self): """connect the serial port to the TCP port by copying everything from one side to the other""" self.alive = True @@ -40,24 +37,13 @@ class Redirector(object): if n: data = data + self.serial.read(n) # and get as much as possible if data: - # the spy shows what's on the serial port, so log it before converting newlines - if self.spy: - sys.stdout.write(codecs.escape_encode(data)[0]) - sys.stdout.flush() - # escape outgoing data when needed (Telnet IAC (0xff) character) - with self._write_lock: - self.socket.sendall(data) # send it over TCP + self.socket.sendall(data) # send it over TCP except socket.error as msg: sys.stderr.write('ERROR: %s\n' % msg) # probably got disconnected break self.alive = False - def write(self, data): - """thread safe socket write with no data escaping. used to send telnet stuff""" - with self._write_lock: - self.socket.sendall(data) - def writer(self): """loop forever and copy socket->serial""" while self.alive: @@ -66,10 +52,6 @@ class Redirector(object): if not data: break self.serial.write(data) # get a bunch of bytes and send them - # the spy shows what's on the serial port, so log it after converting newlines - if self.spy: - sys.stdout.write(codecs.escape_encode(data)[0]) - sys.stdout.flush() except socket.error as msg: sys.stderr.write('ERROR: %s\n' % msg) # probably got disconnected @@ -111,12 +93,6 @@ it waits for the next connect. help='suppress non error messages', default=False) - parser.add_argument('--spy', - dest='spy', - action='store_true', - help='peek at the communication and print all data to the console', - default=False) - group = parser.add_argument_group('serial Port') group.add_argument("--parity", @@ -187,14 +163,8 @@ it waits for the next connect. connection, addr = srv.accept() sys.stderr.write('Connected by %s\n' % (addr,)) # enter network <-> serial loop - r = Redirector( - ser, - connection, - args.spy, - ) - r.shortcut() - if args.spy: - sys.stdout.write('\n') + r = Redirector(ser, connection) + r.shortcircuit() sys.stderr.write('Disconnected\n') connection.close() except KeyboardInterrupt: |