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