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