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