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