cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 1 | #! python |
| 2 | # |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 3 | # This module implements a simple socket based client. |
| 4 | # It does not support changing any port parameters and will silently ignore any |
| 5 | # requests to do so. |
| 6 | # |
| 7 | # The purpose of this module is that applications using pySerial can connect to |
| 8 | # TCP/IP to serial port converters that do not support RFC 2217. |
| 9 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 10 | # This file is part of pySerial. https://github.com/pyserial/pyserial |
Chris Liechti | a422211 | 2015-08-07 01:03:12 +0200 | [diff] [blame] | 11 | # (C) 2001-2015 Chris Liechti <cliechti@gmx.net> |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 12 | # |
| 13 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 14 | # |
| 15 | # URL format: socket://<host>:<port>[/option[/option...]] |
| 16 | # options: |
| 17 | # - "debug" print diagnostic messages |
| 18 | |
Kurt McKee | 057387c | 2018-02-07 22:10:38 -0600 | [diff] [blame^] | 19 | from __future__ import absolute_import |
| 20 | |
Chris Liechti | b10daf4 | 2016-01-26 00:27:10 +0100 | [diff] [blame] | 21 | import errno |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 22 | import logging |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 23 | import select |
| 24 | import socket |
| 25 | import time |
Chris Liechti | c4bca9e | 2015-08-07 14:40:41 +0200 | [diff] [blame] | 26 | try: |
| 27 | import urlparse |
| 28 | except ImportError: |
| 29 | import urllib.parse as urlparse |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 30 | |
Chris Liechti | eb16326 | 2016-12-18 22:40:22 +0100 | [diff] [blame] | 31 | from serial.serialutil import SerialBase, SerialException, to_bytes, \ |
| 32 | portNotOpenError, writeTimeoutError, Timeout |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 33 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 34 | # map log level names to constants. used in from_url() |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 35 | LOGGER_LEVELS = { |
Chris Liechti | 6594df6 | 2016-02-04 21:13:41 +0100 | [diff] [blame] | 36 | 'debug': logging.DEBUG, |
| 37 | 'info': logging.INFO, |
| 38 | 'warning': logging.WARNING, |
| 39 | 'error': logging.ERROR, |
| 40 | } |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 41 | |
Chris Liechti | 4c3ec66 | 2016-04-21 22:52:31 +0200 | [diff] [blame] | 42 | POLL_TIMEOUT = 5 |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 43 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 44 | |
Chris Liechti | ef6b7b4 | 2015-08-06 22:19:26 +0200 | [diff] [blame] | 45 | class Serial(SerialBase): |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 46 | """Serial port implementation for plain sockets.""" |
| 47 | |
| 48 | BAUDRATES = (50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 49 | 9600, 19200, 38400, 57600, 115200) |
| 50 | |
| 51 | def open(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 52 | """\ |
| 53 | Open port with current settings. This may throw a SerialException |
| 54 | if the port cannot be opened. |
| 55 | """ |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 56 | self.logger = None |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 57 | if self._port is None: |
| 58 | raise SerialException("Port must be configured before it can be used.") |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 59 | if self.is_open: |
cliechti | 8f69e70 | 2011-03-19 00:22:32 +0000 | [diff] [blame] | 60 | raise SerialException("Port is already open.") |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 61 | try: |
Chris Liechti | 4c3ec66 | 2016-04-21 22:52:31 +0200 | [diff] [blame] | 62 | # timeout is used for write timeout support :/ and to get an initial connection timeout |
| 63 | self._socket = socket.create_connection(self.from_url(self.portstr), timeout=POLL_TIMEOUT) |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 64 | except Exception as msg: |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 65 | self._socket = None |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 66 | raise SerialException("Could not open port {}: {}".format(self.portstr, msg)) |
Chris Liechti | eb16326 | 2016-12-18 22:40:22 +0100 | [diff] [blame] | 67 | # after connecting, switch to non-blocking, we're using select |
| 68 | self._socket.setblocking(False) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 69 | |
Chris Liechti | 4c3ec66 | 2016-04-21 22:52:31 +0200 | [diff] [blame] | 70 | # not that there is anything to configure... |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 71 | self._reconfigure_port() |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 72 | # all things set up get, now a clean start |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 73 | self.is_open = True |
| 74 | if not self._dsrdtr: |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 75 | self._update_dtr_state() |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 76 | if not self._rtscts: |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 77 | self._update_rts_state() |
| 78 | self.reset_input_buffer() |
| 79 | self.reset_output_buffer() |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 80 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 81 | def _reconfigure_port(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 82 | """\ |
| 83 | Set communication parameters on opened port. For the socket:// |
| 84 | protocol all settings are ignored! |
| 85 | """ |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 86 | if self._socket is None: |
| 87 | raise SerialException("Can only operate on open ports") |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 88 | if self.logger: |
| 89 | self.logger.info('ignored port configuration change') |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 90 | |
| 91 | def close(self): |
| 92 | """Close port""" |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 93 | if self.is_open: |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 94 | if self._socket: |
| 95 | try: |
| 96 | self._socket.shutdown(socket.SHUT_RDWR) |
| 97 | self._socket.close() |
| 98 | except: |
| 99 | # ignore errors. |
| 100 | pass |
| 101 | self._socket = None |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 102 | self.is_open = False |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 103 | # in case of quick reconnects, give the server some time |
| 104 | time.sleep(0.3) |
| 105 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 106 | def from_url(self, url): |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 107 | """extract host and port from an URL string""" |
Chris Liechti | a422211 | 2015-08-07 01:03:12 +0200 | [diff] [blame] | 108 | parts = urlparse.urlsplit(url) |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 109 | if parts.scheme != "socket": |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 110 | raise SerialException( |
| 111 | 'expected a string in the form ' |
| 112 | '"socket://<host>:<port>[?logging={debug|info|warning|error}]": ' |
| 113 | 'not starting with socket:// ({!r})'.format(parts.scheme)) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 114 | try: |
Chris Liechti | a422211 | 2015-08-07 01:03:12 +0200 | [diff] [blame] | 115 | # process options now, directly altering self |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 116 | for option, values in urlparse.parse_qs(parts.query, True).items(): |
| 117 | if option == 'logging': |
Chris Liechti | a422211 | 2015-08-07 01:03:12 +0200 | [diff] [blame] | 118 | logging.basicConfig() # XXX is that good to call it here? |
| 119 | self.logger = logging.getLogger('pySerial.socket') |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 120 | self.logger.setLevel(LOGGER_LEVELS[values[0]]) |
Chris Liechti | a422211 | 2015-08-07 01:03:12 +0200 | [diff] [blame] | 121 | self.logger.debug('enabled logging') |
| 122 | else: |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 123 | raise ValueError('unknown option: {!r}'.format(option)) |
| 124 | if not 0 <= parts.port < 65536: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 125 | raise ValueError("port not in range 0...65535") |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 126 | except ValueError as e: |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 127 | raise SerialException( |
| 128 | 'expected a string in the form ' |
| 129 | '"socket://<host>:<port>[?logging={debug|info|warning|error}]": {}'.format(e)) |
| 130 | |
| 131 | return (parts.hostname, parts.port) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 132 | |
| 133 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 134 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 135 | @property |
| 136 | def in_waiting(self): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 137 | """Return the number of bytes currently in the input buffer.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 138 | if not self.is_open: |
| 139 | raise portNotOpenError |
cliechti | 77e088a | 2014-08-04 10:29:24 +0000 | [diff] [blame] | 140 | # Poll the socket to see if it is ready for reading. |
| 141 | # If ready, at least one byte will be to read. |
| 142 | lr, lw, lx = select.select([self._socket], [], [], 0) |
| 143 | return len(lr) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 144 | |
Chris Liechti | 6032cf5 | 2016-01-15 23:12:20 +0100 | [diff] [blame] | 145 | # select based implementation, similar to posix, but only using socket API |
| 146 | # to be portable, additionally handle socket timeout which is used to |
| 147 | # emulate write timeouts |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 148 | def read(self, size=1): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 149 | """\ |
| 150 | Read size bytes from the serial port. If a timeout is set it may |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 151 | return less characters as requested. With no timeout it will block |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 152 | until the requested number of bytes is read. |
| 153 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 154 | if not self.is_open: |
| 155 | raise portNotOpenError |
Chris Liechti | 6032cf5 | 2016-01-15 23:12:20 +0100 | [diff] [blame] | 156 | read = bytearray() |
Chris Liechti | eb16326 | 2016-12-18 22:40:22 +0100 | [diff] [blame] | 157 | timeout = Timeout(self._timeout) |
Chris Liechti | 6032cf5 | 2016-01-15 23:12:20 +0100 | [diff] [blame] | 158 | while len(read) < size: |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 159 | try: |
Chris Liechti | eb16326 | 2016-12-18 22:40:22 +0100 | [diff] [blame] | 160 | ready, _, _ = select.select([self._socket], [], [], timeout.time_left()) |
Chris Liechti | 6032cf5 | 2016-01-15 23:12:20 +0100 | [diff] [blame] | 161 | # If select was used with a timeout, and the timeout occurs, it |
| 162 | # returns with empty lists -> thus abort read operation. |
| 163 | # For timeout == 0 (non-blocking operation) also abort when |
| 164 | # there is nothing to read. |
| 165 | if not ready: |
| 166 | break # timeout |
| 167 | buf = self._socket.recv(size - len(read)) |
| 168 | # read should always return some data as select reported it was |
| 169 | # ready to read when we get to this point, unless it is EOF |
| 170 | if not buf: |
| 171 | raise SerialException('socket disconnected') |
| 172 | read.extend(buf) |
Chris Liechti | 6032cf5 | 2016-01-15 23:12:20 +0100 | [diff] [blame] | 173 | except OSError as e: |
| 174 | # this is for Python 3.x where select.error is a subclass of |
Chris Liechti | fc1bf5a | 2017-05-06 23:39:05 +0200 | [diff] [blame] | 175 | # OSError ignore BlockingIOErrors and EINTR. other errors are shown |
| 176 | # https://www.python.org/dev/peps/pep-0475. |
| 177 | if e.errno not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR): |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 178 | raise SerialException('read failed: {}'.format(e)) |
Chris Liechti | eb16326 | 2016-12-18 22:40:22 +0100 | [diff] [blame] | 179 | except (select.error, socket.error) as e: |
Chris Liechti | 6032cf5 | 2016-01-15 23:12:20 +0100 | [diff] [blame] | 180 | # this is for Python 2.x |
Chris Liechti | fc1bf5a | 2017-05-06 23:39:05 +0200 | [diff] [blame] | 181 | # ignore BlockingIOErrors and EINTR. all errors are shown |
Chris Liechti | 6032cf5 | 2016-01-15 23:12:20 +0100 | [diff] [blame] | 182 | # see also http://www.python.org/dev/peps/pep-3151/#select |
Chris Liechti | fc1bf5a | 2017-05-06 23:39:05 +0200 | [diff] [blame] | 183 | if e[0] not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR): |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 184 | raise SerialException('read failed: {}'.format(e)) |
Chris Liechti | eb16326 | 2016-12-18 22:40:22 +0100 | [diff] [blame] | 185 | if timeout.expired(): |
| 186 | break |
Chris Liechti | 6032cf5 | 2016-01-15 23:12:20 +0100 | [diff] [blame] | 187 | return bytes(read) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 188 | |
| 189 | def write(self, data): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 190 | """\ |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 191 | Output the given byte string over the serial port. Can block if the |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 192 | connection is blocked. May raise SerialException if the connection is |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 193 | closed. |
| 194 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 195 | if not self.is_open: |
| 196 | raise portNotOpenError |
Chris Liechti | eb16326 | 2016-12-18 22:40:22 +0100 | [diff] [blame] | 197 | |
| 198 | d = to_bytes(data) |
| 199 | tx_len = length = len(d) |
| 200 | timeout = Timeout(self._write_timeout) |
| 201 | while tx_len > 0: |
| 202 | try: |
| 203 | n = self._socket.send(d) |
| 204 | if timeout.is_non_blocking: |
| 205 | # Zero timeout indicates non-blocking - simply return the |
| 206 | # number of bytes of data actually written |
| 207 | return n |
| 208 | elif not timeout.is_infinite: |
| 209 | # when timeout is set, use select to wait for being ready |
| 210 | # with the time left as timeout |
| 211 | if timeout.expired(): |
| 212 | raise writeTimeoutError |
| 213 | _, ready, _ = select.select([], [self._socket], [], timeout.time_left()) |
| 214 | if not ready: |
| 215 | raise writeTimeoutError |
| 216 | else: |
| 217 | assert timeout.time_left() is None |
| 218 | # wait for write operation |
| 219 | _, ready, _ = select.select([], [self._socket], [], None) |
| 220 | if not ready: |
| 221 | raise SerialException('write failed (select)') |
| 222 | d = d[n:] |
| 223 | tx_len -= n |
| 224 | except SerialException: |
| 225 | raise |
Chris Liechti | fc1bf5a | 2017-05-06 23:39:05 +0200 | [diff] [blame] | 226 | except OSError as e: |
| 227 | # this is for Python 3.x where select.error is a subclass of |
| 228 | # OSError ignore BlockingIOErrors and EINTR. other errors are shown |
| 229 | # https://www.python.org/dev/peps/pep-0475. |
| 230 | if e.errno not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR): |
| 231 | raise SerialException('write failed: {}'.format(e)) |
| 232 | except select.error as e: |
| 233 | # this is for Python 2.x |
| 234 | # ignore BlockingIOErrors and EINTR. all errors are shown |
| 235 | # see also http://www.python.org/dev/peps/pep-3151/#select |
| 236 | if e[0] not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR): |
| 237 | raise SerialException('write failed: {}'.format(e)) |
| 238 | if not timeout.is_non_blocking and timeout.expired(): |
| 239 | raise writeTimeoutError |
Chris Liechti | eb16326 | 2016-12-18 22:40:22 +0100 | [diff] [blame] | 240 | return length - len(d) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 241 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 242 | def reset_input_buffer(self): |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 243 | """Clear input buffer, discarding all that is in the buffer.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 244 | if not self.is_open: |
| 245 | raise portNotOpenError |
Chris Liechti | 64d5992 | 2016-12-19 03:12:05 +0100 | [diff] [blame] | 246 | |
| 247 | # just use recv to remove input, while there is some |
| 248 | ready = True |
| 249 | while ready: |
| 250 | ready, _, _ = select.select([self._socket], [], [], 0) |
| 251 | try: |
| 252 | self._socket.recv(4096) |
| 253 | except OSError as e: |
| 254 | # this is for Python 3.x where select.error is a subclass of |
Chris Liechti | fc1bf5a | 2017-05-06 23:39:05 +0200 | [diff] [blame] | 255 | # OSError ignore BlockingIOErrors and EINTR. other errors are shown |
| 256 | # https://www.python.org/dev/peps/pep-0475. |
| 257 | if e.errno not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR): |
| 258 | raise SerialException('read failed: {}'.format(e)) |
Chris Liechti | 64d5992 | 2016-12-19 03:12:05 +0100 | [diff] [blame] | 259 | except (select.error, socket.error) as e: |
| 260 | # this is for Python 2.x |
Chris Liechti | fc1bf5a | 2017-05-06 23:39:05 +0200 | [diff] [blame] | 261 | # ignore BlockingIOErrors and EINTR. all errors are shown |
Chris Liechti | 64d5992 | 2016-12-19 03:12:05 +0100 | [diff] [blame] | 262 | # see also http://www.python.org/dev/peps/pep-3151/#select |
Chris Liechti | fc1bf5a | 2017-05-06 23:39:05 +0200 | [diff] [blame] | 263 | if e[0] not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR): |
| 264 | raise SerialException('read failed: {}'.format(e)) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 265 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 266 | def reset_output_buffer(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 267 | """\ |
| 268 | Clear output buffer, aborting the current output and |
| 269 | discarding all that is in the buffer. |
| 270 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 271 | if not self.is_open: |
| 272 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 273 | if self.logger: |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 274 | self.logger.info('ignored reset_output_buffer') |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 275 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 276 | def send_break(self, duration=0.25): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 277 | """\ |
| 278 | Send break condition. Timed, returns to idle state after given |
| 279 | duration. |
| 280 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 281 | if not self.is_open: |
| 282 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 283 | if self.logger: |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 284 | self.logger.info('ignored send_break({!r})'.format(duration)) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 285 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 286 | def _update_break_state(self): |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 287 | """Set break: Controls TXD. When active, to transmitting is |
| 288 | possible.""" |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 289 | if self.logger: |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 290 | self.logger.info('ignored _update_break_state({!r})'.format(self._break_state)) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 291 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 292 | def _update_rts_state(self): |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 293 | """Set terminal status line: Request To Send""" |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 294 | if self.logger: |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 295 | self.logger.info('ignored _update_rts_state({!r})'.format(self._rts_state)) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 296 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 297 | def _update_dtr_state(self): |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 298 | """Set terminal status line: Data Terminal Ready""" |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 299 | if self.logger: |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 300 | self.logger.info('ignored _update_dtr_state({!r})'.format(self._dtr_state)) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 301 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 302 | @property |
| 303 | def cts(self): |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 304 | """Read terminal status line: Clear To Send""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 305 | if not self.is_open: |
| 306 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 307 | if self.logger: |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 308 | self.logger.info('returning dummy for cts') |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 309 | return True |
| 310 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 311 | @property |
| 312 | def dsr(self): |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 313 | """Read terminal status line: Data Set Ready""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 314 | if not self.is_open: |
| 315 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 316 | if self.logger: |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 317 | self.logger.info('returning dummy for dsr') |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 318 | return True |
| 319 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 320 | @property |
| 321 | def ri(self): |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 322 | """Read terminal status line: Ring Indicator""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 323 | if not self.is_open: |
| 324 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 325 | if self.logger: |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 326 | self.logger.info('returning dummy for ri') |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 327 | return False |
| 328 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 329 | @property |
| 330 | def cd(self): |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 331 | """Read terminal status line: Carrier Detect""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 332 | if not self.is_open: |
| 333 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 334 | if self.logger: |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 335 | self.logger.info('returning dummy for cd)') |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 336 | return True |
| 337 | |
| 338 | # - - - platform specific - - - |
cliechti | b869edb | 2014-07-31 22:13:19 +0000 | [diff] [blame] | 339 | |
| 340 | # works on Linux and probably all the other POSIX systems |
| 341 | def fileno(self): |
| 342 | """Get the file handle of the underlying socket for use with select""" |
| 343 | return self._socket.fileno() |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 344 | |
| 345 | |
Chris Liechti | ef6b7b4 | 2015-08-06 22:19:26 +0200 | [diff] [blame] | 346 | # |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 347 | # simple client test |
| 348 | if __name__ == '__main__': |
| 349 | import sys |
| 350 | s = Serial('socket://localhost:7000') |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 351 | sys.stdout.write('{}\n'.format(s)) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 352 | |
| 353 | sys.stdout.write("write...\n") |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 354 | s.write(b"hello\n") |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 355 | s.flush() |
Chris Liechti | 4daa9d5 | 2016-03-22 00:32:01 +0100 | [diff] [blame] | 356 | sys.stdout.write("read: {}\n".format(s.read(5))) |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 357 | |
| 358 | s.close() |