cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 1 | ============== |
| 2 | pySerial API |
| 3 | ============== |
| 4 | |
| 5 | .. module:: serial |
| 6 | |
| 7 | Classes |
| 8 | ======= |
| 9 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 10 | Native ports |
| 11 | ------------ |
| 12 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 13 | .. class:: Serial |
| 14 | |
Rob Gaddi | 636cc64 | 2017-02-24 11:39:46 -0800 | [diff] [blame] | 15 | .. method:: __init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, write_timeout=None, dsrdtr=False, inter_byte_timeout=None, exclusive=None) |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 16 | |
| 17 | :param port: |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 18 | Device name or :const:`None`. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 19 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 20 | :param int baudrate: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 21 | Baud rate such as 9600 or 115200 etc. |
| 22 | |
| 23 | :param bytesize: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 24 | Number of data bits. Possible values: |
| 25 | :const:`FIVEBITS`, :const:`SIXBITS`, :const:`SEVENBITS`, |
| 26 | :const:`EIGHTBITS` |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 27 | |
| 28 | :param parity: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 29 | Enable parity checking. Possible values: |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 30 | :const:`PARITY_NONE`, :const:`PARITY_EVEN`, :const:`PARITY_ODD` |
| 31 | :const:`PARITY_MARK`, :const:`PARITY_SPACE` |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 32 | |
| 33 | :param stopbits: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 34 | Number of stop bits. Possible values: |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 35 | :const:`STOPBITS_ONE`, :const:`STOPBITS_ONE_POINT_FIVE`, |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 36 | :const:`STOPBITS_TWO` |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 37 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 38 | :param float timeout: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 39 | Set a read timeout value. |
| 40 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 41 | :param bool xonxoff: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 42 | Enable software flow control. |
| 43 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 44 | :param bool rtscts: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 45 | Enable hardware (RTS/CTS) flow control. |
| 46 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 47 | :param bool dsrdtr: |
cliechti | 07709e4 | 2010-05-21 00:30:09 +0000 | [diff] [blame] | 48 | Enable hardware (DSR/DTR) flow control. |
| 49 | |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 50 | :param float write_timeout: |
cliechti | 07709e4 | 2010-05-21 00:30:09 +0000 | [diff] [blame] | 51 | Set a write timeout value. |
| 52 | |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 53 | :param float inter_byte_timeout: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 54 | Inter-character timeout, :const:`None` to disable (default). |
Chris Liechti | 1c4bc81 | 2017-03-08 02:44:04 +0100 | [diff] [blame] | 55 | |
Rob Gaddi | 636cc64 | 2017-02-24 11:39:46 -0800 | [diff] [blame] | 56 | :param bool exclusive: |
Rob Gaddi | b1e3e32 | 2017-02-24 11:40:06 -0800 | [diff] [blame] | 57 | Set exclusive access mode (POSIX only). A port cannot be opened in |
| 58 | exclusive access mode if it is already open in exclusive access mode. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 59 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 60 | :exception ValueError: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 61 | Will be raised when parameter are out of range, e.g. baud rate, data bits. |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 62 | |
| 63 | :exception SerialException: |
| 64 | In case the device can not be found or can not be configured. |
| 65 | |
| 66 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 67 | The port is immediately opened on object creation, when a *port* is |
| 68 | given. It is not opened when *port* is :const:`None` and a successive call |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 69 | to :meth:`open` is required. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 70 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 71 | *port* is a device name: depending on operating system. e.g. |
| 72 | ``/dev/ttyUSB0`` on GNU/Linux or ``COM3`` on Windows. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 73 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 74 | The parameter *baudrate* can be one of the standard values: |
| 75 | 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 76 | 9600, 19200, 38400, 57600, 115200. |
cliechti | 28b8fd0 | 2011-12-28 21:39:42 +0000 | [diff] [blame] | 77 | These are well supported on all platforms. |
| 78 | |
| 79 | Standard values above 115200, such as: 230400, 460800, 500000, 576000, |
| 80 | 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000, |
| 81 | 4000000 also work on many platforms and devices. |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 82 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 83 | Non-standard values are also supported on some platforms (GNU/Linux, MAC |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 84 | OSX >= Tiger, Windows). Though, even on these platforms some serial |
| 85 | ports may reject non-standard values. |
| 86 | |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 87 | Possible values for the parameter *timeout* which controls the behavior |
| 88 | of :meth:`read`: |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 89 | |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 90 | - ``timeout = None``: wait forever / until requested number of bytes |
| 91 | are received |
| 92 | - ``timeout = 0``: non-blocking mode, return immediately in any case, |
| 93 | returning zero or more, up to the requested number of bytes |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 94 | - ``timeout = x``: set timeout to ``x`` seconds (float allowed) |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 95 | returns immediately when the requested number of bytes are available, |
| 96 | otherwise wait until the timeout expires and return all bytes that |
| 97 | were received until then. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 98 | |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 99 | :meth:`write` is blocking by default, unless *write_timeout* is set. |
| 100 | For possible values refer to the list for *timeout* above. |
cliechti | 07709e4 | 2010-05-21 00:30:09 +0000 | [diff] [blame] | 101 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 102 | Note that enabling both flow control methods (*xonxoff* and *rtscts*) |
| 103 | together may not be supported. It is common to use one of the methods |
| 104 | at once, not both. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 105 | |
cliechti | 07709e4 | 2010-05-21 00:30:09 +0000 | [diff] [blame] | 106 | *dsrdtr* is not supported by all platforms (silently ignored). Setting |
| 107 | it to ``None`` has the effect that its state follows *rtscts*. |
| 108 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 109 | Also consider using the function :func:`serial_for_url` instead of |
| 110 | creating Serial instances directly. |
| 111 | |
cliechti | 5c72e4d | 2010-07-21 14:04:54 +0000 | [diff] [blame] | 112 | .. versionchanged:: 2.5 |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 113 | *dsrdtr* now defaults to ``False`` (instead of *None*) |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 114 | .. versionchanged:: 3.0 numbers as *port* argument are no longer supported |
Chris Liechti | 1c4bc81 | 2017-03-08 02:44:04 +0100 | [diff] [blame] | 115 | .. versionadded:: 3.3 ``exclusive`` flag |
cliechti | 5c72e4d | 2010-07-21 14:04:54 +0000 | [diff] [blame] | 116 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 117 | .. method:: open() |
| 118 | |
Chris Liechti | aefbf03 | 2016-09-30 23:57:22 +0200 | [diff] [blame] | 119 | Open port. The state of :attr:`rts` and :attr:`dtr` is applied. |
| 120 | |
Chris Liechti | 5e953c3 | 2016-09-28 18:18:25 +0200 | [diff] [blame] | 121 | .. note:: |
Chris Liechti | aefbf03 | 2016-09-30 23:57:22 +0200 | [diff] [blame] | 122 | |
Chris Liechti | 5e953c3 | 2016-09-28 18:18:25 +0200 | [diff] [blame] | 123 | Some OS and/or drivers may activate RTS and or DTR automatically, |
| 124 | as soon as the port is opened. There may be a glitch on RTS/DTR |
| 125 | when :attr:`rts`` or :attr:`dtr` are set differently from their |
| 126 | default value (``True`` / active). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 127 | |
Chris Liechti | aefbf03 | 2016-09-30 23:57:22 +0200 | [diff] [blame] | 128 | .. note:: |
| 129 | |
| 130 | For compatibility reasons, no error is reported when applying |
| 131 | :attr:`rts` or :attr:`dtr` fails on POSIX due to EINVAL (22) or |
| 132 | ENOTTY (25). |
| 133 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 134 | .. method:: close() |
| 135 | |
| 136 | Close port immediately. |
| 137 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 138 | .. method:: __del__() |
| 139 | |
| 140 | Destructor, close port when serial port instance is freed. |
| 141 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 142 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 143 | The following methods may raise :exc:`SerialException` when applied to a closed |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 144 | port. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 145 | |
| 146 | .. method:: read(size=1) |
| 147 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 148 | :param size: Number of bytes to read. |
| 149 | :return: Bytes read from the port. |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 150 | :rtype: bytes |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 151 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 152 | Read *size* bytes from the serial port. If a timeout is set it may |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 153 | return less characters as requested. With no timeout it will block |
| 154 | until the requested number of bytes is read. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 155 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 156 | .. versionchanged:: 2.5 |
| 157 | Returns an instance of :class:`bytes` when available (Python 2.6 |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 158 | and newer) and :class:`str` otherwise. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 159 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 160 | .. method:: write(data) |
| 161 | |
| 162 | :param data: Data to send. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 163 | :return: Number of bytes written. |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 164 | :rtype: int |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 165 | :exception SerialTimeoutException: |
| 166 | In case a write timeout is configured for the port and the time is |
| 167 | exceeded. |
| 168 | |
Chris Liechti | 9ad044a | 2015-12-17 19:43:59 +0100 | [diff] [blame] | 169 | Write the bytes *data* to the port. This should be of type ``bytes`` |
| 170 | (or compatible such as ``bytearray`` or ``memoryview``). Unicode |
| 171 | strings must be encoded (e.g. ``'hello'.encode('utf-8'``). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 172 | |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 173 | .. versionchanged:: 2.5 |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 174 | Accepts instances of :class:`bytes` and :class:`bytearray` when |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 175 | available (Python 2.6 and newer) and :class:`str` otherwise. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 176 | |
cliechti | 9a1c695 | 2009-10-20 22:18:28 +0000 | [diff] [blame] | 177 | .. versionchanged:: 2.5 |
| 178 | Write returned ``None`` in previous versions. |
| 179 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 180 | .. method:: flush() |
| 181 | |
| 182 | Flush of file like objects. In this case, wait until all data is |
| 183 | written. |
| 184 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 185 | .. attribute:: in_waiting |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 186 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 187 | :getter: Get the number of bytes in the input buffer |
| 188 | :type: int |
| 189 | |
| 190 | Return the number of bytes in the receive buffer. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 191 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 192 | .. versionchanged:: 3.0 changed to property from ``inWaiting()`` |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 193 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 194 | .. attribute:: out_waiting |
| 195 | |
| 196 | :getter: Get the number of bytes in the output buffer |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 197 | :type: int |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 198 | :platform: Posix |
| 199 | :platform: Windows |
| 200 | |
| 201 | Return the number of bytes in the output buffer. |
| 202 | |
| 203 | .. versionchanged:: 2.7 (Posix support added) |
| 204 | .. versionchanged:: 3.0 changed to property from ``outWaiting()`` |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 205 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 206 | .. method:: reset_input_buffer() |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 207 | |
Chris Liechti | 4f988d4 | 2017-03-20 23:59:40 +0100 | [diff] [blame^] | 208 | Flush input buffer, discarding all its contents. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 209 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 210 | .. versionchanged:: 3.0 renamed from ``flushInput()`` |
| 211 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 212 | .. method:: reset_output_buffer() |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 213 | |
| 214 | Clear output buffer, aborting the current output and |
| 215 | discarding all that is in the buffer. |
| 216 | |
Chris Liechti | 48e40e9 | 2016-05-24 00:09:12 +0200 | [diff] [blame] | 217 | Note, for some USB serial adapters, this may only flush the buffer of |
| 218 | the OS and not all the data that may be present in the USB part. |
| 219 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 220 | .. versionchanged:: 3.0 renamed from ``flushOutput()`` |
| 221 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 222 | .. method:: send_break(duration=0.25) |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 223 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 224 | :param float duration: Time to activate the BREAK condition. |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 225 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 226 | Send break condition. Timed, returns to idle state after given |
| 227 | duration. |
| 228 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 229 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 230 | .. attribute:: break_condition |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 231 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 232 | :getter: Get the current BREAK state |
| 233 | :setter: Control the BREAK state |
| 234 | :type: bool |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 235 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 236 | When set to ``True`` activate BREAK condition, else disable. |
| 237 | Controls TXD. When active, no transmitting is possible. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 238 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 239 | .. attribute:: rts |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 240 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 241 | :setter: Set the state of the RTS line |
| 242 | :getter: Return the state of the RTS line |
| 243 | :type: bool |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 244 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 245 | Set RTS line to specified logic level. It is possible to assign this |
| 246 | value before opening the serial port, then the value is applied uppon |
Chris Liechti | 5e953c3 | 2016-09-28 18:18:25 +0200 | [diff] [blame] | 247 | :meth:`open` (with restrictions, see :meth:`open`). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 248 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 249 | .. attribute:: dtr |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 250 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 251 | :setter: Set the state of the DTR line |
| 252 | :getter: Return the state of the DTR line |
| 253 | :type: bool |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 254 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 255 | Set DTR line to specified logic level. It is possible to assign this |
| 256 | value before opening the serial port, then the value is applied uppon |
Chris Liechti | 5e953c3 | 2016-09-28 18:18:25 +0200 | [diff] [blame] | 257 | :meth:`open` (with restrictions, see :meth:`open`). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 258 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 259 | Read-only attributes: |
| 260 | |
| 261 | .. attribute:: name |
| 262 | |
| 263 | :getter: Device name. |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 264 | :type: str |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 265 | |
| 266 | .. versionadded:: 2.5 |
| 267 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 268 | .. attribute:: cts |
| 269 | |
| 270 | :getter: Get the state of the CTS line |
| 271 | :type: bool |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 272 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 273 | Return the state of the CTS line. |
| 274 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 275 | .. attribute:: dsr |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 276 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 277 | :getter: Get the state of the DSR line |
| 278 | :type: bool |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 279 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 280 | Return the state of the DSR line. |
| 281 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 282 | .. attribute:: ri |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 283 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 284 | :getter: Get the state of the RI line |
| 285 | :type: bool |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 286 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 287 | Return the state of the RI line. |
| 288 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 289 | .. attribute:: cd |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 290 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 291 | :getter: Get the state of the CD line |
| 292 | :type: bool |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 293 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 294 | Return the state of the CD line |
| 295 | |
Matthew West | fa58a97 | 2016-07-08 14:27:04 -0700 | [diff] [blame] | 296 | .. attribute:: is_open |
Chris Liechti | aefbf03 | 2016-09-30 23:57:22 +0200 | [diff] [blame] | 297 | |
Chris Liechti | 5e953c3 | 2016-09-28 18:18:25 +0200 | [diff] [blame] | 298 | :getter: Get the state of the serial port, whether it's open. |
| 299 | :type: bool |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 300 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 301 | New values can be assigned to the following attributes (properties), the |
| 302 | port will be reconfigured, even if it's opened at that time: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 303 | |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 304 | |
| 305 | .. attribute:: port |
| 306 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 307 | :type: str |
| 308 | |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 309 | Read or write port. When the port is already open, it will be closed |
| 310 | and reopened with the new setting. |
| 311 | |
| 312 | .. attribute:: baudrate |
| 313 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 314 | :getter: Get current baud rate |
| 315 | :setter: Set new baud rate |
| 316 | :type: int |
| 317 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 318 | Read or write current baud rate setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 319 | |
| 320 | .. attribute:: bytesize |
| 321 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 322 | :getter: Get current byte size |
| 323 | :setter: Set new byte size. Possible values: |
| 324 | :const:`FIVEBITS`, :const:`SIXBITS`, :const:`SEVENBITS`, |
| 325 | :const:`EIGHTBITS` |
| 326 | :type: int |
| 327 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 328 | Read or write current data byte size setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 329 | |
| 330 | .. attribute:: parity |
| 331 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 332 | :getter: Get current parity setting |
| 333 | :setter: Set new parity mode. Possible values: |
| 334 | :const:`PARITY_NONE`, :const:`PARITY_EVEN`, :const:`PARITY_ODD` |
| 335 | :const:`PARITY_MARK`, :const:`PARITY_SPACE` |
| 336 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 337 | Read or write current parity setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 338 | |
| 339 | .. attribute:: stopbits |
| 340 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 341 | :getter: Get current stop bit setting |
| 342 | :setter: Set new stop bit setting. Possible values: |
| 343 | :const:`STOPBITS_ONE`, :const:`STOPBITS_ONE_POINT_FIVE`, |
| 344 | :const:`STOPBITS_TWO` |
| 345 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 346 | Read or write current stop bit width setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 347 | |
| 348 | .. attribute:: timeout |
| 349 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 350 | :getter: Get current read timeout setting |
| 351 | :setter: Set read timeout |
| 352 | :type: float (seconds) |
| 353 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 354 | Read or write current read timeout setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 355 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 356 | .. attribute:: write_timeout |
| 357 | |
| 358 | :getter: Get current write timeout setting |
| 359 | :setter: Set write timeout |
| 360 | :type: float (seconds) |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 361 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 362 | Read or write current write timeout setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 363 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 364 | .. versionchanged:: 3.0 renamed from ``writeTimeout`` |
| 365 | |
| 366 | .. attribute:: inter_byte_timeout |
| 367 | |
| 368 | :getter: Get current inter byte timeout setting |
| 369 | :setter: Disable (``None``) or enable the inter byte timeout |
| 370 | :type: float or None |
| 371 | |
| 372 | Read or write current inter byte timeout setting. |
| 373 | |
| 374 | .. versionchanged:: 3.0 renamed from ``interCharTimeout`` |
| 375 | |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 376 | .. attribute:: xonxoff |
| 377 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 378 | :getter: Get current software flow control setting |
| 379 | :setter: Enable or disable software flow control |
| 380 | :type: bool |
| 381 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 382 | Read or write current software flow control rate setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 383 | |
| 384 | .. attribute:: rtscts |
| 385 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 386 | :getter: Get current hardware flow control setting |
| 387 | :setter: Enable or disable hardware flow control |
| 388 | :type: bool |
| 389 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 390 | Read or write current hardware flow control setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 391 | |
| 392 | .. attribute:: dsrdtr |
| 393 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 394 | :getter: Get current hardware flow control setting |
| 395 | :setter: Enable or disable hardware flow control |
| 396 | :type: bool |
| 397 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 398 | Read or write current hardware flow control setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 399 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 400 | .. attribute:: rs485_mode |
| 401 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 402 | :getter: Get the current RS485 settings |
| 403 | :setter: Disable (``None``) or enable the RS485 settings |
| 404 | :type: :class:`rs485.RS485Settings` or ``None`` |
| 405 | :platform: Posix (Linux, limited set of hardware) |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 406 | :platform: Windows (only RTS on TX possible) |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 407 | |
| 408 | Attribute to configure RS485 support. When set to an instance of |
| 409 | :class:`rs485.RS485Settings` and supported by OS, RTS will be active |
| 410 | when data is sent and inactive otherwise (for reception). The |
| 411 | :class:`rs485.RS485Settings` class provides additional settings |
| 412 | supported on some platforms. |
| 413 | |
| 414 | .. versionadded:: 3.0 |
| 415 | |
| 416 | |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 417 | The following constants are also provided: |
| 418 | |
| 419 | .. attribute:: BAUDRATES |
| 420 | |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 421 | A list of valid baud rates. The list may be incomplete, such that higher |
| 422 | and/or intermediate baud rates may also be supported by the device |
| 423 | (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 424 | |
| 425 | .. attribute:: BYTESIZES |
| 426 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 427 | A list of valid byte sizes for the device (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 428 | |
| 429 | .. attribute:: PARITIES |
| 430 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 431 | A list of valid parities for the device (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 432 | |
| 433 | .. attribute:: STOPBITS |
| 434 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 435 | A list of valid stop bit widths for the device (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 436 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 437 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 438 | The following methods are for compatibility with the :mod:`io` library. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 439 | |
| 440 | .. method:: readable() |
| 441 | |
| 442 | :return: True |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 443 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 444 | .. versionadded:: 2.5 |
| 445 | |
| 446 | .. method:: writable() |
| 447 | |
| 448 | :return: True |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 449 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 450 | .. versionadded:: 2.5 |
| 451 | |
| 452 | .. method:: seekable() |
| 453 | |
| 454 | :return: False |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 455 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 456 | .. versionadded:: 2.5 |
| 457 | |
| 458 | .. method:: readinto(b) |
| 459 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 460 | :param b: bytearray or array instance |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 461 | :return: Number of byte read |
| 462 | |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 463 | Read up to len(b) bytes into :class:`bytearray` *b* and return the |
| 464 | number of bytes read. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 465 | |
| 466 | .. versionadded:: 2.5 |
| 467 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 468 | The port settings can be read and written as dictionary. The following |
| 469 | keys are supported: ``write_timeout``, ``inter_byte_timeout``, |
| 470 | ``dsrdtr``, ``baudrate``, ``timeout``, ``parity``, ``bytesize``, |
| 471 | ``rtscts``, ``stopbits``, ``xonxoff`` |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 472 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 473 | .. method:: get_settings() |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 474 | |
| 475 | :return: a dictionary with current port settings. |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 476 | :rtype: dict |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 477 | |
| 478 | Get a dictionary with port settings. This is useful to backup the |
| 479 | current settings so that a later point in time they can be restored |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 480 | using :meth:`apply_settings`. |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 481 | |
Chris Liechti | 19688bf | 2016-05-06 23:48:36 +0200 | [diff] [blame] | 482 | Note that the state of control lines (RTS/DTR) are not part of the |
| 483 | settings. |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 484 | |
| 485 | .. versionadded:: 2.5 |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 486 | .. versionchanged:: 3.0 renamed from ``getSettingsDict`` |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 487 | |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 488 | .. method:: apply_settings(d) |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 489 | |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 490 | :param dict d: a dictionary with port settings. |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 491 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 492 | Applies a dictionary that was created by :meth:`get_settings`. Only |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 493 | changes are applied and when a key is missing, it means that the |
| 494 | setting stays unchanged. |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 495 | |
| 496 | Note that control lines (RTS/DTR) are not changed. |
| 497 | |
| 498 | .. versionadded:: 2.5 |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 499 | .. versionchanged:: 3.0 renamed from ``applySettingsDict`` |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 500 | |
Chris Liechti | c9f8996 | 2016-08-12 21:04:37 +0200 | [diff] [blame] | 501 | |
Chris Liechti | 9e20574 | 2016-10-31 22:37:13 +0100 | [diff] [blame] | 502 | .. _context-manager: |
| 503 | |
Chris Liechti | c9f8996 | 2016-08-12 21:04:37 +0200 | [diff] [blame] | 504 | This class can be used as context manager. The serial port is closed when |
| 505 | the context is left. |
| 506 | |
| 507 | .. method:: __enter__() |
| 508 | |
| 509 | :returns: Serial instance |
| 510 | |
| 511 | Returns the instance that was used in the ``with`` statement. |
| 512 | |
| 513 | Example: |
| 514 | |
| 515 | >>> with serial.serial_for_url(port) as s: |
| 516 | ... s.write(b'hello') |
| 517 | |
| 518 | Here no port argument is given, so it is not opened automatically: |
| 519 | |
| 520 | >>> with serial.Serial() as s: |
| 521 | ... s.port = ... |
| 522 | ... s.open() |
| 523 | ... s.write(b'hello') |
| 524 | |
| 525 | |
| 526 | .. method:: __exit__(exc_type, exc_val, exc_tb) |
| 527 | |
Chris Liechti | 4f988d4 | 2017-03-20 23:59:40 +0100 | [diff] [blame^] | 528 | Closes serial port (exceptions are not handled by ``__exit__``). |
Chris Liechti | c9f8996 | 2016-08-12 21:04:37 +0200 | [diff] [blame] | 529 | |
| 530 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 531 | Platform specific methods. |
| 532 | |
cliechti | c648da9 | 2011-12-29 01:17:51 +0000 | [diff] [blame] | 533 | .. warning:: Programs using the following methods and attributes are not |
| 534 | portable to other platforms! |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 535 | |
| 536 | .. method:: nonblocking() |
| 537 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 538 | :platform: Posix |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 539 | |
Chris Liechti | 5984842 | 2016-06-04 22:28:14 +0200 | [diff] [blame] | 540 | .. deprecated:: 3.2 |
| 541 | The serial port is already opened in this mode. This method is not |
| 542 | needed and going away. |
| 543 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 544 | |
| 545 | .. method:: fileno() |
| 546 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 547 | :platform: Posix |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 548 | :return: File descriptor. |
| 549 | |
| 550 | Return file descriptor number for the port that is opened by this object. |
| 551 | It is useful when serial ports are used with :mod:`select`. |
| 552 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 553 | .. method:: set_input_flow_control(enable) |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 554 | |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 555 | :platform: Posix |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 556 | :param bool enable: Set flow control state. |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 557 | |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 558 | Manually control flow - when software flow control is enabled. |
| 559 | |
| 560 | This will send XON (true) and XOFF (false) to the other device. |
| 561 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 562 | .. versionadded:: 2.7 (Posix support added) |
| 563 | .. versionchanged:: 3.0 renamed from ``flowControlOut`` |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 564 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 565 | .. method:: set_output_flow_control(enable) |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 566 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 567 | :platform: Posix (HW and SW flow control) |
| 568 | :platform: Windows (SW flow control only) |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 569 | :param bool enable: Set flow control state. |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 570 | |
| 571 | Manually control flow of outgoing data - when hardware or software flow |
| 572 | control is enabled. |
| 573 | |
| 574 | Sending will be suspended when called with ``False`` and enabled when |
| 575 | called with ``True``. |
| 576 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 577 | .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``) |
| 578 | .. versionchanged:: 3.0 renamed from ``setXON`` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 579 | |
Chris Liechti | 19688bf | 2016-05-06 23:48:36 +0200 | [diff] [blame] | 580 | .. method:: cancel_read() |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 581 | |
Chris Liechti | 9d89305 | 2016-05-18 22:03:29 +0200 | [diff] [blame] | 582 | :platform: Posix |
Chris Liechti | 19688bf | 2016-05-06 23:48:36 +0200 | [diff] [blame] | 583 | :platform: Windows |
| 584 | |
Jakub Wilk | 772a6fa | 2016-12-20 21:59:27 +0100 | [diff] [blame] | 585 | Cancel a pending read operation from another thread. A blocking |
Chris Liechti | 9d89305 | 2016-05-18 22:03:29 +0200 | [diff] [blame] | 586 | :meth:`read` call is aborted immediately. :meth:`read` will not report |
| 587 | any error but return all data received up to that point (similar to a |
| 588 | timeout). |
| 589 | |
| 590 | On Posix a call to `cancel_read()` may cancel a future :meth:`read` call. |
Chris Liechti | 19688bf | 2016-05-06 23:48:36 +0200 | [diff] [blame] | 591 | |
| 592 | .. versionadded:: 3.1 |
| 593 | |
| 594 | .. method:: cancel_write() |
| 595 | |
Chris Liechti | 9d89305 | 2016-05-18 22:03:29 +0200 | [diff] [blame] | 596 | :platform: Posix |
Chris Liechti | 19688bf | 2016-05-06 23:48:36 +0200 | [diff] [blame] | 597 | :platform: Windows |
| 598 | |
Jakub Wilk | 772a6fa | 2016-12-20 21:59:27 +0100 | [diff] [blame] | 599 | Cancel a pending write operation from another thread. The |
Chris Liechti | 9d89305 | 2016-05-18 22:03:29 +0200 | [diff] [blame] | 600 | :meth:`write` method will return immediately (no error indicated). |
| 601 | However the OS may still be sending from the buffer, a separate call to |
| 602 | :meth:`reset_output_buffer` may be needed. |
| 603 | |
| 604 | On Posix a call to `cancel_write()` may cancel a future :meth:`write` call. |
Chris Liechti | 19688bf | 2016-05-06 23:48:36 +0200 | [diff] [blame] | 605 | |
| 606 | .. versionadded:: 3.1 |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 607 | |
Chris Liechti | 9a99cb2 | 2015-08-30 22:16:50 +0200 | [diff] [blame] | 608 | .. note:: The following members are deprecated and will be removed in a |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 609 | future release. |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 610 | |
| 611 | .. attribute:: portstr |
| 612 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 613 | .. deprecated:: 2.5 use :attr:`name` instead |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 614 | |
| 615 | .. method:: inWaiting() |
| 616 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 617 | .. deprecated:: 3.0 see :attr:`in_waiting` |
| 618 | |
Matthew West | fa58a97 | 2016-07-08 14:27:04 -0700 | [diff] [blame] | 619 | .. method:: isOpen() |
| 620 | |
| 621 | .. deprecated:: 3.0 see :attr:`is_open` |
| 622 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 623 | .. attribute:: writeTimeout |
| 624 | |
| 625 | .. deprecated:: 3.0 see :attr:`write_timeout` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 626 | |
| 627 | .. attribute:: interCharTimeout |
| 628 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 629 | .. deprecated:: 3.0 see :attr:`inter_byte_timeout` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 630 | |
| 631 | .. method:: sendBreak(duration=0.25) |
| 632 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 633 | .. deprecated:: 3.0 see :meth:`send_break` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 634 | |
| 635 | .. method:: flushInput() |
| 636 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 637 | .. deprecated:: 3.0 see :meth:`reset_input_buffer` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 638 | |
| 639 | .. method:: flushOutput() |
| 640 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 641 | .. deprecated:: 3.0 see :meth:`reset_output_buffer` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 642 | |
| 643 | .. method:: setBreak(level=True) |
| 644 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 645 | .. deprecated:: 3.0 see :attr:`break_condition` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 646 | |
| 647 | .. method:: setRTS(level=True) |
| 648 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 649 | .. deprecated:: 3.0 see :attr:`rts` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 650 | |
| 651 | .. method:: setDTR(level=True) |
| 652 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 653 | .. deprecated:: 3.0 see :attr:`dtr` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 654 | |
| 655 | .. method:: getCTS() |
| 656 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 657 | .. deprecated:: 3.0 see :attr:`cts` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 658 | |
| 659 | .. method:: getDSR() |
| 660 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 661 | .. deprecated:: 3.0 see :attr:`dsr` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 662 | |
| 663 | .. method:: getRI() |
| 664 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 665 | .. deprecated:: 3.0 see :attr:`ri` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 666 | |
| 667 | .. method:: getCD() |
| 668 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 669 | .. deprecated:: 3.0 see :attr:`cd` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 670 | |
| 671 | .. method:: getSettingsDict() |
| 672 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 673 | .. deprecated:: 3.0 see :meth:`get_settings` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 674 | |
| 675 | .. method:: applySettingsDict(d) |
| 676 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 677 | .. deprecated:: 3.0 see :meth:`apply_settings` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 678 | |
| 679 | .. method:: outWaiting() |
| 680 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 681 | .. deprecated:: 3.0 see :attr:`out_waiting` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 682 | |
| 683 | .. method:: setXON(level=True) |
| 684 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 685 | .. deprecated:: 3.0 see :meth:`set_output_flow_control` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 686 | |
| 687 | .. method:: flowControlOut(enable) |
| 688 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 689 | .. deprecated:: 3.0 see :meth:`set_input_flow_control` |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 690 | |
cliechti | c648da9 | 2011-12-29 01:17:51 +0000 | [diff] [blame] | 691 | .. attribute:: rtsToggle |
| 692 | |
| 693 | :platform: Windows |
| 694 | |
| 695 | Attribute to configure RTS toggle control setting. When enabled and |
| 696 | supported by OS, RTS will be active when data is available and inactive |
| 697 | if no data is available. |
| 698 | |
| 699 | .. versionadded:: 2.6 |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 700 | .. versionchanged:: 3.0 (removed, see :attr:`rs485_mode` instead) |
cliechti | c648da9 | 2011-12-29 01:17:51 +0000 | [diff] [blame] | 701 | |
| 702 | |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 703 | Implementation detail: some attributes and functions are provided by the |
| 704 | class :class:`SerialBase` and some by the platform specific class and |
| 705 | others by the base class mentioned above. |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 706 | |
Chris Liechti | 4f988d4 | 2017-03-20 23:59:40 +0100 | [diff] [blame^] | 707 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 708 | RS485 support |
| 709 | ------------- |
| 710 | The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to |
| 711 | enable RS485 specific support on some platforms. Currently Windows and Linux |
| 712 | (only a small number of devices) are supported. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 713 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 714 | :attr:`Serial.rs485_mode` needs to be set to an instance of |
| 715 | :class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 716 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 717 | Usage:: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 718 | |
Chris Liechti | 1153824 | 2016-09-06 22:20:09 +0200 | [diff] [blame] | 719 | import serial |
| 720 | import serial.rs485 |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 721 | ser = serial.Serial(...) |
| 722 | ser.rs485_mode = serial.rs485.RS485Settings(...) |
| 723 | ser.write(b'hello') |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 724 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 725 | There is a subclass :class:`rs485.RS485` available to emulate the RS485 support |
Chris Liechti | 1153824 | 2016-09-06 22:20:09 +0200 | [diff] [blame] | 726 | on regular serial ports (``serial.rs485`` needs to be imported). |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 727 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 728 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 729 | .. class:: rs485.RS485Settings |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 730 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 731 | A class that holds RS485 specific settings which are supported on |
| 732 | some platforms. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 733 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 734 | .. versionadded:: 3.0 |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 735 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 736 | .. method:: __init__(rts_level_for_tx=True, rts_level_for_rx=False, loopback=False, delay_before_tx=None, delay_before_rx=None): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 737 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 738 | :param bool rts_level_for_tx: |
| 739 | RTS level for transmission |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 740 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 741 | :param bool rts_level_for_rx: |
| 742 | RTS level for reception |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 743 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 744 | :param bool loopback: |
| 745 | When set to ``True`` transmitted data is also received. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 746 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 747 | :param float delay_before_tx: |
| 748 | Delay after setting RTS but before transmission starts |
| 749 | |
| 750 | :param float delay_before_rx: |
| 751 | Delay after transmission ends and resetting RTS |
| 752 | |
| 753 | .. attribute:: rts_level_for_tx |
| 754 | |
| 755 | RTS level for transmission. |
| 756 | |
| 757 | .. attribute:: rts_level_for_rx |
| 758 | |
| 759 | RTS level for reception. |
| 760 | |
| 761 | .. attribute:: loopback |
| 762 | |
| 763 | When set to ``True`` transmitted data is also received. |
| 764 | |
| 765 | .. attribute:: delay_before_tx |
| 766 | |
| 767 | Delay after setting RTS but before transmission starts (seconds as float). |
| 768 | |
| 769 | .. attribute:: delay_before_rx |
| 770 | |
| 771 | Delay after transmission ends and resetting RTS (seconds as float). |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 772 | |
| 773 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 774 | .. class:: rs485.RS485 |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 775 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 776 | A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS |
| 777 | according to the RS485 settings. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 778 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 779 | Usage:: |
| 780 | |
| 781 | ser = serial.rs485.RS485(...) |
| 782 | ser.rs485_mode = serial.rs485.RS485Settings(...) |
| 783 | ser.write(b'hello') |
| 784 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 785 | .. warning:: This may work unreliably on some serial ports (control signals not |
| 786 | synchronized or delayed compared to data). Using delays may be unreliable |
| 787 | (varying times, larger than expected) as the OS may not support very fine |
| 788 | grained delays (no smaller than in the order of tens of milliseconds). |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 789 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 790 | .. note:: Some implementations support this natively in the class |
| 791 | :class:`Serial`. Better performance can be expected when the native version |
| 792 | is used. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 793 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 794 | .. note:: The loopback property is ignored by this implementation. The actual |
| 795 | behavior depends on the used hardware. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 796 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 797 | |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 798 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 799 | :rfc:`2217` Network ports |
| 800 | ------------------------- |
| 801 | |
| 802 | .. warning:: This implementation is currently in an experimental state. Use |
| 803 | at your own risk. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 804 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 805 | .. class:: rfc2217.Serial |
| 806 | |
Chris Liechti | 2a1befc | 2015-10-21 17:37:08 +0200 | [diff] [blame] | 807 | This implements a :rfc:`2217` compatible client. Port names are :ref:`URL |
| 808 | <URLs>` in the form: ``rfc2217://<host>:<port>[?<option>[&<option>]]`` |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 809 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 810 | This class API is compatible to :class:`Serial` with a few exceptions: |
| 811 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 812 | - ``write_timeout`` is not implemented |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 813 | - The current implementation starts a thread that keeps reading from the |
| 814 | (internal) socket. The thread is managed automatically by the |
| 815 | :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`. |
| 816 | However it may be a problem for user applications that like to use select |
| 817 | instead of threads. |
| 818 | |
| 819 | Due to the nature of the network and protocol involved there are a few |
| 820 | extra points to keep in mind: |
| 821 | |
| 822 | - All operations have an additional latency time. |
| 823 | - Setting control lines (RTS/CTS) needs more time. |
| 824 | - Reading the status lines (DSR/DTR etc.) returns a cached value. When that |
| 825 | cache is updated depends entirely on the server. The server itself may |
| 826 | implement a polling at a certain rate and quick changes may be invisible. |
| 827 | - The network layer also has buffers. This means that :meth:`flush`, |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 828 | :meth:`reset_input_buffer` and :meth:`reset_output_buffer` may work with |
| 829 | additional delay. Likewise :attr:`in_waiting` returns the size of the |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 830 | data arrived at the objects internal buffer and excludes any bytes in the |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 831 | network buffers or any server side buffer. |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 832 | - Closing and immediately reopening the same port may fail due to time |
| 833 | needed by the server to get ready again. |
| 834 | |
| 835 | Not implemented yet / Possible problems with the implementation: |
| 836 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 837 | - :rfc:`2217` flow control between client and server (objects internal |
| 838 | buffer may eat all your memory when never read). |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 839 | - No authentication support (servers may not prompt for a password etc.) |
| 840 | - No encryption. |
| 841 | |
| 842 | Due to lack of authentication and encryption it is not suitable to use this |
| 843 | client for connections across the internet and should only be used in |
| 844 | controlled environments. |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 845 | |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 846 | .. versionadded:: 2.5 |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 847 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 848 | |
| 849 | .. class:: rfc2217.PortManager |
| 850 | |
| 851 | This class provides helper functions for implementing :rfc:`2217` |
| 852 | compatible servers. |
| 853 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 854 | Basically, it implements everything needed for the :rfc:`2217` protocol. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 855 | It just does not open sockets and read/write to serial ports (though it |
| 856 | changes other port settings). The user of this class must take care of the |
| 857 | data transmission itself. The reason for that is, that this way, this class |
| 858 | supports all programming models such as threads and select. |
| 859 | |
| 860 | Usage examples can be found in the examples where two TCP/IP - serial |
| 861 | converters are shown, one using threads (the single port server) and an |
| 862 | other using select (the multi port server). |
| 863 | |
| 864 | .. note:: Each new client connection must create a new instance as this |
| 865 | object (and the :rfc:`2217` protocol) has internal state. |
| 866 | |
| 867 | .. method:: __init__(serial_port, connection, debug_output=False) |
| 868 | |
| 869 | :param serial_port: a :class:`Serial` instance that is managed. |
| 870 | :param connection: an object implementing :meth:`write`, used to write |
| 871 | to the network. |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 872 | :param debug_output: enables debug messages: a :class:`logging.Logger` |
| 873 | instance or None. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 874 | |
| 875 | Initializes the Manager and starts negotiating with client in Telnet |
| 876 | and :rfc:`2217` protocol. The negotiation starts immediately so that |
| 877 | the class should be instantiated in the moment the client connects. |
| 878 | |
| 879 | The *serial_port* can be controlled by :rfc:`2217` commands. This |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 880 | object will modify the port settings (baud rate etc.) and control lines |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 881 | (RTS/DTR) send BREAK etc. when the corresponding commands are found by |
| 882 | the :meth:`filter` method. |
| 883 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 884 | The *connection* object must implement a :meth:`write` function. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 885 | This function must ensure that *data* is written at once (no user data |
| 886 | mixed in, i.e. it must be thread-safe). All data must be sent in its |
| 887 | raw form (:meth:`escape` must not be used) as it is used to send Telnet |
| 888 | and :rfc:`2217` control commands. |
| 889 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 890 | For diagnostics of the connection or the implementation, *debug_output* |
| 891 | can be set to an instance of a :class:`logging.Logger` (e.g. |
| 892 | ``logging.getLogger('rfc2217.server')``). The caller should configure |
| 893 | the logger using ``setLevel`` for the desired detail level of the logs. |
| 894 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 895 | .. method:: escape(data) |
| 896 | |
| 897 | :param data: data to be sent over the network. |
| 898 | :return: data, escaped for Telnet/:rfc:`2217` |
| 899 | |
| 900 | A generator that escapes all data to be compatible with :rfc:`2217`. |
| 901 | Implementors of servers should use this function to process all data |
| 902 | sent over the network. |
| 903 | |
| 904 | The function returns a generator which can be used in ``for`` loops. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 905 | It can be converted to bytes using :func:`serial.to_bytes`. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 906 | |
| 907 | .. method:: filter(data) |
| 908 | |
| 909 | :param data: data read from the network, including Telnet and |
| 910 | :rfc:`2217` controls. |
| 911 | :return: data, free from Telnet and :rfc:`2217` controls. |
| 912 | |
| 913 | A generator that filters and processes all data related to :rfc:`2217`. |
| 914 | Implementors of servers should use this function to process all data |
| 915 | received from the network. |
| 916 | |
| 917 | The function returns a generator which can be used in ``for`` loops. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 918 | It can be converted to bytes using :func:`serial.to_bytes`. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 919 | |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 920 | .. method:: check_modem_lines(force_notification=False) |
| 921 | |
| 922 | :param force_notification: Set to false. Parameter is for internal use. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 923 | |
| 924 | This function needs to be called periodically (e.g. every second) when |
| 925 | the server wants to send NOTIFY_MODEMSTATE messages. This is required |
| 926 | to support the client for reading CTS/DSR/RI/CD status lines. |
| 927 | |
| 928 | The function reads the status line and issues the notifications |
| 929 | automatically. |
| 930 | |
| 931 | .. versionadded:: 2.5 |
| 932 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 933 | .. seealso:: |
| 934 | |
| 935 | :rfc:`2217` - Telnet Com Port Control Option |
| 936 | |
| 937 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 938 | Exceptions |
| 939 | ========== |
| 940 | |
| 941 | .. exception:: SerialException |
| 942 | |
| 943 | Base class for serial port exceptions. |
| 944 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 945 | .. versionchanged:: 2.5 |
cliechti | 4cb9466 | 2013-10-17 03:17:50 +0000 | [diff] [blame] | 946 | Now derives from :exc:`IOError` instead of :exc:`Exception` |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 947 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 948 | .. exception:: SerialTimeoutException |
| 949 | |
| 950 | Exception that is raised on write timeouts. |
| 951 | |
| 952 | |
| 953 | Constants |
| 954 | ========= |
| 955 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 956 | *Parity* |
| 957 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 958 | .. data:: PARITY_NONE |
| 959 | .. data:: PARITY_EVEN |
| 960 | .. data:: PARITY_ODD |
| 961 | .. data:: PARITY_MARK |
| 962 | .. data:: PARITY_SPACE |
| 963 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 964 | *Stop bits* |
| 965 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 966 | .. data:: STOPBITS_ONE |
| 967 | .. data:: STOPBITS_ONE_POINT_FIVE |
| 968 | .. data:: STOPBITS_TWO |
| 969 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 970 | Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop |
cliechti | 41be039 | 2010-01-02 03:02:38 +0000 | [diff] [blame] | 971 | bits. |
| 972 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 973 | *Byte size* |
| 974 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 975 | .. data:: FIVEBITS |
| 976 | .. data:: SIXBITS |
| 977 | .. data:: SEVENBITS |
| 978 | .. data:: EIGHTBITS |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 979 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 980 | |
| 981 | *Others* |
| 982 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 983 | Default control characters (instances of :class:`bytes` for Python 3.0+) for |
cliechti | daf47ba | 2009-07-28 01:28:16 +0000 | [diff] [blame] | 984 | software flow control: |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 985 | |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 986 | .. data:: XON |
| 987 | .. data:: XOFF |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 988 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 989 | Module version: |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 990 | |
| 991 | .. data:: VERSION |
| 992 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 993 | A string indicating the pySerial version, such as ``3.0``. |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 994 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 995 | .. versionadded:: 2.3 |
| 996 | |
cliechti | 44eb98f | 2011-03-21 21:41:10 +0000 | [diff] [blame] | 997 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 998 | Module functions and attributes |
| 999 | =============================== |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 1000 | |
| 1001 | .. function:: device(number) |
| 1002 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 1003 | .. versionchanged:: 3.0 removed, use ``serial.tools.list_ports`` instead |
| 1004 | |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 1005 | |
cliechti | e3ab353 | 2009-08-05 12:40:38 +0000 | [diff] [blame] | 1006 | .. function:: serial_for_url(url, \*args, \*\*kwargs) |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 1007 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 1008 | :param url: Device name, number or :ref:`URL <URLs>` |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 1009 | :param do_not_open: When set to true, the serial port is not opened. |
| 1010 | :return: an instance of :class:`Serial` or a compatible object. |
| 1011 | |
| 1012 | Get a native or a :rfc:`2217` implementation of the Serial class, depending |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 1013 | on port/url. This factory function is useful when an application wants |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 1014 | to support both, local ports and remote ports. There is also support |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 1015 | for other types, see :ref:`URL <URLs>` section. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 1016 | |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 1017 | The port is not opened when a keyword parameter called *do_not_open* is |
| 1018 | given and true, by default it is opened. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 1019 | |
| 1020 | .. versionadded:: 2.5 |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 1021 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 1022 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1023 | .. attribute:: protocol_handler_packages |
| 1024 | |
| 1025 | This attribute is a list of package names (strings) that is searched for |
| 1026 | protocol handlers. |
| 1027 | |
| 1028 | e.g. we want to support a URL ``foobar://``. A module |
| 1029 | ``my_handlers.protocol_foobar`` is provided by the user:: |
| 1030 | |
| 1031 | serial.protocol_handler_packages.append("my_handlers") |
| 1032 | s = serial.serial_for_url("foobar://") |
| 1033 | |
| 1034 | For an URL starting with ``XY://`` is the function :func:`serial_for_url` |
| 1035 | attempts to import ``PACKAGE.protocol_XY`` with each candidate for |
| 1036 | ``PACKAGE`` from this list. |
| 1037 | |
| 1038 | .. versionadded:: 2.6 |
| 1039 | |
| 1040 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 1041 | .. function:: to_bytes(sequence) |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 1042 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 1043 | :param sequence: bytes, bytearray or memoryview |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 1044 | :returns: an instance of ``bytes`` |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 1045 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1046 | Convert a sequence to a ``bytes`` type. This is used to write code that is |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 1047 | compatible to Python 2.x and 3.x. |
| 1048 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1049 | In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 1050 | ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1051 | ``bytes(sequence)`` doesn't work for all versions of Python. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 1052 | |
| 1053 | This function is used internally and in the unit tests. |
| 1054 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1055 | .. versionadded:: 2.5 |
| 1056 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 1057 | .. function:: iterbytes(sequence) |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 1058 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 1059 | :param sequence: bytes, bytearray or memoryview |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 1060 | :returns: a generator that yields bytes |
| 1061 | |
| 1062 | Some versions of Python (3.x) would return integers instead of bytes when |
Chris Liechti | 4f988d4 | 2017-03-20 23:59:40 +0100 | [diff] [blame^] | 1063 | looping over an instance of ``bytes``. This helper function ensures that |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 1064 | bytes are returned. |
| 1065 | |
| 1066 | .. versionadded:: 3.0 |
| 1067 | |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 1068 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1069 | Threading |
| 1070 | ========= |
| 1071 | |
| 1072 | .. module:: serial.threaded |
| 1073 | .. versionadded:: 3.0 |
| 1074 | |
| 1075 | .. warning:: This implementation is currently in an experimental state. Use |
| 1076 | at your own risk. |
| 1077 | |
| 1078 | This module provides classes to simplify working with threads and protocols. |
| 1079 | |
| 1080 | .. class:: Protocol |
| 1081 | |
Chris Liechti | 5665d57 | 2015-10-13 23:50:01 +0200 | [diff] [blame] | 1082 | Protocol as used by the :class:`ReaderThread`. This base class provides empty |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1083 | implementations of all methods. |
| 1084 | |
| 1085 | |
| 1086 | .. method:: connection_made(transport) |
| 1087 | |
| 1088 | :param transport: instance used to write to serial port. |
| 1089 | |
| 1090 | Called when reader thread is started. |
| 1091 | |
| 1092 | .. method:: data_received(data) |
| 1093 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1094 | :param bytes data: received bytes |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1095 | |
| 1096 | Called with snippets received from the serial port. |
| 1097 | |
| 1098 | .. method:: connection_lost(exc) |
| 1099 | |
| 1100 | :param exc: Exception if connection was terminated by error else ``None`` |
| 1101 | |
| 1102 | Called when the serial port is closed or the reader loop terminated |
| 1103 | otherwise. |
| 1104 | |
| 1105 | .. class:: Packetizer(Protocol) |
| 1106 | |
| 1107 | Read binary packets from serial port. Packets are expected to be terminated |
| 1108 | with a ``TERMINATOR`` byte (null byte by default). |
| 1109 | |
| 1110 | The class also keeps track of the transport. |
| 1111 | |
| 1112 | .. attribute:: TERMINATOR = b'\\0' |
| 1113 | |
| 1114 | .. method:: __init__() |
| 1115 | |
| 1116 | .. method:: connection_made(transport) |
| 1117 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1118 | Stores transport. |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1119 | |
| 1120 | .. method:: connection_lost(exc) |
| 1121 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1122 | Forgets transport. |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1123 | |
| 1124 | .. method:: data_received(data) |
| 1125 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1126 | :param bytes data: partial received data |
| 1127 | |
| 1128 | Buffer received data and search for :attr:`TERMINATOR`, when found, |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1129 | call :meth:`handle_packet`. |
| 1130 | |
| 1131 | .. method:: handle_packet(packet) |
| 1132 | |
Chris Liechti | 1c4c599 | 2016-01-18 20:46:00 +0100 | [diff] [blame] | 1133 | :param bytes packet: a packet as defined by ``TERMINATOR`` |
| 1134 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1135 | Process packets - to be overridden by subclassing. |
| 1136 | |
| 1137 | |
| 1138 | .. class:: LineReader(Packetizer) |
| 1139 | |
| 1140 | Read and write (Unicode) lines from/to serial port. |
| 1141 | The encoding is applied. |
| 1142 | |
| 1143 | |
| 1144 | .. attribute:: TERMINATOR = b'\\r\\n' |
Chris Liechti | 1c4c599 | 2016-01-18 20:46:00 +0100 | [diff] [blame] | 1145 | |
| 1146 | Line ending. |
| 1147 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1148 | .. attribute:: ENCODING = 'utf-8' |
Chris Liechti | 1c4c599 | 2016-01-18 20:46:00 +0100 | [diff] [blame] | 1149 | |
| 1150 | Encoding of the send and received data. |
| 1151 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1152 | .. attribute:: UNICODE_HANDLING = 'replace' |
| 1153 | |
Chris Liechti | 1c4c599 | 2016-01-18 20:46:00 +0100 | [diff] [blame] | 1154 | Unicode error handly policy. |
| 1155 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1156 | .. method:: handle_packet(packet) |
| 1157 | |
Chris Liechti | 1c4c599 | 2016-01-18 20:46:00 +0100 | [diff] [blame] | 1158 | :param bytes packet: a packet as defined by ``TERMINATOR`` |
| 1159 | |
| 1160 | In this case it will be a line, calls :meth:`handle_line` after applying |
| 1161 | the :attr:`ENCODING`. |
| 1162 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1163 | .. method:: handle_line(line) |
| 1164 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1165 | :param str line: Unicode string with one line (excluding line terminator) |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1166 | |
| 1167 | Process one line - to be overridden by subclassing. |
| 1168 | |
| 1169 | .. method:: write_line(text) |
| 1170 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1171 | :param str text: Unicode string with one line (excluding line terminator) |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1172 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1173 | Write *text* to the transport. *text* is expected to be a Unicode |
| 1174 | string and the encoding is applied before sending and also the |
| 1175 | :attr:`TERMINATOR` (new line) is appended. |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1176 | |
| 1177 | |
Chris Liechti | 5665d57 | 2015-10-13 23:50:01 +0200 | [diff] [blame] | 1178 | .. class:: ReaderThread(threading.Thread) |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1179 | |
| 1180 | Implement a serial port read loop and dispatch to a Protocol instance (like |
| 1181 | the :class:`asyncio.Protocol`) but do it with threads. |
| 1182 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1183 | Calls to :meth:`close` will close the serial port but it is also possible |
| 1184 | to just :meth:`stop` this thread and continue to use the serial port |
| 1185 | instance otherwise. |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1186 | |
| 1187 | .. method:: __init__(serial_instance, protocol_factory) |
| 1188 | |
| 1189 | :param serial_instance: serial port instance (opened) to be used. |
| 1190 | :param protocol_factory: a callable that returns a Protocol instance |
| 1191 | |
| 1192 | Initialize thread. |
| 1193 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1194 | Note that the ``serial_instance`` 's timeout is set to one second! |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1195 | Other settings are not changed. |
| 1196 | |
| 1197 | .. method:: stop() |
| 1198 | |
| 1199 | Stop the reader thread. |
| 1200 | |
| 1201 | .. method:: run() |
| 1202 | |
| 1203 | The actual reader loop driven by the thread. It calls |
| 1204 | :meth:`Protocol.connection_made`, reads from the serial port calling |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1205 | :meth:`Protocol.data_received` and finally calls :meth:`Protocol.connection_lost` |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1206 | when :meth:`close` is called or an error occurs. |
| 1207 | |
| 1208 | .. method:: write(data) |
| 1209 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1210 | :param bytes data: data to write |
| 1211 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1212 | Thread safe writing (uses lock). |
| 1213 | |
| 1214 | .. method:: close() |
| 1215 | |
| 1216 | Close the serial port and exit reader thread, calls :meth:`stop` (uses lock). |
| 1217 | |
| 1218 | .. method:: connect() |
| 1219 | |
| 1220 | Wait until connection is set up and return the transport and protocol |
| 1221 | instances. |
| 1222 | |
| 1223 | |
| 1224 | This class can be used as context manager, in this case it starts |
| 1225 | the thread and connects automatically. The serial port is closed |
| 1226 | when the context is left. |
| 1227 | |
| 1228 | .. method:: __enter__() |
| 1229 | |
| 1230 | :returns: protocol |
| 1231 | |
| 1232 | Connect and return protocol instance. |
| 1233 | |
| 1234 | .. method:: __exit__(exc_type, exc_val, exc_tb) |
| 1235 | |
| 1236 | Closes serial port. |
| 1237 | |
Chris Liechti | a1436b1 | 2015-10-12 23:19:04 +0200 | [diff] [blame] | 1238 | Example:: |
| 1239 | |
| 1240 | class PrintLines(LineReader): |
| 1241 | def connection_made(self, transport): |
| 1242 | super(PrintLines, self).connection_made(transport) |
| 1243 | sys.stdout.write('port opened\n') |
| 1244 | self.write_line('hello world') |
| 1245 | |
| 1246 | def handle_line(self, data): |
| 1247 | sys.stdout.write('line received: {}\n'.format(repr(data))) |
| 1248 | |
| 1249 | def connection_lost(self, exc): |
| 1250 | if exc: |
| 1251 | traceback.print_exc(exc) |
| 1252 | sys.stdout.write('port closed\n') |
| 1253 | |
| 1254 | ser = serial.serial_for_url('loop://', baudrate=115200, timeout=1) |
Chris Liechti | 5665d57 | 2015-10-13 23:50:01 +0200 | [diff] [blame] | 1255 | with ReaderThread(ser, PrintLines) as protocol: |
Chris Liechti | a1436b1 | 2015-10-12 23:19:04 +0200 | [diff] [blame] | 1256 | protocol.write_line('hello') |
| 1257 | time.sleep(2) |
| 1258 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1259 | |
Chris Liechti | 1f0c9b4 | 2015-09-03 23:49:48 +0200 | [diff] [blame] | 1260 | asyncio |
| 1261 | ======= |
| 1262 | |
Chris Liechti | 2c5a31b | 2016-06-18 22:57:24 +0200 | [diff] [blame] | 1263 | ``asyncio`` was introduced with Python 3.4. Experimental support for pySerial |
| 1264 | is provided via a separate distribution `pyserial-asyncio`_. |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 1265 | |
Jakub Wilk | 772a6fa | 2016-12-20 21:59:27 +0100 | [diff] [blame] | 1266 | It is currently under development, see: |
Chris Liechti | 1f0c9b4 | 2015-09-03 23:49:48 +0200 | [diff] [blame] | 1267 | |
Chris Liechti | 2c5a31b | 2016-06-18 22:57:24 +0200 | [diff] [blame] | 1268 | - http://pyserial-asyncio.readthedocs.io/ |
| 1269 | - https://github.com/pyserial/pyserial-asyncio |
Chris Liechti | 1f0c9b4 | 2015-09-03 23:49:48 +0200 | [diff] [blame] | 1270 | |
Chris Liechti | 2c5a31b | 2016-06-18 22:57:24 +0200 | [diff] [blame] | 1271 | .. _`pyserial-asyncio`: https://pypi.python.org/pypi/pyserial-asyncio |
Chris Liechti | 1f0c9b4 | 2015-09-03 23:49:48 +0200 | [diff] [blame] | 1272 | |