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 |
Jakub Wilk | bbe6d83 | 2017-04-08 15:43:36 +0200 | [diff] [blame] | 125 | when :attr:`rts` or :attr:`dtr` are set differently from their |
Chris Liechti | 5e953c3 | 2016-09-28 18:18:25 +0200 | [diff] [blame] | 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 |
Connor Weeks | c9661f7 | 2018-04-06 09:49:15 -0600 | [diff] [blame^] | 246 | value before opening the serial port, then the value is applied upon |
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 |
Connor Weeks | c9661f7 | 2018-04-06 09:49:15 -0600 | [diff] [blame^] | 256 | value before opening the serial port, then the value is applied upon |
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 | |
Chris Liechti | fd70a55 | 2017-07-20 23:46:34 +0200 | [diff] [blame] | 518 | The port is opened automatically: |
Chris Liechti | c9f8996 | 2016-08-12 21:04:37 +0200 | [diff] [blame] | 519 | |
Chris Liechti | fd70a55 | 2017-07-20 23:46:34 +0200 | [diff] [blame] | 520 | >>> port = serial.Serial() |
| 521 | >>> port.port = '...' |
| 522 | >>> with port as s: |
Chris Liechti | c9f8996 | 2016-08-12 21:04:37 +0200 | [diff] [blame] | 523 | ... s.write(b'hello') |
| 524 | |
Chris Liechti | fd70a55 | 2017-07-20 23:46:34 +0200 | [diff] [blame] | 525 | Which also means that ``with`` statements can be used repeatedly, |
| 526 | each time opening and closing the port. |
| 527 | |
| 528 | .. versionchanged:: 3.4 the port is automatically opened |
| 529 | |
Chris Liechti | c9f8996 | 2016-08-12 21:04:37 +0200 | [diff] [blame] | 530 | |
| 531 | .. method:: __exit__(exc_type, exc_val, exc_tb) |
| 532 | |
Chris Liechti | 4f988d4 | 2017-03-20 23:59:40 +0100 | [diff] [blame] | 533 | Closes serial port (exceptions are not handled by ``__exit__``). |
Chris Liechti | c9f8996 | 2016-08-12 21:04:37 +0200 | [diff] [blame] | 534 | |
| 535 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 536 | Platform specific methods. |
| 537 | |
cliechti | c648da9 | 2011-12-29 01:17:51 +0000 | [diff] [blame] | 538 | .. warning:: Programs using the following methods and attributes are not |
| 539 | portable to other platforms! |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 540 | |
| 541 | .. method:: nonblocking() |
| 542 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 543 | :platform: Posix |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 544 | |
Chris Liechti | 5984842 | 2016-06-04 22:28:14 +0200 | [diff] [blame] | 545 | .. deprecated:: 3.2 |
| 546 | The serial port is already opened in this mode. This method is not |
| 547 | needed and going away. |
| 548 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 549 | |
| 550 | .. method:: fileno() |
| 551 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 552 | :platform: Posix |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 553 | :return: File descriptor. |
| 554 | |
| 555 | Return file descriptor number for the port that is opened by this object. |
| 556 | It is useful when serial ports are used with :mod:`select`. |
| 557 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 558 | .. method:: set_input_flow_control(enable) |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 559 | |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 560 | :platform: Posix |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 561 | :param bool enable: Set flow control state. |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 562 | |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 563 | Manually control flow - when software flow control is enabled. |
| 564 | |
| 565 | This will send XON (true) and XOFF (false) to the other device. |
| 566 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 567 | .. versionadded:: 2.7 (Posix support added) |
| 568 | .. versionchanged:: 3.0 renamed from ``flowControlOut`` |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 569 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 570 | .. method:: set_output_flow_control(enable) |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 571 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 572 | :platform: Posix (HW and SW flow control) |
| 573 | :platform: Windows (SW flow control only) |
Chris Liechti | 3e0dcc7 | 2015-12-18 23:23:26 +0100 | [diff] [blame] | 574 | :param bool enable: Set flow control state. |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 575 | |
| 576 | Manually control flow of outgoing data - when hardware or software flow |
| 577 | control is enabled. |
| 578 | |
| 579 | Sending will be suspended when called with ``False`` and enabled when |
| 580 | called with ``True``. |
| 581 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 582 | .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``) |
| 583 | .. versionchanged:: 3.0 renamed from ``setXON`` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 584 | |
Chris Liechti | 19688bf | 2016-05-06 23:48:36 +0200 | [diff] [blame] | 585 | .. method:: cancel_read() |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 586 | |
Chris Liechti | 9d89305 | 2016-05-18 22:03:29 +0200 | [diff] [blame] | 587 | :platform: Posix |
Chris Liechti | 19688bf | 2016-05-06 23:48:36 +0200 | [diff] [blame] | 588 | :platform: Windows |
| 589 | |
Jakub Wilk | 772a6fa | 2016-12-20 21:59:27 +0100 | [diff] [blame] | 590 | Cancel a pending read operation from another thread. A blocking |
Chris Liechti | 9d89305 | 2016-05-18 22:03:29 +0200 | [diff] [blame] | 591 | :meth:`read` call is aborted immediately. :meth:`read` will not report |
| 592 | any error but return all data received up to that point (similar to a |
| 593 | timeout). |
| 594 | |
| 595 | 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] | 596 | |
| 597 | .. versionadded:: 3.1 |
| 598 | |
| 599 | .. method:: cancel_write() |
| 600 | |
Chris Liechti | 9d89305 | 2016-05-18 22:03:29 +0200 | [diff] [blame] | 601 | :platform: Posix |
Chris Liechti | 19688bf | 2016-05-06 23:48:36 +0200 | [diff] [blame] | 602 | :platform: Windows |
| 603 | |
Jakub Wilk | 772a6fa | 2016-12-20 21:59:27 +0100 | [diff] [blame] | 604 | Cancel a pending write operation from another thread. The |
Chris Liechti | 9d89305 | 2016-05-18 22:03:29 +0200 | [diff] [blame] | 605 | :meth:`write` method will return immediately (no error indicated). |
| 606 | However the OS may still be sending from the buffer, a separate call to |
| 607 | :meth:`reset_output_buffer` may be needed. |
| 608 | |
| 609 | 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] | 610 | |
| 611 | .. versionadded:: 3.1 |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 612 | |
Chris Liechti | 9a99cb2 | 2015-08-30 22:16:50 +0200 | [diff] [blame] | 613 | .. note:: The following members are deprecated and will be removed in a |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 614 | future release. |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 615 | |
| 616 | .. attribute:: portstr |
| 617 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 618 | .. deprecated:: 2.5 use :attr:`name` instead |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 619 | |
| 620 | .. method:: inWaiting() |
| 621 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 622 | .. deprecated:: 3.0 see :attr:`in_waiting` |
| 623 | |
Matthew West | fa58a97 | 2016-07-08 14:27:04 -0700 | [diff] [blame] | 624 | .. method:: isOpen() |
| 625 | |
Chris Liechti | fd70a55 | 2017-07-20 23:46:34 +0200 | [diff] [blame] | 626 | .. deprecated:: 3.0 see :attr:`is_open` |
Matthew West | fa58a97 | 2016-07-08 14:27:04 -0700 | [diff] [blame] | 627 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 628 | .. attribute:: writeTimeout |
| 629 | |
| 630 | .. deprecated:: 3.0 see :attr:`write_timeout` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 631 | |
| 632 | .. attribute:: interCharTimeout |
| 633 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 634 | .. deprecated:: 3.0 see :attr:`inter_byte_timeout` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 635 | |
| 636 | .. method:: sendBreak(duration=0.25) |
| 637 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 638 | .. deprecated:: 3.0 see :meth:`send_break` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 639 | |
| 640 | .. method:: flushInput() |
| 641 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 642 | .. deprecated:: 3.0 see :meth:`reset_input_buffer` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 643 | |
| 644 | .. method:: flushOutput() |
| 645 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 646 | .. deprecated:: 3.0 see :meth:`reset_output_buffer` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 647 | |
| 648 | .. method:: setBreak(level=True) |
| 649 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 650 | .. deprecated:: 3.0 see :attr:`break_condition` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 651 | |
| 652 | .. method:: setRTS(level=True) |
| 653 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 654 | .. deprecated:: 3.0 see :attr:`rts` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 655 | |
| 656 | .. method:: setDTR(level=True) |
| 657 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 658 | .. deprecated:: 3.0 see :attr:`dtr` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 659 | |
| 660 | .. method:: getCTS() |
| 661 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 662 | .. deprecated:: 3.0 see :attr:`cts` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 663 | |
| 664 | .. method:: getDSR() |
| 665 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 666 | .. deprecated:: 3.0 see :attr:`dsr` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 667 | |
| 668 | .. method:: getRI() |
| 669 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 670 | .. deprecated:: 3.0 see :attr:`ri` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 671 | |
| 672 | .. method:: getCD() |
| 673 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 674 | .. deprecated:: 3.0 see :attr:`cd` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 675 | |
| 676 | .. method:: getSettingsDict() |
| 677 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 678 | .. deprecated:: 3.0 see :meth:`get_settings` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 679 | |
| 680 | .. method:: applySettingsDict(d) |
| 681 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 682 | .. deprecated:: 3.0 see :meth:`apply_settings` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 683 | |
| 684 | .. method:: outWaiting() |
| 685 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 686 | .. deprecated:: 3.0 see :attr:`out_waiting` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 687 | |
| 688 | .. method:: setXON(level=True) |
| 689 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 690 | .. deprecated:: 3.0 see :meth:`set_output_flow_control` |
Chris Liechti | 256ea21 | 2015-08-29 23:57:00 +0200 | [diff] [blame] | 691 | |
| 692 | .. method:: flowControlOut(enable) |
| 693 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 694 | .. deprecated:: 3.0 see :meth:`set_input_flow_control` |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 695 | |
cliechti | c648da9 | 2011-12-29 01:17:51 +0000 | [diff] [blame] | 696 | .. attribute:: rtsToggle |
| 697 | |
| 698 | :platform: Windows |
| 699 | |
| 700 | Attribute to configure RTS toggle control setting. When enabled and |
| 701 | supported by OS, RTS will be active when data is available and inactive |
| 702 | if no data is available. |
| 703 | |
| 704 | .. versionadded:: 2.6 |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 705 | .. versionchanged:: 3.0 (removed, see :attr:`rs485_mode` instead) |
cliechti | c648da9 | 2011-12-29 01:17:51 +0000 | [diff] [blame] | 706 | |
| 707 | |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 708 | Implementation detail: some attributes and functions are provided by the |
| 709 | class :class:`SerialBase` and some by the platform specific class and |
| 710 | others by the base class mentioned above. |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 711 | |
Chris Liechti | 4f988d4 | 2017-03-20 23:59:40 +0100 | [diff] [blame] | 712 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 713 | RS485 support |
| 714 | ------------- |
| 715 | The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to |
| 716 | enable RS485 specific support on some platforms. Currently Windows and Linux |
| 717 | (only a small number of devices) are supported. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 718 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 719 | :attr:`Serial.rs485_mode` needs to be set to an instance of |
| 720 | :class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 721 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 722 | Usage:: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 723 | |
Chris Liechti | 1153824 | 2016-09-06 22:20:09 +0200 | [diff] [blame] | 724 | import serial |
| 725 | import serial.rs485 |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 726 | ser = serial.Serial(...) |
| 727 | ser.rs485_mode = serial.rs485.RS485Settings(...) |
| 728 | ser.write(b'hello') |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 729 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 730 | 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] | 731 | on regular serial ports (``serial.rs485`` needs to be imported). |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 732 | |
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 | .. class:: rs485.RS485Settings |
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 | A class that holds RS485 specific settings which are supported on |
| 737 | some platforms. |
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 | .. versionadded:: 3.0 |
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 | .. 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] | 742 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 743 | :param bool rts_level_for_tx: |
| 744 | RTS level for transmission |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 745 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 746 | :param bool rts_level_for_rx: |
| 747 | RTS level for reception |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 748 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 749 | :param bool loopback: |
| 750 | When set to ``True`` transmitted data is also received. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 751 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 752 | :param float delay_before_tx: |
| 753 | Delay after setting RTS but before transmission starts |
| 754 | |
| 755 | :param float delay_before_rx: |
| 756 | Delay after transmission ends and resetting RTS |
| 757 | |
| 758 | .. attribute:: rts_level_for_tx |
| 759 | |
| 760 | RTS level for transmission. |
| 761 | |
| 762 | .. attribute:: rts_level_for_rx |
| 763 | |
| 764 | RTS level for reception. |
| 765 | |
| 766 | .. attribute:: loopback |
| 767 | |
| 768 | When set to ``True`` transmitted data is also received. |
| 769 | |
| 770 | .. attribute:: delay_before_tx |
| 771 | |
| 772 | Delay after setting RTS but before transmission starts (seconds as float). |
| 773 | |
| 774 | .. attribute:: delay_before_rx |
| 775 | |
| 776 | Delay after transmission ends and resetting RTS (seconds as float). |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 777 | |
| 778 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 779 | .. class:: rs485.RS485 |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 780 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 781 | A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS |
| 782 | according to the RS485 settings. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 783 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 784 | Usage:: |
| 785 | |
| 786 | ser = serial.rs485.RS485(...) |
| 787 | ser.rs485_mode = serial.rs485.RS485Settings(...) |
| 788 | ser.write(b'hello') |
| 789 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 790 | .. warning:: This may work unreliably on some serial ports (control signals not |
| 791 | synchronized or delayed compared to data). Using delays may be unreliable |
| 792 | (varying times, larger than expected) as the OS may not support very fine |
| 793 | grained delays (no smaller than in the order of tens of milliseconds). |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 794 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 795 | .. note:: Some implementations support this natively in the class |
| 796 | :class:`Serial`. Better performance can be expected when the native version |
| 797 | is used. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 798 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 799 | .. note:: The loopback property is ignored by this implementation. The actual |
| 800 | behavior depends on the used hardware. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 801 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 802 | |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 803 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 804 | :rfc:`2217` Network ports |
| 805 | ------------------------- |
| 806 | |
| 807 | .. warning:: This implementation is currently in an experimental state. Use |
| 808 | at your own risk. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 809 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 810 | .. class:: rfc2217.Serial |
| 811 | |
Chris Liechti | 2a1befc | 2015-10-21 17:37:08 +0200 | [diff] [blame] | 812 | This implements a :rfc:`2217` compatible client. Port names are :ref:`URL |
| 813 | <URLs>` in the form: ``rfc2217://<host>:<port>[?<option>[&<option>]]`` |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 814 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 815 | This class API is compatible to :class:`Serial` with a few exceptions: |
| 816 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 817 | - ``write_timeout`` is not implemented |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 818 | - The current implementation starts a thread that keeps reading from the |
| 819 | (internal) socket. The thread is managed automatically by the |
| 820 | :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`. |
| 821 | However it may be a problem for user applications that like to use select |
| 822 | instead of threads. |
| 823 | |
| 824 | Due to the nature of the network and protocol involved there are a few |
| 825 | extra points to keep in mind: |
| 826 | |
| 827 | - All operations have an additional latency time. |
| 828 | - Setting control lines (RTS/CTS) needs more time. |
| 829 | - Reading the status lines (DSR/DTR etc.) returns a cached value. When that |
| 830 | cache is updated depends entirely on the server. The server itself may |
| 831 | implement a polling at a certain rate and quick changes may be invisible. |
| 832 | - The network layer also has buffers. This means that :meth:`flush`, |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 833 | :meth:`reset_input_buffer` and :meth:`reset_output_buffer` may work with |
| 834 | additional delay. Likewise :attr:`in_waiting` returns the size of the |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 835 | 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] | 836 | network buffers or any server side buffer. |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 837 | - Closing and immediately reopening the same port may fail due to time |
| 838 | needed by the server to get ready again. |
| 839 | |
| 840 | Not implemented yet / Possible problems with the implementation: |
| 841 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 842 | - :rfc:`2217` flow control between client and server (objects internal |
| 843 | buffer may eat all your memory when never read). |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 844 | - No authentication support (servers may not prompt for a password etc.) |
| 845 | - No encryption. |
| 846 | |
| 847 | Due to lack of authentication and encryption it is not suitable to use this |
| 848 | client for connections across the internet and should only be used in |
| 849 | controlled environments. |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 850 | |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 851 | .. versionadded:: 2.5 |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 852 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 853 | |
| 854 | .. class:: rfc2217.PortManager |
| 855 | |
| 856 | This class provides helper functions for implementing :rfc:`2217` |
| 857 | compatible servers. |
| 858 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 859 | Basically, it implements everything needed for the :rfc:`2217` protocol. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 860 | It just does not open sockets and read/write to serial ports (though it |
| 861 | changes other port settings). The user of this class must take care of the |
| 862 | data transmission itself. The reason for that is, that this way, this class |
| 863 | supports all programming models such as threads and select. |
| 864 | |
| 865 | Usage examples can be found in the examples where two TCP/IP - serial |
| 866 | converters are shown, one using threads (the single port server) and an |
| 867 | other using select (the multi port server). |
| 868 | |
| 869 | .. note:: Each new client connection must create a new instance as this |
| 870 | object (and the :rfc:`2217` protocol) has internal state. |
| 871 | |
| 872 | .. method:: __init__(serial_port, connection, debug_output=False) |
| 873 | |
| 874 | :param serial_port: a :class:`Serial` instance that is managed. |
| 875 | :param connection: an object implementing :meth:`write`, used to write |
| 876 | to the network. |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 877 | :param debug_output: enables debug messages: a :class:`logging.Logger` |
| 878 | instance or None. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 879 | |
| 880 | Initializes the Manager and starts negotiating with client in Telnet |
| 881 | and :rfc:`2217` protocol. The negotiation starts immediately so that |
| 882 | the class should be instantiated in the moment the client connects. |
| 883 | |
| 884 | The *serial_port* can be controlled by :rfc:`2217` commands. This |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 885 | object will modify the port settings (baud rate etc.) and control lines |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 886 | (RTS/DTR) send BREAK etc. when the corresponding commands are found by |
| 887 | the :meth:`filter` method. |
| 888 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 889 | The *connection* object must implement a :meth:`write` function. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 890 | This function must ensure that *data* is written at once (no user data |
| 891 | mixed in, i.e. it must be thread-safe). All data must be sent in its |
| 892 | raw form (:meth:`escape` must not be used) as it is used to send Telnet |
| 893 | and :rfc:`2217` control commands. |
| 894 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 895 | For diagnostics of the connection or the implementation, *debug_output* |
| 896 | can be set to an instance of a :class:`logging.Logger` (e.g. |
| 897 | ``logging.getLogger('rfc2217.server')``). The caller should configure |
| 898 | the logger using ``setLevel`` for the desired detail level of the logs. |
| 899 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 900 | .. method:: escape(data) |
| 901 | |
| 902 | :param data: data to be sent over the network. |
| 903 | :return: data, escaped for Telnet/:rfc:`2217` |
| 904 | |
| 905 | A generator that escapes all data to be compatible with :rfc:`2217`. |
| 906 | Implementors of servers should use this function to process all data |
| 907 | sent over the network. |
| 908 | |
| 909 | The function returns a generator which can be used in ``for`` loops. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 910 | It can be converted to bytes using :func:`serial.to_bytes`. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 911 | |
| 912 | .. method:: filter(data) |
| 913 | |
| 914 | :param data: data read from the network, including Telnet and |
| 915 | :rfc:`2217` controls. |
| 916 | :return: data, free from Telnet and :rfc:`2217` controls. |
| 917 | |
| 918 | A generator that filters and processes all data related to :rfc:`2217`. |
| 919 | Implementors of servers should use this function to process all data |
| 920 | received from the network. |
| 921 | |
| 922 | The function returns a generator which can be used in ``for`` loops. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 923 | It can be converted to bytes using :func:`serial.to_bytes`. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 924 | |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 925 | .. method:: check_modem_lines(force_notification=False) |
| 926 | |
| 927 | :param force_notification: Set to false. Parameter is for internal use. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 928 | |
| 929 | This function needs to be called periodically (e.g. every second) when |
| 930 | the server wants to send NOTIFY_MODEMSTATE messages. This is required |
| 931 | to support the client for reading CTS/DSR/RI/CD status lines. |
| 932 | |
| 933 | The function reads the status line and issues the notifications |
| 934 | automatically. |
| 935 | |
| 936 | .. versionadded:: 2.5 |
| 937 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 938 | .. seealso:: |
| 939 | |
| 940 | :rfc:`2217` - Telnet Com Port Control Option |
| 941 | |
| 942 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 943 | Exceptions |
| 944 | ========== |
| 945 | |
| 946 | .. exception:: SerialException |
| 947 | |
| 948 | Base class for serial port exceptions. |
| 949 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 950 | .. versionchanged:: 2.5 |
cliechti | 4cb9466 | 2013-10-17 03:17:50 +0000 | [diff] [blame] | 951 | Now derives from :exc:`IOError` instead of :exc:`Exception` |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 952 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 953 | .. exception:: SerialTimeoutException |
| 954 | |
| 955 | Exception that is raised on write timeouts. |
| 956 | |
| 957 | |
| 958 | Constants |
| 959 | ========= |
| 960 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 961 | *Parity* |
| 962 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 963 | .. data:: PARITY_NONE |
| 964 | .. data:: PARITY_EVEN |
| 965 | .. data:: PARITY_ODD |
| 966 | .. data:: PARITY_MARK |
| 967 | .. data:: PARITY_SPACE |
| 968 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 969 | *Stop bits* |
| 970 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 971 | .. data:: STOPBITS_ONE |
| 972 | .. data:: STOPBITS_ONE_POINT_FIVE |
| 973 | .. data:: STOPBITS_TWO |
| 974 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 975 | 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] | 976 | bits. |
| 977 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 978 | *Byte size* |
| 979 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 980 | .. data:: FIVEBITS |
| 981 | .. data:: SIXBITS |
| 982 | .. data:: SEVENBITS |
| 983 | .. data:: EIGHTBITS |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 984 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 985 | |
| 986 | *Others* |
| 987 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 988 | Default control characters (instances of :class:`bytes` for Python 3.0+) for |
cliechti | daf47ba | 2009-07-28 01:28:16 +0000 | [diff] [blame] | 989 | software flow control: |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 990 | |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 991 | .. data:: XON |
| 992 | .. data:: XOFF |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 993 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 994 | Module version: |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 995 | |
| 996 | .. data:: VERSION |
| 997 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 998 | A string indicating the pySerial version, such as ``3.0``. |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 999 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 1000 | .. versionadded:: 2.3 |
| 1001 | |
cliechti | 44eb98f | 2011-03-21 21:41:10 +0000 | [diff] [blame] | 1002 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1003 | Module functions and attributes |
| 1004 | =============================== |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 1005 | |
| 1006 | .. function:: device(number) |
| 1007 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 1008 | .. versionchanged:: 3.0 removed, use ``serial.tools.list_ports`` instead |
| 1009 | |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 1010 | |
cliechti | e3ab353 | 2009-08-05 12:40:38 +0000 | [diff] [blame] | 1011 | .. function:: serial_for_url(url, \*args, \*\*kwargs) |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 1012 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 1013 | :param url: Device name, number or :ref:`URL <URLs>` |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 1014 | :param do_not_open: When set to true, the serial port is not opened. |
| 1015 | :return: an instance of :class:`Serial` or a compatible object. |
| 1016 | |
| 1017 | Get a native or a :rfc:`2217` implementation of the Serial class, depending |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 1018 | on port/url. This factory function is useful when an application wants |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 1019 | to support both, local ports and remote ports. There is also support |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 1020 | for other types, see :ref:`URL <URLs>` section. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 1021 | |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 1022 | The port is not opened when a keyword parameter called *do_not_open* is |
| 1023 | given and true, by default it is opened. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 1024 | |
| 1025 | .. versionadded:: 2.5 |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 1026 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 1027 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1028 | .. attribute:: protocol_handler_packages |
| 1029 | |
| 1030 | This attribute is a list of package names (strings) that is searched for |
| 1031 | protocol handlers. |
| 1032 | |
| 1033 | e.g. we want to support a URL ``foobar://``. A module |
| 1034 | ``my_handlers.protocol_foobar`` is provided by the user:: |
| 1035 | |
| 1036 | serial.protocol_handler_packages.append("my_handlers") |
| 1037 | s = serial.serial_for_url("foobar://") |
| 1038 | |
| 1039 | For an URL starting with ``XY://`` is the function :func:`serial_for_url` |
| 1040 | attempts to import ``PACKAGE.protocol_XY`` with each candidate for |
| 1041 | ``PACKAGE`` from this list. |
| 1042 | |
| 1043 | .. versionadded:: 2.6 |
| 1044 | |
| 1045 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 1046 | .. function:: to_bytes(sequence) |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 1047 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 1048 | :param sequence: bytes, bytearray or memoryview |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 1049 | :returns: an instance of ``bytes`` |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 1050 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1051 | 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] | 1052 | compatible to Python 2.x and 3.x. |
| 1053 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1054 | 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] | 1055 | ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1056 | ``bytes(sequence)`` doesn't work for all versions of Python. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 1057 | |
| 1058 | This function is used internally and in the unit tests. |
| 1059 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 1060 | .. versionadded:: 2.5 |
| 1061 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 1062 | .. function:: iterbytes(sequence) |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 1063 | |
Chris Liechti | 32ccf2e | 2015-12-19 23:42:40 +0100 | [diff] [blame] | 1064 | :param sequence: bytes, bytearray or memoryview |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 1065 | :returns: a generator that yields bytes |
| 1066 | |
| 1067 | 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] | 1068 | looping over an instance of ``bytes``. This helper function ensures that |
Chris Liechti | d14b1ab | 2015-08-21 00:28:53 +0200 | [diff] [blame] | 1069 | bytes are returned. |
| 1070 | |
| 1071 | .. versionadded:: 3.0 |
| 1072 | |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 1073 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1074 | Threading |
| 1075 | ========= |
| 1076 | |
| 1077 | .. module:: serial.threaded |
| 1078 | .. versionadded:: 3.0 |
| 1079 | |
| 1080 | .. warning:: This implementation is currently in an experimental state. Use |
| 1081 | at your own risk. |
| 1082 | |
| 1083 | This module provides classes to simplify working with threads and protocols. |
| 1084 | |
| 1085 | .. class:: Protocol |
| 1086 | |
Chris Liechti | 5665d57 | 2015-10-13 23:50:01 +0200 | [diff] [blame] | 1087 | Protocol as used by the :class:`ReaderThread`. This base class provides empty |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1088 | implementations of all methods. |
| 1089 | |
| 1090 | |
| 1091 | .. method:: connection_made(transport) |
| 1092 | |
| 1093 | :param transport: instance used to write to serial port. |
| 1094 | |
| 1095 | Called when reader thread is started. |
| 1096 | |
| 1097 | .. method:: data_received(data) |
| 1098 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1099 | :param bytes data: received bytes |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1100 | |
| 1101 | Called with snippets received from the serial port. |
| 1102 | |
| 1103 | .. method:: connection_lost(exc) |
| 1104 | |
| 1105 | :param exc: Exception if connection was terminated by error else ``None`` |
| 1106 | |
| 1107 | Called when the serial port is closed or the reader loop terminated |
| 1108 | otherwise. |
| 1109 | |
| 1110 | .. class:: Packetizer(Protocol) |
| 1111 | |
| 1112 | Read binary packets from serial port. Packets are expected to be terminated |
| 1113 | with a ``TERMINATOR`` byte (null byte by default). |
| 1114 | |
| 1115 | The class also keeps track of the transport. |
| 1116 | |
| 1117 | .. attribute:: TERMINATOR = b'\\0' |
| 1118 | |
| 1119 | .. method:: __init__() |
| 1120 | |
| 1121 | .. method:: connection_made(transport) |
| 1122 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1123 | Stores transport. |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1124 | |
| 1125 | .. method:: connection_lost(exc) |
| 1126 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1127 | Forgets transport. |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1128 | |
| 1129 | .. method:: data_received(data) |
| 1130 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1131 | :param bytes data: partial received data |
| 1132 | |
| 1133 | Buffer received data and search for :attr:`TERMINATOR`, when found, |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1134 | call :meth:`handle_packet`. |
| 1135 | |
| 1136 | .. method:: handle_packet(packet) |
| 1137 | |
Chris Liechti | 1c4c599 | 2016-01-18 20:46:00 +0100 | [diff] [blame] | 1138 | :param bytes packet: a packet as defined by ``TERMINATOR`` |
| 1139 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1140 | Process packets - to be overridden by subclassing. |
| 1141 | |
| 1142 | |
| 1143 | .. class:: LineReader(Packetizer) |
| 1144 | |
| 1145 | Read and write (Unicode) lines from/to serial port. |
| 1146 | The encoding is applied. |
| 1147 | |
| 1148 | |
| 1149 | .. attribute:: TERMINATOR = b'\\r\\n' |
Chris Liechti | 1c4c599 | 2016-01-18 20:46:00 +0100 | [diff] [blame] | 1150 | |
| 1151 | Line ending. |
| 1152 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1153 | .. attribute:: ENCODING = 'utf-8' |
Chris Liechti | 1c4c599 | 2016-01-18 20:46:00 +0100 | [diff] [blame] | 1154 | |
| 1155 | Encoding of the send and received data. |
| 1156 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1157 | .. attribute:: UNICODE_HANDLING = 'replace' |
| 1158 | |
Chris Liechti | 1c4c599 | 2016-01-18 20:46:00 +0100 | [diff] [blame] | 1159 | Unicode error handly policy. |
| 1160 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1161 | .. method:: handle_packet(packet) |
| 1162 | |
Chris Liechti | 1c4c599 | 2016-01-18 20:46:00 +0100 | [diff] [blame] | 1163 | :param bytes packet: a packet as defined by ``TERMINATOR`` |
| 1164 | |
| 1165 | In this case it will be a line, calls :meth:`handle_line` after applying |
| 1166 | the :attr:`ENCODING`. |
| 1167 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1168 | .. method:: handle_line(line) |
| 1169 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1170 | :param str line: Unicode string with one line (excluding line terminator) |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1171 | |
| 1172 | Process one line - to be overridden by subclassing. |
| 1173 | |
| 1174 | .. method:: write_line(text) |
| 1175 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1176 | :param str text: Unicode string with one line (excluding line terminator) |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1177 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1178 | Write *text* to the transport. *text* is expected to be a Unicode |
| 1179 | string and the encoding is applied before sending and also the |
| 1180 | :attr:`TERMINATOR` (new line) is appended. |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1181 | |
| 1182 | |
Chris Liechti | 5665d57 | 2015-10-13 23:50:01 +0200 | [diff] [blame] | 1183 | .. class:: ReaderThread(threading.Thread) |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1184 | |
| 1185 | Implement a serial port read loop and dispatch to a Protocol instance (like |
| 1186 | the :class:`asyncio.Protocol`) but do it with threads. |
| 1187 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1188 | Calls to :meth:`close` will close the serial port but it is also possible |
| 1189 | to just :meth:`stop` this thread and continue to use the serial port |
| 1190 | instance otherwise. |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1191 | |
| 1192 | .. method:: __init__(serial_instance, protocol_factory) |
| 1193 | |
| 1194 | :param serial_instance: serial port instance (opened) to be used. |
| 1195 | :param protocol_factory: a callable that returns a Protocol instance |
| 1196 | |
| 1197 | Initialize thread. |
| 1198 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1199 | Note that the ``serial_instance`` 's timeout is set to one second! |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1200 | Other settings are not changed. |
| 1201 | |
| 1202 | .. method:: stop() |
| 1203 | |
| 1204 | Stop the reader thread. |
| 1205 | |
| 1206 | .. method:: run() |
| 1207 | |
| 1208 | The actual reader loop driven by the thread. It calls |
| 1209 | :meth:`Protocol.connection_made`, reads from the serial port calling |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1210 | :meth:`Protocol.data_received` and finally calls :meth:`Protocol.connection_lost` |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1211 | when :meth:`close` is called or an error occurs. |
| 1212 | |
| 1213 | .. method:: write(data) |
| 1214 | |
Chris Liechti | f8a0919 | 2015-12-20 23:36:38 +0100 | [diff] [blame] | 1215 | :param bytes data: data to write |
| 1216 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1217 | Thread safe writing (uses lock). |
| 1218 | |
| 1219 | .. method:: close() |
| 1220 | |
| 1221 | Close the serial port and exit reader thread, calls :meth:`stop` (uses lock). |
| 1222 | |
| 1223 | .. method:: connect() |
| 1224 | |
| 1225 | Wait until connection is set up and return the transport and protocol |
| 1226 | instances. |
| 1227 | |
| 1228 | |
| 1229 | This class can be used as context manager, in this case it starts |
| 1230 | the thread and connects automatically. The serial port is closed |
| 1231 | when the context is left. |
| 1232 | |
| 1233 | .. method:: __enter__() |
| 1234 | |
| 1235 | :returns: protocol |
| 1236 | |
| 1237 | Connect and return protocol instance. |
| 1238 | |
| 1239 | .. method:: __exit__(exc_type, exc_val, exc_tb) |
| 1240 | |
| 1241 | Closes serial port. |
| 1242 | |
Chris Liechti | a1436b1 | 2015-10-12 23:19:04 +0200 | [diff] [blame] | 1243 | Example:: |
| 1244 | |
| 1245 | class PrintLines(LineReader): |
| 1246 | def connection_made(self, transport): |
| 1247 | super(PrintLines, self).connection_made(transport) |
| 1248 | sys.stdout.write('port opened\n') |
| 1249 | self.write_line('hello world') |
| 1250 | |
| 1251 | def handle_line(self, data): |
| 1252 | sys.stdout.write('line received: {}\n'.format(repr(data))) |
| 1253 | |
| 1254 | def connection_lost(self, exc): |
| 1255 | if exc: |
| 1256 | traceback.print_exc(exc) |
| 1257 | sys.stdout.write('port closed\n') |
| 1258 | |
| 1259 | ser = serial.serial_for_url('loop://', baudrate=115200, timeout=1) |
Chris Liechti | 5665d57 | 2015-10-13 23:50:01 +0200 | [diff] [blame] | 1260 | with ReaderThread(ser, PrintLines) as protocol: |
Chris Liechti | a1436b1 | 2015-10-12 23:19:04 +0200 | [diff] [blame] | 1261 | protocol.write_line('hello') |
| 1262 | time.sleep(2) |
| 1263 | |
Chris Liechti | 811bf38 | 2015-10-10 00:54:47 +0200 | [diff] [blame] | 1264 | |
Chris Liechti | 1f0c9b4 | 2015-09-03 23:49:48 +0200 | [diff] [blame] | 1265 | asyncio |
| 1266 | ======= |
| 1267 | |
Chris Liechti | 2c5a31b | 2016-06-18 22:57:24 +0200 | [diff] [blame] | 1268 | ``asyncio`` was introduced with Python 3.4. Experimental support for pySerial |
| 1269 | is provided via a separate distribution `pyserial-asyncio`_. |
Chris Liechti | 589c92a | 2015-09-04 23:04:34 +0200 | [diff] [blame] | 1270 | |
Jakub Wilk | 772a6fa | 2016-12-20 21:59:27 +0100 | [diff] [blame] | 1271 | It is currently under development, see: |
Chris Liechti | 1f0c9b4 | 2015-09-03 23:49:48 +0200 | [diff] [blame] | 1272 | |
Chris Liechti | 2c5a31b | 2016-06-18 22:57:24 +0200 | [diff] [blame] | 1273 | - http://pyserial-asyncio.readthedocs.io/ |
| 1274 | - https://github.com/pyserial/pyserial-asyncio |
Chris Liechti | 1f0c9b4 | 2015-09-03 23:49:48 +0200 | [diff] [blame] | 1275 | |
Chris Liechti | 2c5a31b | 2016-06-18 22:57:24 +0200 | [diff] [blame] | 1276 | .. _`pyserial-asyncio`: https://pypi.python.org/pypi/pyserial-asyncio |
Chris Liechti | 1f0c9b4 | 2015-09-03 23:49:48 +0200 | [diff] [blame] | 1277 | |