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