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 | |
cliechti | 5c72e4d | 2010-07-21 14:04:54 +0000 | [diff] [blame] | 15 | .. method:: __init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, writeTimeout=None, dsrdtr=False, interCharTimeout=None) |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 16 | |
| 17 | :param port: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 18 | Device name or port number number 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 | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 50 | :param float writeTimeout: |
cliechti | 07709e4 | 2010-05-21 00:30:09 +0000 | [diff] [blame] | 51 | Set a write timeout value. |
| 52 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 53 | :param float interCharTimeout: |
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 | |
| 67 | Possible values for the parameter *port*: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 68 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 69 | - Number: number of device, numbering starts at zero. |
| 70 | - Device name: depending on operating system. e.g. ``/dev/ttyUSB0`` |
| 71 | on GNU/Linux or ``COM3`` on Windows. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 72 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 73 | The parameter *baudrate* can be one of the standard values: |
| 74 | 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 75 | 9600, 19200, 38400, 57600, 115200. |
cliechti | 28b8fd0 | 2011-12-28 21:39:42 +0000 | [diff] [blame] | 76 | These are well supported on all platforms. |
| 77 | |
| 78 | Standard values above 115200, such as: 230400, 460800, 500000, 576000, |
| 79 | 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000, |
| 80 | 4000000 also work on many platforms and devices. |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 81 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 82 | Non-standard values are also supported on some platforms (GNU/Linux, MAC |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 83 | OSX >= Tiger, Windows). Though, even on these platforms some serial |
| 84 | ports may reject non-standard values. |
| 85 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 86 | Possible values for the parameter *timeout*: |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 87 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 88 | - ``timeout = None``: wait forever |
| 89 | - ``timeout = 0``: non-blocking mode (return immediately on read) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 90 | - ``timeout = x``: set timeout to ``x`` seconds (float allowed) |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 91 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 92 | Writes are blocking by default, unless *writeTimeout* is set. For |
| 93 | possible values refer to the list for *timeout* above. |
cliechti | 07709e4 | 2010-05-21 00:30:09 +0000 | [diff] [blame] | 94 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 95 | Note that enabling both flow control methods (*xonxoff* and *rtscts*) |
| 96 | together may not be supported. It is common to use one of the methods |
| 97 | at once, not both. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 98 | |
cliechti | 07709e4 | 2010-05-21 00:30:09 +0000 | [diff] [blame] | 99 | *dsrdtr* is not supported by all platforms (silently ignored). Setting |
| 100 | it to ``None`` has the effect that its state follows *rtscts*. |
| 101 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 102 | Also consider using the function :func:`serial_for_url` instead of |
| 103 | creating Serial instances directly. |
| 104 | |
cliechti | 5c72e4d | 2010-07-21 14:04:54 +0000 | [diff] [blame] | 105 | .. versionchanged:: 2.5 |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 106 | *dsrdtr* now defaults to ``False`` (instead of *None*) |
cliechti | 5c72e4d | 2010-07-21 14:04:54 +0000 | [diff] [blame] | 107 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 108 | .. method:: open() |
| 109 | |
| 110 | Open port. |
| 111 | |
| 112 | .. method:: close() |
| 113 | |
| 114 | Close port immediately. |
| 115 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 116 | .. method:: __del__() |
| 117 | |
| 118 | Destructor, close port when serial port instance is freed. |
| 119 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 120 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 121 | The following methods may raise :exc:`ValueError` when applied to a closed |
| 122 | port. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 123 | |
| 124 | .. method:: read(size=1) |
| 125 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 126 | :param size: Number of bytes to read. |
| 127 | :return: Bytes read from the port. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 128 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 129 | 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] | 130 | return less characters as requested. With no timeout it will block |
| 131 | until the requested number of bytes is read. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 132 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 133 | .. versionchanged:: 2.5 |
| 134 | Returns an instance of :class:`bytes` when available (Python 2.6 |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 135 | and newer) and :class:`str` otherwise. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 136 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 137 | .. method:: write(data) |
| 138 | |
| 139 | :param data: Data to send. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 140 | :return: Number of bytes written. |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 141 | :exception SerialTimeoutException: |
| 142 | In case a write timeout is configured for the port and the time is |
| 143 | exceeded. |
| 144 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 145 | Write the string *data* to the port. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 146 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 147 | .. versionchanged:: 2.5 |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 148 | Accepts instances of :class:`bytes` and :class:`bytearray` when |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 149 | available (Python 2.6 and newer) and :class:`str` otherwise. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 150 | |
cliechti | 9a1c695 | 2009-10-20 22:18:28 +0000 | [diff] [blame] | 151 | .. versionchanged:: 2.5 |
| 152 | Write returned ``None`` in previous versions. |
| 153 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 154 | .. method:: inWaiting() |
| 155 | |
| 156 | Return the number of chars in the receive buffer. |
| 157 | |
| 158 | .. method:: flush() |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 159 | |
| 160 | Flush of file like objects. In this case, wait until all data is |
| 161 | written. |
| 162 | |
| 163 | .. method:: flushInput() |
| 164 | |
| 165 | Flush input buffer, discarding all it's contents. |
| 166 | |
| 167 | .. method:: flushOutput() |
| 168 | |
| 169 | Clear output buffer, aborting the current output and |
| 170 | discarding all that is in the buffer. |
| 171 | |
| 172 | .. method:: sendBreak(duration=0.25) |
| 173 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 174 | :param duration: Time (float) to activate the BREAK condition. |
| 175 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 176 | Send break condition. Timed, returns to idle state after given |
| 177 | duration. |
| 178 | |
| 179 | .. method:: setBreak(level=True) |
| 180 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 181 | :param level: when true activate BREAK condition, else disable. |
| 182 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 183 | Set break: Controls TXD. When active, no transmitting is possible. |
| 184 | |
| 185 | .. method:: setRTS(level=True) |
| 186 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 187 | :param level: Set control line to logic level. |
| 188 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 189 | Set RTS line to specified logic level. |
| 190 | |
| 191 | .. method:: setDTR(level=True) |
| 192 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 193 | :param level: Set control line to logic level. |
| 194 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 195 | Set DTR line to specified logic level. |
| 196 | |
| 197 | .. method:: getCTS() |
| 198 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 199 | :return: Current state (boolean) |
| 200 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 201 | Return the state of the CTS line. |
| 202 | |
| 203 | .. method:: getDSR() |
| 204 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 205 | :return: Current state (boolean) |
| 206 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 207 | Return the state of the DSR line. |
| 208 | |
| 209 | .. method:: getRI() |
| 210 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 211 | :return: Current state (boolean) |
| 212 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 213 | Return the state of the RI line. |
| 214 | |
| 215 | .. method:: getCD() |
| 216 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 217 | :return: Current state (boolean) |
| 218 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 219 | Return the state of the CD line |
| 220 | |
| 221 | Read-only attributes: |
| 222 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 223 | .. attribute:: name |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 224 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 225 | Device name. This is always the device name even if the |
| 226 | port was opened by a number. (Read Only). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 227 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 228 | .. versionadded:: 2.5 |
| 229 | |
| 230 | .. attribute:: portstr |
| 231 | |
| 232 | :deprecated: use :attr:`name` instead |
| 233 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 234 | New values can be assigned to the following attributes (properties), the |
| 235 | port will be reconfigured, even if it's opened at that time: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 236 | |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 237 | |
| 238 | .. attribute:: port |
| 239 | |
| 240 | Read or write port. When the port is already open, it will be closed |
| 241 | and reopened with the new setting. |
| 242 | |
| 243 | .. attribute:: baudrate |
| 244 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 245 | Read or write current baud rate setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 246 | |
| 247 | .. attribute:: bytesize |
| 248 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 249 | Read or write current data byte size setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 250 | |
| 251 | .. attribute:: parity |
| 252 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 253 | Read or write current parity setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 254 | |
| 255 | .. attribute:: stopbits |
| 256 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 257 | Read or write current stop bit width setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 258 | |
| 259 | .. attribute:: timeout |
| 260 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 261 | Read or write current read timeout setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 262 | |
| 263 | .. attribute:: writeTimeout |
| 264 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 265 | Read or write current write timeout setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 266 | |
| 267 | .. attribute:: xonxoff |
| 268 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 269 | Read or write current software flow control rate setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 270 | |
| 271 | .. attribute:: rtscts |
| 272 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 273 | Read or write current hardware flow control setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 274 | |
| 275 | .. attribute:: dsrdtr |
| 276 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 277 | Read or write current hardware flow control setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 278 | |
| 279 | .. attribute:: interCharTimeout |
| 280 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 281 | Read or write current inter character timeout setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 282 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 283 | .. attribute:: rs485_mode |
| 284 | |
| 285 | :platform: Posix |
| 286 | :platform: Windows |
| 287 | |
| 288 | Attribute to configure RS485 support. When set to an instance of |
| 289 | :class:`rs485.RS485Settings` and supported by OS, RTS will be active |
| 290 | when data is sent and inactive otherwise (for reception). The |
| 291 | :class:`rs485.RS485Settings` class provides additional settings |
| 292 | supported on some platforms. |
| 293 | |
| 294 | .. versionadded:: 3.0 |
| 295 | |
| 296 | |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 297 | The following constants are also provided: |
| 298 | |
| 299 | .. attribute:: BAUDRATES |
| 300 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 301 | A list of valid baud rates. The list may be incomplete such that higher |
| 302 | baud rates may be supported by the device and that values in between the |
| 303 | standard baud rates are supported. (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 304 | |
| 305 | .. attribute:: BYTESIZES |
| 306 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 307 | A list of valid byte sizes for the device (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 308 | |
| 309 | .. attribute:: PARITIES |
| 310 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 311 | A list of valid parities for the device (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 312 | |
| 313 | .. attribute:: STOPBITS |
| 314 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 315 | A list of valid stop bit widths for the device (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 316 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 317 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 318 | The following methods are for compatibility with the :mod:`io` library. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 319 | |
| 320 | .. method:: readable() |
| 321 | |
| 322 | :return: True |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 323 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 324 | .. versionadded:: 2.5 |
| 325 | |
| 326 | .. method:: writable() |
| 327 | |
| 328 | :return: True |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 329 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 330 | .. versionadded:: 2.5 |
| 331 | |
| 332 | .. method:: seekable() |
| 333 | |
| 334 | :return: False |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 335 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 336 | .. versionadded:: 2.5 |
| 337 | |
| 338 | .. method:: readinto(b) |
| 339 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 340 | :param b: bytearray or array instance |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 341 | :return: Number of byte read |
| 342 | |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 343 | Read up to len(b) bytes into :class:`bytearray` *b* and return the |
| 344 | number of bytes read. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 345 | |
| 346 | .. versionadded:: 2.5 |
| 347 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 348 | The port settings can be read and written as dictionary. |
| 349 | |
cliechti | 7053b23 | 2009-08-10 01:37:34 +0000 | [diff] [blame] | 350 | .. method:: getSettingsDict() |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 351 | |
| 352 | :return: a dictionary with current port settings. |
| 353 | |
| 354 | Get a dictionary with port settings. This is useful to backup the |
| 355 | current settings so that a later point in time they can be restored |
| 356 | using :meth:`applySettingsDict`. |
| 357 | |
| 358 | Note that control lines (RTS/DTR) are part of the settings. |
| 359 | |
| 360 | .. versionadded:: 2.5 |
| 361 | |
cliechti | 7053b23 | 2009-08-10 01:37:34 +0000 | [diff] [blame] | 362 | .. method:: applySettingsDict(d) |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 363 | |
| 364 | :param d: a dictionary with port settings. |
| 365 | |
| 366 | Applies a dictionary that was created by :meth:`getSettingsDict`. Only |
| 367 | changes are applied and when a key is missing it means that the setting |
| 368 | stays unchanged. |
| 369 | |
| 370 | Note that control lines (RTS/DTR) are not changed. |
| 371 | |
| 372 | .. versionadded:: 2.5 |
| 373 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 374 | Platform specific methods. |
| 375 | |
cliechti | c648da9 | 2011-12-29 01:17:51 +0000 | [diff] [blame] | 376 | .. warning:: Programs using the following methods and attributes are not |
| 377 | portable to other platforms! |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 378 | |
cliechti | 28b8fd0 | 2011-12-28 21:39:42 +0000 | [diff] [blame] | 379 | .. method:: outWaiting() |
| 380 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 381 | :platform: Posix |
cliechti | 28b8fd0 | 2011-12-28 21:39:42 +0000 | [diff] [blame] | 382 | :platform: Windows |
| 383 | |
| 384 | Return the number of bytes in the output buffer. |
| 385 | |
| 386 | .. versionchanged:: 2.7 (Posix support added) |
| 387 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 388 | .. method:: nonblocking() |
| 389 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 390 | :platform: Posix |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 391 | |
| 392 | Configure the device for nonblocking operation. This can be useful if |
| 393 | the port is used with :mod:`select`. |
| 394 | |
| 395 | .. method:: fileno() |
| 396 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 397 | :platform: Posix |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 398 | :return: File descriptor. |
| 399 | |
| 400 | Return file descriptor number for the port that is opened by this object. |
| 401 | It is useful when serial ports are used with :mod:`select`. |
| 402 | |
| 403 | .. method:: setXON(level=True) |
| 404 | |
| 405 | :platform: Windows |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 406 | :platform: Posix |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 407 | :param level: Set flow control state. |
| 408 | |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 409 | Manually control flow - when software flow control is enabled. |
| 410 | |
| 411 | This will send XON (true) and XOFF (false) to the other device. |
| 412 | |
| 413 | .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``) |
| 414 | |
| 415 | .. method:: flowControlOut(enable) |
| 416 | |
| 417 | :platform: Posix |
| 418 | :param enable: Set flow control state. |
| 419 | |
| 420 | Manually control flow of outgoing data - when hardware or software flow |
| 421 | control is enabled. |
| 422 | |
| 423 | Sending will be suspended when called with ``False`` and enabled when |
| 424 | called with ``True``. |
| 425 | |
| 426 | .. versionadded:: 2.7 (Posix support added) |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 427 | |
cliechti | c648da9 | 2011-12-29 01:17:51 +0000 | [diff] [blame] | 428 | .. attribute:: rtsToggle |
| 429 | |
| 430 | :platform: Windows |
| 431 | |
| 432 | Attribute to configure RTS toggle control setting. When enabled and |
| 433 | supported by OS, RTS will be active when data is available and inactive |
| 434 | if no data is available. |
| 435 | |
| 436 | .. versionadded:: 2.6 |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 437 | .. versionchanged:: 3.0 (removed, see :meth:`rs485_mode` instead) |
cliechti | c648da9 | 2011-12-29 01:17:51 +0000 | [diff] [blame] | 438 | |
| 439 | |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 440 | Implementation detail: some attributes and functions are provided by the |
| 441 | class :class:`SerialBase` and some by the platform specific class and |
| 442 | others by the base class mentioned above. |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 443 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 444 | RS485 support |
| 445 | ------------- |
| 446 | The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to |
| 447 | enable RS485 specific support on some platforms. Currently Windows and Linux |
| 448 | (only a small number of devices) are supported. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 449 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 450 | :attr:`Serial.rs485_mode` needs to be set to an instance of |
| 451 | :class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 452 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 453 | Usage:: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 454 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 455 | ser = serial.Serial(...) |
| 456 | ser.rs485_mode = serial.rs485.RS485Settings(...) |
| 457 | ser.write(b'hello') |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 458 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 459 | There is a subclass :class:`rs485.RS485` available to emulate the RS485 support |
| 460 | on regular serial ports. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 461 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 462 | Usage:: |
| 463 | |
| 464 | ser = serial.rs485.RS485(...) |
| 465 | ser.rs485_mode = serial.rs485.RS485Settings(...) |
| 466 | ser.write(b'hello') |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 467 | |
| 468 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 469 | .. class:: rs485.RS485Settings |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 470 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 471 | A class that holds RS485 specific settings which are supported on |
| 472 | some platforms. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 473 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 474 | .. versionadded:: 3.0 |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 475 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 476 | .. 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] | 477 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 478 | :param bool rts_level_for_tx: |
| 479 | RTS level for transmission |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 480 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 481 | :param bool rts_level_for_rx: |
| 482 | RTS level for reception |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 483 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 484 | :param bool loopback: |
| 485 | When set to ``True`` transmitted data is also received. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 486 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 487 | :param float delay_before_tx: |
| 488 | Delay after setting RTS but before transmission starts |
| 489 | |
| 490 | :param float delay_before_rx: |
| 491 | Delay after transmission ends and resetting RTS |
| 492 | |
| 493 | .. attribute:: rts_level_for_tx |
| 494 | |
| 495 | RTS level for transmission. |
| 496 | |
| 497 | .. attribute:: rts_level_for_rx |
| 498 | |
| 499 | RTS level for reception. |
| 500 | |
| 501 | .. attribute:: loopback |
| 502 | |
| 503 | When set to ``True`` transmitted data is also received. |
| 504 | |
| 505 | .. attribute:: delay_before_tx |
| 506 | |
| 507 | Delay after setting RTS but before transmission starts (seconds as float). |
| 508 | |
| 509 | .. attribute:: delay_before_rx |
| 510 | |
| 511 | Delay after transmission ends and resetting RTS (seconds as float). |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 512 | |
| 513 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 514 | .. class:: rs485.RS485 |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 515 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 516 | A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS |
| 517 | according to the RS485 settings. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 518 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 519 | .. warning:: This may work unreliably on some serial ports (control signals not |
| 520 | synchronized or delayed compared to data). Using delays may be unreliable |
| 521 | (varying times, larger than expected) as the OS may not support very fine |
| 522 | grained delays (no smaller than in the order of tens of milliseconds). |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 523 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 524 | .. note:: Some implementations support this natively in the class |
| 525 | :class:`Serial`. Better performance can be expected when the native version |
| 526 | is used. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 527 | |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 528 | .. note:: The loopback property is ignored by this implementation. The actual |
| 529 | behavior depends on the used hardware. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 530 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 531 | |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 532 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 533 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 534 | :rfc:`2217` Network ports |
| 535 | ------------------------- |
| 536 | |
| 537 | .. warning:: This implementation is currently in an experimental state. Use |
| 538 | at your own risk. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 539 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 540 | .. class:: rfc2217.Serial |
| 541 | |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 542 | This implements a :rfc:`2217` compatible client. Port names are URLs_ in the |
| 543 | form: ``rfc2217://<host>:<port>[/<option>[/<option>]]`` |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 544 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 545 | This class API is compatible to :class:`Serial` with a few exceptions: |
| 546 | |
| 547 | - numbers as port name are not allowed, only URLs in the form described |
| 548 | above. |
| 549 | - writeTimeout is not implemented |
| 550 | - The current implementation starts a thread that keeps reading from the |
| 551 | (internal) socket. The thread is managed automatically by the |
| 552 | :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`. |
| 553 | However it may be a problem for user applications that like to use select |
| 554 | instead of threads. |
| 555 | |
| 556 | Due to the nature of the network and protocol involved there are a few |
| 557 | extra points to keep in mind: |
| 558 | |
| 559 | - All operations have an additional latency time. |
| 560 | - Setting control lines (RTS/CTS) needs more time. |
| 561 | - Reading the status lines (DSR/DTR etc.) returns a cached value. When that |
| 562 | cache is updated depends entirely on the server. The server itself may |
| 563 | implement a polling at a certain rate and quick changes may be invisible. |
| 564 | - The network layer also has buffers. This means that :meth:`flush`, |
| 565 | :meth:`flushInput` and :meth:`flushOutput` may work with additional delay. |
| 566 | Likewise :meth:`inWaiting` returns the size of the data arrived at the |
| 567 | object internal buffer and excludes any bytes in the network buffers or |
| 568 | any server side buffer. |
| 569 | - Closing and immediately reopening the same port may fail due to time |
| 570 | needed by the server to get ready again. |
| 571 | |
| 572 | Not implemented yet / Possible problems with the implementation: |
| 573 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 574 | - :rfc:`2217` flow control between client and server (objects internal |
| 575 | buffer may eat all your memory when never read). |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 576 | - No authentication support (servers may not prompt for a password etc.) |
| 577 | - No encryption. |
| 578 | |
| 579 | Due to lack of authentication and encryption it is not suitable to use this |
| 580 | client for connections across the internet and should only be used in |
| 581 | controlled environments. |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 582 | |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 583 | .. versionadded:: 2.5 |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 584 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 585 | |
| 586 | .. class:: rfc2217.PortManager |
| 587 | |
| 588 | This class provides helper functions for implementing :rfc:`2217` |
| 589 | compatible servers. |
| 590 | |
| 591 | Basically, it implements every thing needed for the :rfc:`2217` protocol. |
| 592 | It just does not open sockets and read/write to serial ports (though it |
| 593 | changes other port settings). The user of this class must take care of the |
| 594 | data transmission itself. The reason for that is, that this way, this class |
| 595 | supports all programming models such as threads and select. |
| 596 | |
| 597 | Usage examples can be found in the examples where two TCP/IP - serial |
| 598 | converters are shown, one using threads (the single port server) and an |
| 599 | other using select (the multi port server). |
| 600 | |
| 601 | .. note:: Each new client connection must create a new instance as this |
| 602 | object (and the :rfc:`2217` protocol) has internal state. |
| 603 | |
| 604 | .. method:: __init__(serial_port, connection, debug_output=False) |
| 605 | |
| 606 | :param serial_port: a :class:`Serial` instance that is managed. |
| 607 | :param connection: an object implementing :meth:`write`, used to write |
| 608 | to the network. |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 609 | :param debug_output: enables debug messages: a :class:`logging.Logger` |
| 610 | instance or None. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 611 | |
| 612 | Initializes the Manager and starts negotiating with client in Telnet |
| 613 | and :rfc:`2217` protocol. The negotiation starts immediately so that |
| 614 | the class should be instantiated in the moment the client connects. |
| 615 | |
| 616 | The *serial_port* can be controlled by :rfc:`2217` commands. This |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 617 | object will modify the port settings (baud rate etc.) and control lines |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 618 | (RTS/DTR) send BREAK etc. when the corresponding commands are found by |
| 619 | the :meth:`filter` method. |
| 620 | |
| 621 | The *connection* object must implement a :meth:`write(data)` function. |
| 622 | This function must ensure that *data* is written at once (no user data |
| 623 | mixed in, i.e. it must be thread-safe). All data must be sent in its |
| 624 | raw form (:meth:`escape` must not be used) as it is used to send Telnet |
| 625 | and :rfc:`2217` control commands. |
| 626 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 627 | For diagnostics of the connection or the implementation, *debug_output* |
| 628 | can be set to an instance of a :class:`logging.Logger` (e.g. |
| 629 | ``logging.getLogger('rfc2217.server')``). The caller should configure |
| 630 | the logger using ``setLevel`` for the desired detail level of the logs. |
| 631 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 632 | .. method:: escape(data) |
| 633 | |
| 634 | :param data: data to be sent over the network. |
| 635 | :return: data, escaped for Telnet/:rfc:`2217` |
| 636 | |
| 637 | A generator that escapes all data to be compatible with :rfc:`2217`. |
| 638 | Implementors of servers should use this function to process all data |
| 639 | sent over the network. |
| 640 | |
| 641 | The function returns a generator which can be used in ``for`` loops. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 642 | It can be converted to bytes using :func:`serial.to_bytes`. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 643 | |
| 644 | .. method:: filter(data) |
| 645 | |
| 646 | :param data: data read from the network, including Telnet and |
| 647 | :rfc:`2217` controls. |
| 648 | :return: data, free from Telnet and :rfc:`2217` controls. |
| 649 | |
| 650 | A generator that filters and processes all data related to :rfc:`2217`. |
| 651 | Implementors of servers should use this function to process all data |
| 652 | received from the network. |
| 653 | |
| 654 | The function returns a generator which can be used in ``for`` loops. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 655 | It can be converted to bytes using :func:`serial.to_bytes`. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 656 | |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 657 | .. method:: check_modem_lines(force_notification=False) |
| 658 | |
| 659 | :param force_notification: Set to false. Parameter is for internal use. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 660 | |
| 661 | This function needs to be called periodically (e.g. every second) when |
| 662 | the server wants to send NOTIFY_MODEMSTATE messages. This is required |
| 663 | to support the client for reading CTS/DSR/RI/CD status lines. |
| 664 | |
| 665 | The function reads the status line and issues the notifications |
| 666 | automatically. |
| 667 | |
| 668 | .. versionadded:: 2.5 |
| 669 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 670 | .. seealso:: |
| 671 | |
| 672 | :rfc:`2217` - Telnet Com Port Control Option |
| 673 | |
| 674 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 675 | Exceptions |
| 676 | ========== |
| 677 | |
| 678 | .. exception:: SerialException |
| 679 | |
| 680 | Base class for serial port exceptions. |
| 681 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 682 | .. versionchanged:: 2.5 |
cliechti | 4cb9466 | 2013-10-17 03:17:50 +0000 | [diff] [blame] | 683 | Now derives from :exc:`IOError` instead of :exc:`Exception` |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 684 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 685 | .. exception:: SerialTimeoutException |
| 686 | |
| 687 | Exception that is raised on write timeouts. |
| 688 | |
| 689 | |
| 690 | Constants |
| 691 | ========= |
| 692 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 693 | *Parity* |
| 694 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 695 | .. data:: PARITY_NONE |
| 696 | .. data:: PARITY_EVEN |
| 697 | .. data:: PARITY_ODD |
| 698 | .. data:: PARITY_MARK |
| 699 | .. data:: PARITY_SPACE |
| 700 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 701 | *Stop bits* |
| 702 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 703 | .. data:: STOPBITS_ONE |
| 704 | .. data:: STOPBITS_ONE_POINT_FIVE |
| 705 | .. data:: STOPBITS_TWO |
| 706 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 707 | 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] | 708 | bits. |
| 709 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 710 | *Byte size* |
| 711 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 712 | .. data:: FIVEBITS |
| 713 | .. data:: SIXBITS |
| 714 | .. data:: SEVENBITS |
| 715 | .. data:: EIGHTBITS |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 716 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 717 | |
| 718 | *Others* |
| 719 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 720 | Default control characters (instances of :class:`bytes` for Python 3.0+) for |
cliechti | daf47ba | 2009-07-28 01:28:16 +0000 | [diff] [blame] | 721 | software flow control: |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 722 | |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 723 | .. data:: XON |
| 724 | .. data:: XOFF |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 725 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 726 | Module version: |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 727 | |
| 728 | .. data:: VERSION |
| 729 | |
| 730 | A string indicating the pySerial version, such as ``2.5``. |
| 731 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 732 | .. versionadded:: 2.3 |
| 733 | |
cliechti | 44eb98f | 2011-03-21 21:41:10 +0000 | [diff] [blame] | 734 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 735 | Module functions and attributes |
| 736 | =============================== |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 737 | |
| 738 | .. function:: device(number) |
| 739 | |
| 740 | :param number: Port number. |
| 741 | :return: String containing device name. |
| 742 | :deprecated: Use device names directly. |
| 743 | |
| 744 | Convert a port number to a platform dependent device name. Unfortunately |
| 745 | this does not work well for all platforms; e.g. some may miss USB-Serial |
| 746 | converters and enumerate only internal serial ports. |
| 747 | |
| 748 | The conversion may be made off-line, that is, there is no guarantee that |
| 749 | the returned device name really exists on the system. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 750 | |
| 751 | |
cliechti | e3ab353 | 2009-08-05 12:40:38 +0000 | [diff] [blame] | 752 | .. function:: serial_for_url(url, \*args, \*\*kwargs) |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 753 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 754 | :param url: Device name, number or :ref:`URL <URLs>` |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 755 | :param do_not_open: When set to true, the serial port is not opened. |
| 756 | :return: an instance of :class:`Serial` or a compatible object. |
| 757 | |
| 758 | Get a native or a :rfc:`2217` implementation of the Serial class, depending |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 759 | on port/url. This factory function is useful when an application wants |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 760 | to support both, local ports and remote ports. There is also support |
| 761 | for other types, see :ref:`URL <URLs>` section below. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 762 | |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 763 | The port is not opened when a keyword parameter called *do_not_open* is |
| 764 | given and true, by default it is opened. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 765 | |
| 766 | .. versionadded:: 2.5 |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 767 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 768 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 769 | .. attribute:: protocol_handler_packages |
| 770 | |
| 771 | This attribute is a list of package names (strings) that is searched for |
| 772 | protocol handlers. |
| 773 | |
| 774 | e.g. we want to support a URL ``foobar://``. A module |
| 775 | ``my_handlers.protocol_foobar`` is provided by the user:: |
| 776 | |
| 777 | serial.protocol_handler_packages.append("my_handlers") |
| 778 | s = serial.serial_for_url("foobar://") |
| 779 | |
| 780 | For an URL starting with ``XY://`` is the function :func:`serial_for_url` |
| 781 | attempts to import ``PACKAGE.protocol_XY`` with each candidate for |
| 782 | ``PACKAGE`` from this list. |
| 783 | |
| 784 | .. versionadded:: 2.6 |
| 785 | |
| 786 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 787 | .. function:: to_bytes(sequence) |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 788 | |
| 789 | :param sequence: String or list of integers |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 790 | :returns: an instance of ``bytes`` |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 791 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 792 | 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] | 793 | compatible to Python 2.x and 3.x. |
| 794 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 795 | 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] | 796 | ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 797 | ``bytes(sequence)`` doesn't work for all versions of Python. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 798 | |
| 799 | This function is used internally and in the unit tests. |
| 800 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 801 | .. versionadded:: 2.5 |
| 802 | |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 803 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 804 | .. _URLs: |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 805 | |
| 806 | URLs |
| 807 | ---- |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 808 | The function :func:`serial_for_url` accepts the following types of URLs: |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 809 | |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 810 | - ``rfc2217://<host>:<port>[/<option>[/<option>]]`` |
| 811 | - ``socket://<host>:<port>[/<option>[/<option>]]`` |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 812 | - ``loop://[<option>[/<option>]]`` |
Chris Liechti | e9e27ff | 2015-08-20 23:46:51 +0200 | [diff] [blame^] | 813 | - ``spy://port[?option[=value][&option[=value]]]`` |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 814 | |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 815 | Device names are also supported, e.g.: |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 816 | |
| 817 | - ``/dev/ttyUSB0`` (Linux) |
| 818 | - ``COM3`` (Windows) |
| 819 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 820 | Future releases of pySerial might add more types. Since pySerial 2.6 it is also |
| 821 | possible for the user to add protocol handlers using |
| 822 | :attr:`protocol_handler_packages`. |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 823 | |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 824 | ``rfc2217://`` |
| 825 | Used to connect to :rfc:`2217` compatible servers. All serial port |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 826 | functions are supported. Implemented by :class:`rfc2217.Serial`. |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 827 | |
| 828 | Supported options in the URL are: |
| 829 | |
| 830 | - ``ign_set_control`` does not wait for acknowledges to SET_CONTROL. This |
| 831 | option can be used for non compliant servers (i.e. when getting an |
| 832 | ``remote rejected value for option 'control'`` error when connecting). |
| 833 | |
| 834 | - ``poll_modem``: The client issues NOTIFY_MODEMSTATE requests when status |
| 835 | lines are read (CTS/DTR/RI/CD). Without this option it relies on the server |
| 836 | sending the notifications automatically (that's what the RFC suggests and |
| 837 | most servers do). Enable this option when :meth:`getCTS` does not work as |
| 838 | expected, i.e. for servers that do not send notifications. |
| 839 | |
cliechti | dfe2d27 | 2009-08-10 22:19:41 +0000 | [diff] [blame] | 840 | - ``timeout=<value>``: Change network timeout (default 3 seconds). This is |
| 841 | useful when the server takes a little more time to send its answers. The |
| 842 | timeout applies to the initial Telnet / :rfc:`2271` negotiation as well |
| 843 | as changing port settings or control line change commands. |
| 844 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 845 | - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 846 | useful for end users). It uses the logging module and a logger called |
| 847 | ``pySerial.rfc2217`` so that the application can setup up logging |
| 848 | handlers etc. It will call :meth:`logging.basicConfig` which initializes |
| 849 | for output on ``sys.stderr`` (if no logging was set up already). |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 850 | |
| 851 | ``socket://`` |
| 852 | The purpose of this connection type is that applications using pySerial can |
| 853 | connect to TCP/IP to serial port converters that do not support :rfc:`2217`. |
| 854 | |
| 855 | Uses a TCP/IP socket. All serial port settings, control and status lines |
| 856 | are ignored. Only data is transmitted and received. |
| 857 | |
| 858 | Supported options in the URL are: |
| 859 | |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 860 | - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not |
| 861 | useful for end users). It uses the logging module and a logger called |
| 862 | ``pySerial.socket`` so that the application can setup up logging handlers |
| 863 | etc. It will call :meth:`logging.basicConfig` which initializes for |
| 864 | output on ``sys.stderr`` (if no logging was set up already). |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 865 | |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 866 | ``loop://`` |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 867 | The least useful type. It simulates a loop back connection |
| 868 | (``RX<->TX`` ``RTS<->CTS`` ``DTR<->DSR``). It could be used to test |
| 869 | applications or run the unit tests. |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 870 | |
| 871 | Supported options in the URL are: |
| 872 | |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 873 | - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not |
| 874 | useful for end users). It uses the logging module and a logger called |
| 875 | ``pySerial.loop`` so that the application can setup up logging handlers |
| 876 | etc. It will call :meth:`logging.basicConfig` which initializes for |
| 877 | output on ``sys.stderr`` (if no logging was set up already). |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 878 | |
cliechti | cdc660c | 2011-11-03 23:24:31 +0000 | [diff] [blame] | 879 | ``hwgrep://`` |
| 880 | This type uses :mod:`serial.tools.list_ports` to obtain a list of ports and |
| 881 | searches the list for matches by a regexp (see :py:mod:`re`) that follows |
| 882 | the slashes. |
| 883 | |
| 884 | Depending on the capabilities of the list_ports module on the system, it is |
| 885 | possible to search for the description or hardware ID of a device, e.g. USB |
| 886 | VID:PID or texts. |
| 887 | |
| 888 | Unfortunately, on some systems list_ports only lists a subset of the port |
Chris Liechti | ce62188 | 2015-08-06 19:29:31 +0200 | [diff] [blame] | 889 | names with no additional information. Currently, on Windows and Linux and |
| 890 | OSX it should find additional information. |
cliechti | cdc660c | 2011-11-03 23:24:31 +0000 | [diff] [blame] | 891 | |
Chris Liechti | e9e27ff | 2015-08-20 23:46:51 +0200 | [diff] [blame^] | 892 | ``spy://`` |
| 893 | Wrapping the native serial port, this protocol makes it possible to |
| 894 | intercept the data received and transmitted as well as the access to the |
| 895 | control lines, break and flush commands. |
| 896 | |
| 897 | Supported options in the URL are: |
| 898 | |
| 899 | - ``dev=FILENAME`` output to given file or device instead of stderr |
| 900 | - ``color`` enable ANSI escape sequences to colorize output |
| 901 | - ``raw`` output the read and written data directly (default is to create a |
| 902 | hex dump). In this mode, no control line and other commands are logged. |
| 903 | |
| 904 | Example:: |
| 905 | |
| 906 | import serial |
| 907 | |
| 908 | with serial.serial_for_url('spy:///dev/ttyUSB0?dev=test.txt', timeout=1) as s: |
| 909 | s.setDTR(False) |
| 910 | s.write('hello world') |
| 911 | s.read(20) |
| 912 | s.setDTR(True) |
| 913 | s.write(serial.to_bytes(range(256))) |
| 914 | s.read(400) |
| 915 | s.sendBreak() |
| 916 | |
| 917 | with open('test.txt') as f: |
| 918 | print(f.read()) |
| 919 | |
| 920 | Outputs:: |
| 921 | |
| 922 | 000000.002 FLSH flushInput |
| 923 | 000000.002 DTR inactive |
| 924 | 000000.002 TX 0000 68 65 6C 6C 6F 20 77 6F 72 6C 64 hello wo rld |
| 925 | 000001.015 RX 0000 68 65 6C 6C 6F 20 77 6F 72 6C 64 hello wo rld |
| 926 | 000001.015 DTR active |
| 927 | 000001.015 TX 0000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ........ ........ |
| 928 | 000001.015 TX 0010 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ........ ........ |
| 929 | 000001.015 TX 0020 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&' ()*+,-./ |
| 930 | 000001.015 TX 0030 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 01234567 89:;<=>? |
| 931 | 000001.015 TX 0040 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F @ABCDEFG HIJKLMNO |
| 932 | 000001.016 TX 0050 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F PQRSTUVW XYZ[\]^_ |
| 933 | 000001.016 TX 0060 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F `abcdefg hijklmno |
| 934 | 000001.016 TX 0070 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F pqrstuvw xyz{|}~. |
| 935 | 000001.016 TX 0080 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F ........ ........ |
| 936 | 000001.016 TX 0090 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F ........ ........ |
| 937 | 000001.016 TX 00A0 A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF ........ ........ |
| 938 | 000001.016 TX 00B0 B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF ........ ........ |
| 939 | 000001.016 TX 00C0 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF ........ ........ |
| 940 | 000001.016 TX 00D0 D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF ........ ........ |
| 941 | 000001.016 TX 00E0 E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF ........ ........ |
| 942 | 000001.016 TX 00F0 F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF ........ ........ |
| 943 | 000002.284 RX 0000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ........ ........ |
| 944 | 000002.284 RX 0010 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ........ ........ |
| 945 | 000002.284 RX 0020 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&' ()*+,-./ |
| 946 | 000002.284 RX 0030 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 01234567 89:;<=>? |
| 947 | 000002.284 RX 0040 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F @ABCDEFG HIJKLMNO |
| 948 | 000002.284 RX 0050 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F PQRSTUVW XYZ[\]^_ |
| 949 | 000002.284 RX 0060 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F `abcdefg hijklmno |
| 950 | 000002.284 RX 0070 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F pqrstuvw xyz{|}~. |
| 951 | 000002.284 RX 0080 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F ........ ........ |
| 952 | 000002.284 RX 0090 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F ........ ........ |
| 953 | 000002.284 RX 00A0 A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF ........ ........ |
| 954 | 000002.284 RX 00B0 B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF ........ ........ |
| 955 | 000002.284 RX 00C0 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF ........ ........ |
| 956 | 000002.284 RX 00D0 D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF ........ ........ |
| 957 | 000002.284 RX 00E0 E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF ........ ........ |
| 958 | 000002.284 RX 00F0 F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF ........ ........ |
| 959 | 000002.284 BRK sendBreak 0.25 |
| 960 | |
cliechti | cdc660c | 2011-11-03 23:24:31 +0000 | [diff] [blame] | 961 | |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 962 | |
cliechti | dfe2d27 | 2009-08-10 22:19:41 +0000 | [diff] [blame] | 963 | Examples: |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 964 | |
cliechti | dfe2d27 | 2009-08-10 22:19:41 +0000 | [diff] [blame] | 965 | - ``rfc2217://localhost:7000`` |
| 966 | - ``rfc2217://localhost:7000/poll_modem`` |
| 967 | - ``rfc2217://localhost:7000/ign_set_control/timeout=5.5`` |
| 968 | - ``socket://localhost:7777`` |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 969 | - ``loop://logging=debug`` |
cliechti | cdc660c | 2011-11-03 23:24:31 +0000 | [diff] [blame] | 970 | - ``hwgrep://0451:f432`` (USB VID:PID) |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 971 | |
| 972 | Tools |
| 973 | ===== |
| 974 | |
| 975 | |
| 976 | serial.tools.list_ports |
| 977 | ----------------------- |
| 978 | .. module:: serial.tools.list_ports |
| 979 | .. versionadded:: 2.6 |
| 980 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 981 | This module can be executed to get a list of ports (``python -m |
| 982 | serial.tools.list_ports``). It also contains the following functions. |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 983 | |
| 984 | |
| 985 | .. function:: comports() |
| 986 | |
| 987 | :return: an iterable. |
| 988 | |
| 989 | The function returns an iterable that yields tuples of three strings: |
| 990 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 991 | - port name as it can be passed to :class:`serial.Serial` or |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 992 | :func:`serial.serial_for_url` |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 993 | - description in human readable form |
| 994 | - sort of hardware ID. E.g. may contain VID:PID of USB-serial adapters. |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 995 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 996 | Items are returned in no particular order. It may make sense to sort the |
| 997 | items. Also note that the reported strings are different across platforms |
| 998 | and operating systems, even for the same device. |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 999 | |
| 1000 | .. note:: Support is limited to a number of operating systems. On some |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 1001 | systems description and hardware ID will not be available |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 1002 | (``None``). |
| 1003 | |
| 1004 | :platform: Posix (/dev files) |
| 1005 | :platform: Linux (/dev files, sysfs and lsusb) |
cliechti | 4cb9466 | 2013-10-17 03:17:50 +0000 | [diff] [blame] | 1006 | :platform: OSX (iokit) |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 1007 | :platform: Windows (setupapi, registry) |
| 1008 | |
| 1009 | |
| 1010 | .. function:: grep(regexp) |
| 1011 | |
| 1012 | :param regexp: regular expression (see stdlib :mod:`re`) |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 1013 | :return: filtered sequence, see :func:`comports`. |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 1014 | |
| 1015 | Search for ports using a regular expression. Port name, description and |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 1016 | hardware ID are searched (case insensitive). The function returns an |
| 1017 | iterable that contains the same tuples that :func:`comport` generates but |
| 1018 | only those that match the regexp. |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 1019 | |
| 1020 | |
| 1021 | serial.tools.miniterm |
| 1022 | ----------------------- |
| 1023 | .. module:: serial.tools.miniterm |
| 1024 | .. versionadded:: 2.6 |
| 1025 | |
| 1026 | Miniterm is now available as module instead of example. |
| 1027 | see :ref:`miniterm` for details. |
| 1028 | |