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