cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 1 | #! python |
| 2 | # |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 3 | # This module implements a loop back connection receiving itself what it sent. |
| 4 | # |
| 5 | # The purpose of this module is.. well... You can run the unit tests with it. |
| 6 | # and it was so easy to implement ;-) |
| 7 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 8 | # This file is part of pySerial. https://github.com/pyserial/pyserial |
Chris Liechti | c4bca9e | 2015-08-07 14:40:41 +0200 | [diff] [blame] | 9 | # (C) 2001-2015 Chris Liechti <cliechti@gmx.net> |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 10 | # |
| 11 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 12 | # |
| 13 | # URL format: loop://[option[/option...]] |
| 14 | # options: |
| 15 | # - "debug" print diagnostic messages |
Chris Liechti | c4bca9e | 2015-08-07 14:40:41 +0200 | [diff] [blame] | 16 | import logging |
| 17 | import numbers |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 18 | import time |
Chris Liechti | c4bca9e | 2015-08-07 14:40:41 +0200 | [diff] [blame] | 19 | try: |
| 20 | import urlparse |
| 21 | except ImportError: |
| 22 | import urllib.parse as urlparse |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 23 | try: |
| 24 | import queue |
| 25 | except ImportError: |
| 26 | import Queue as queue |
Chris Liechti | c4bca9e | 2015-08-07 14:40:41 +0200 | [diff] [blame] | 27 | |
Chris Liechti | 6ba7d6f | 2016-01-28 21:02:49 +0100 | [diff] [blame] | 28 | from serial.serialutil import SerialBase, SerialException, to_bytes, iterbytes, writeTimeoutError, portNotOpenError |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [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 | 6594df6 | 2016-02-04 21:13:41 +0100 | [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 | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 38 | |
Chris Liechti | ef6b7b4 | 2015-08-06 22:19:26 +0200 | [diff] [blame] | 39 | class Serial(SerialBase): |
cliechti | ab3d428 | 2011-08-19 01:52:46 +0000 | [diff] [blame] | 40 | """Serial port implementation that simulates a loop back connection in plain software.""" |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 41 | |
| 42 | BAUDRATES = (50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 43 | 9600, 19200, 38400, 57600, 115200) |
| 44 | |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 45 | def __init__(self, *args, **kwargs): |
| 46 | super(Serial, self).__init__(*args, **kwargs) |
| 47 | self.buffer_size = 4096 |
Chris Liechti | 7806fc0 | 2015-12-10 21:15:20 +0100 | [diff] [blame] | 48 | self.queue = None |
| 49 | self.logger = None |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 50 | |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 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 | """ |
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 | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 58 | self.logger = None |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 59 | self.queue = queue.Queue(self.buffer_size) |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 60 | |
| 61 | if self._port is None: |
| 62 | raise SerialException("Port must be configured before it can be used.") |
| 63 | # not that there is anything to open, but the function applies the |
| 64 | # options found in the URL |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 65 | self.from_url(self.port) |
cliechti | 41973a9 | 2009-08-06 02:18:21 +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 | 41973a9 | 2009-08-06 02:18:21 +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 | 41973a9 | 2009-08-06 02:18:21 +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 | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 77 | |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 78 | def close(self): |
Chris Liechti | 7806fc0 | 2015-12-10 21:15:20 +0100 | [diff] [blame] | 79 | if self.is_open: |
| 80 | self.is_open = False |
| 81 | try: |
| 82 | self.queue.put_nowait(None) |
| 83 | except queue.Full: |
| 84 | pass |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 85 | super(Serial, self).close() |
| 86 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 87 | def _reconfigure_port(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 88 | """\ |
| 89 | Set communication parameters on opened port. For the loop:// |
| 90 | protocol all settings are ignored! |
| 91 | """ |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 92 | # not that's it of any real use, but it helps in the unit tests |
Chris Liechti | 92df95a | 2016-02-09 23:30:37 +0100 | [diff] [blame] | 93 | if not isinstance(self._baudrate, numbers.Integral) or not 0 < self._baudrate < 2 ** 32: |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 94 | raise ValueError("invalid baudrate: {!r}".format(self._baudrate)) |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 95 | if self.logger: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 96 | self.logger.info('_reconfigure_port()') |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 97 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 98 | def from_url(self, url): |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 99 | """extract host and port from an URL string""" |
Chris Liechti | c4bca9e | 2015-08-07 14:40:41 +0200 | [diff] [blame] | 100 | parts = urlparse.urlsplit(url) |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 101 | if parts.scheme != "loop": |
Chris Liechti | 43b3b10 | 2016-06-07 21:31:47 +0200 | [diff] [blame] | 102 | raise SerialException( |
| 103 | 'expected a string in the form ' |
| 104 | '"loop://[?logging={debug|info|warning|error}]": not starting ' |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 105 | 'with loop:// ({!r})'.format(parts.scheme)) |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 106 | try: |
| 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': |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 110 | logging.basicConfig() # XXX is that good to call it here? |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 111 | self.logger = logging.getLogger('pySerial.loop') |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 112 | self.logger.setLevel(LOGGER_LEVELS[values[0]]) |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 113 | self.logger.debug('enabled logging') |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 114 | else: |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 115 | raise ValueError('unknown option: {!r}'.format(option)) |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 116 | except ValueError as e: |
Chris Liechti | 43b3b10 | 2016-06-07 21:31:47 +0200 | [diff] [blame] | 117 | raise SerialException( |
| 118 | 'expected a string in the form ' |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 119 | '"loop://[?logging={debug|info|warning|error}]": {}'.format(e)) |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 120 | |
| 121 | # - - - - - - - - - - - - - - - - - - - - - - - - |
| 122 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 123 | @property |
| 124 | def in_waiting(self): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 125 | """Return the number of bytes currently in the input buffer.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 126 | if not self.is_open: |
| 127 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 128 | if self.logger: |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 129 | # attention the logged value can differ from return value in |
| 130 | # threaded environments... |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 131 | self.logger.debug('in_waiting -> {:d}'.format(self.queue.qsize())) |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 132 | return self.queue.qsize() |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 133 | |
| 134 | def read(self, size=1): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 135 | """\ |
| 136 | Read size bytes from the serial port. If a timeout is set it may |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 137 | return less characters as requested. With no timeout it will block |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 138 | until the requested number of bytes is read. |
| 139 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 140 | if not self.is_open: |
| 141 | raise portNotOpenError |
Chris Liechti | 220c3a5 | 2015-09-15 00:06:51 +0200 | [diff] [blame] | 142 | if self._timeout is not None and self._timeout != 0: |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 143 | timeout = time.time() + self._timeout |
| 144 | else: |
cliechti | 024b4f4 | 2009-08-07 18:43:05 +0000 | [diff] [blame] | 145 | timeout = None |
cliechti | 1de32cd | 2009-08-07 19:05:09 +0000 | [diff] [blame] | 146 | data = bytearray() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 147 | while size > 0 and self.is_open: |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 148 | try: |
Chris Liechti | 220c3a5 | 2015-09-15 00:06:51 +0200 | [diff] [blame] | 149 | b = self.queue.get(timeout=self._timeout) # XXX inter char timeout |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 150 | except queue.Empty: |
Chris Liechti | 220c3a5 | 2015-09-15 00:06:51 +0200 | [diff] [blame] | 151 | if self._timeout == 0: |
| 152 | break |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 153 | else: |
Chris Liechti | 220c3a5 | 2015-09-15 00:06:51 +0200 | [diff] [blame] | 154 | if data is not None: |
| 155 | data += b |
| 156 | size -= 1 |
| 157 | else: |
| 158 | break |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 159 | # check for timeout now, after data has been read. |
| 160 | # useful for timeout = 0 (non blocking) read |
cliechti | 024b4f4 | 2009-08-07 18:43:05 +0000 | [diff] [blame] | 161 | if timeout and time.time() > timeout: |
Chris Liechti | 220c3a5 | 2015-09-15 00:06:51 +0200 | [diff] [blame] | 162 | if self.logger: |
| 163 | self.logger.info('read timeout') |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 164 | break |
| 165 | return bytes(data) |
| 166 | |
| 167 | def write(self, data): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 168 | """\ |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 169 | Output the given byte string over the serial port. Can block if the |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 170 | connection is blocked. May raise SerialException if the connection is |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 171 | closed. |
| 172 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 173 | if not self.is_open: |
| 174 | raise portNotOpenError |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 175 | data = to_bytes(data) |
cliechti | 66957bf | 2009-08-06 23:25:37 +0000 | [diff] [blame] | 176 | # calculate aprox time that would be used to send the data |
Chris Liechti | 6594df6 | 2016-02-04 21:13:41 +0100 | [diff] [blame] | 177 | time_used_to_send = 10.0 * len(data) / self._baudrate |
cliechti | 66957bf | 2009-08-06 23:25:37 +0000 | [diff] [blame] | 178 | # when a write timeout is configured check if we would be successful |
| 179 | # (not sending anything, not even the part that would have time) |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 180 | if self._write_timeout is not None and time_used_to_send > self._write_timeout: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 181 | time.sleep(self._write_timeout) # must wait so that unit test succeeds |
cliechti | 66957bf | 2009-08-06 23:25:37 +0000 | [diff] [blame] | 182 | raise writeTimeoutError |
Chris Liechti | f99cd5c | 2015-08-13 22:54:16 +0200 | [diff] [blame] | 183 | for byte in iterbytes(data): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 184 | self.queue.put(byte, timeout=self._write_timeout) |
cliechti | 41973a9 | 2009-08-06 02:18:21 +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 | 41973a9 | 2009-08-06 02:18:21 +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 | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 192 | self.logger.info('reset_input_buffer()') |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 193 | try: |
| 194 | while self.queue.qsize(): |
| 195 | self.queue.get_nowait() |
| 196 | except queue.Empty: |
| 197 | pass |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 198 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 199 | def reset_output_buffer(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 200 | """\ |
| 201 | Clear output buffer, aborting the current output and |
| 202 | discarding all that is in the buffer. |
| 203 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 204 | if not self.is_open: |
| 205 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 206 | if self.logger: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 207 | self.logger.info('reset_output_buffer()') |
Chris Liechti | a469cde | 2015-08-11 23:05:24 +0200 | [diff] [blame] | 208 | try: |
| 209 | while self.queue.qsize(): |
| 210 | self.queue.get_nowait() |
| 211 | except queue.Empty: |
| 212 | pass |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 213 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 214 | def _update_break_state(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 215 | """\ |
| 216 | Set break: Controls TXD. When active, to transmitting is |
| 217 | possible. |
| 218 | """ |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 219 | if self.logger: |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 220 | self.logger.info('_update_break_state({!r})'.format(self._break_state)) |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 221 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 222 | def _update_rts_state(self): |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 223 | """Set terminal status line: Request To Send""" |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 224 | if self.logger: |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 225 | self.logger.info('_update_rts_state({!r}) -> state of CTS'.format(self._rts_state)) |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 226 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 227 | def _update_dtr_state(self): |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 228 | """Set terminal status line: Data Terminal Ready""" |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 229 | if self.logger: |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 230 | self.logger.info('_update_dtr_state({!r}) -> state of DSR'.format(self._dtr_state)) |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 231 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 232 | @property |
| 233 | def cts(self): |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 234 | """Read terminal status line: Clear To Send""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 235 | if not self.is_open: |
| 236 | raise portNotOpenError |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 237 | if self.logger: |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 238 | self.logger.info('CTS -> state of RTS ({!r})'.format(self._rts_state)) |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 239 | return self._rts_state |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 240 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 241 | @property |
| 242 | def dsr(self): |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 243 | """Read terminal status line: Data Set Ready""" |
cliechti | 6a30077 | 2009-08-12 02:28:56 +0000 | [diff] [blame] | 244 | if self.logger: |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 245 | self.logger.info('DSR -> state of DTR ({!r})'.format(self._dtr_state)) |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 246 | return self._dtr_state |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 247 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 248 | @property |
| 249 | def ri(self): |
cliechti | 41973a9 | 2009-08-06 02:18:21 +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 | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 254 | self.logger.info('returning dummy for RI') |
cliechti | 41973a9 | 2009-08-06 02:18:21 +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 | 41973a9 | 2009-08-06 02:18:21 +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 | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 263 | self.logger.info('returning dummy for CD') |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 264 | return True |
| 265 | |
| 266 | # - - - platform specific - - - |
| 267 | # None so far |
| 268 | |
| 269 | |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 270 | # simple client test |
| 271 | if __name__ == '__main__': |
| 272 | import sys |
cliechti | ab3d428 | 2011-08-19 01:52:46 +0000 | [diff] [blame] | 273 | s = Serial('loop://') |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 274 | sys.stdout.write('{}\n'.format(s)) |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 275 | |
| 276 | sys.stdout.write("write...\n") |
| 277 | s.write("hello\n") |
| 278 | s.flush() |
Chris Liechti | c8f3f82 | 2016-06-08 03:35:28 +0200 | [diff] [blame^] | 279 | sys.stdout.write("read: {!r}\n".format(s.read(5))) |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 280 | |
| 281 | s.close() |