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