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