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