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 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 20 | :param baudrate: |
| 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 | |
| 38 | :param timeout: |
| 39 | Set a read timeout value. |
| 40 | |
| 41 | :param xonxoff: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 42 | Enable software flow control. |
| 43 | |
| 44 | :param rtscts: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 45 | Enable hardware (RTS/CTS) flow control. |
| 46 | |
cliechti | 07709e4 | 2010-05-21 00:30:09 +0000 | [diff] [blame] | 47 | :param dsrdtr: |
| 48 | Enable hardware (DSR/DTR) flow control. |
| 49 | |
| 50 | :param writeTimeout: |
| 51 | Set a write timeout value. |
| 52 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 53 | :param 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 | |
| 283 | The following constants are also provided: |
| 284 | |
| 285 | .. attribute:: BAUDRATES |
| 286 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 287 | A list of valid baud rates. The list may be incomplete such that higher |
| 288 | baud rates may be supported by the device and that values in between the |
| 289 | standard baud rates are supported. (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 290 | |
| 291 | .. attribute:: BYTESIZES |
| 292 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 293 | A list of valid byte sizes for the device (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 294 | |
| 295 | .. attribute:: PARITIES |
| 296 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 297 | A list of valid parities for the device (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 298 | |
| 299 | .. attribute:: STOPBITS |
| 300 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 301 | A list of valid stop bit widths for the device (Read Only). |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 302 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 303 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 304 | The following methods are for compatibility with the :mod:`io` library. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 305 | |
| 306 | .. method:: readable() |
| 307 | |
| 308 | :return: True |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 309 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 310 | .. versionadded:: 2.5 |
| 311 | |
| 312 | .. method:: writable() |
| 313 | |
| 314 | :return: True |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 315 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 316 | .. versionadded:: 2.5 |
| 317 | |
| 318 | .. method:: seekable() |
| 319 | |
| 320 | :return: False |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 321 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 322 | .. versionadded:: 2.5 |
| 323 | |
| 324 | .. method:: readinto(b) |
| 325 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 326 | :param b: bytearray or array instance |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 327 | :return: Number of byte read |
| 328 | |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 329 | Read up to len(b) bytes into :class:`bytearray` *b* and return the |
| 330 | number of bytes read. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 331 | |
| 332 | .. versionadded:: 2.5 |
| 333 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 334 | The port settings can be read and written as dictionary. |
| 335 | |
cliechti | 7053b23 | 2009-08-10 01:37:34 +0000 | [diff] [blame] | 336 | .. method:: getSettingsDict() |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 337 | |
| 338 | :return: a dictionary with current port settings. |
| 339 | |
| 340 | Get a dictionary with port settings. This is useful to backup the |
| 341 | current settings so that a later point in time they can be restored |
| 342 | using :meth:`applySettingsDict`. |
| 343 | |
| 344 | Note that control lines (RTS/DTR) are part of the settings. |
| 345 | |
| 346 | .. versionadded:: 2.5 |
| 347 | |
cliechti | 7053b23 | 2009-08-10 01:37:34 +0000 | [diff] [blame] | 348 | .. method:: applySettingsDict(d) |
cliechti | 4065dce | 2009-08-10 00:55:46 +0000 | [diff] [blame] | 349 | |
| 350 | :param d: a dictionary with port settings. |
| 351 | |
| 352 | Applies a dictionary that was created by :meth:`getSettingsDict`. Only |
| 353 | changes are applied and when a key is missing it means that the setting |
| 354 | stays unchanged. |
| 355 | |
| 356 | Note that control lines (RTS/DTR) are not changed. |
| 357 | |
| 358 | .. versionadded:: 2.5 |
| 359 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 360 | Platform specific methods. |
| 361 | |
cliechti | c648da9 | 2011-12-29 01:17:51 +0000 | [diff] [blame] | 362 | .. warning:: Programs using the following methods and attributes are not |
| 363 | portable to other platforms! |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 364 | |
cliechti | 28b8fd0 | 2011-12-28 21:39:42 +0000 | [diff] [blame] | 365 | .. method:: outWaiting() |
| 366 | |
| 367 | :platform: Unix |
| 368 | :platform: Windows |
| 369 | |
| 370 | Return the number of bytes in the output buffer. |
| 371 | |
| 372 | .. versionchanged:: 2.7 (Posix support added) |
| 373 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 374 | .. method:: nonblocking() |
| 375 | |
| 376 | :platform: Unix |
| 377 | |
| 378 | Configure the device for nonblocking operation. This can be useful if |
| 379 | the port is used with :mod:`select`. |
| 380 | |
| 381 | .. method:: fileno() |
| 382 | |
| 383 | :platform: Unix |
| 384 | :return: File descriptor. |
| 385 | |
| 386 | Return file descriptor number for the port that is opened by this object. |
| 387 | It is useful when serial ports are used with :mod:`select`. |
| 388 | |
| 389 | .. method:: setXON(level=True) |
| 390 | |
| 391 | :platform: Windows |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 392 | :platform: Posix |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 393 | :param level: Set flow control state. |
| 394 | |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 395 | Manually control flow - when software flow control is enabled. |
| 396 | |
| 397 | This will send XON (true) and XOFF (false) to the other device. |
| 398 | |
| 399 | .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``) |
| 400 | |
| 401 | .. method:: flowControlOut(enable) |
| 402 | |
| 403 | :platform: Posix |
| 404 | :param enable: Set flow control state. |
| 405 | |
| 406 | Manually control flow of outgoing data - when hardware or software flow |
| 407 | control is enabled. |
| 408 | |
| 409 | Sending will be suspended when called with ``False`` and enabled when |
| 410 | called with ``True``. |
| 411 | |
| 412 | .. versionadded:: 2.7 (Posix support added) |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 413 | |
cliechti | c648da9 | 2011-12-29 01:17:51 +0000 | [diff] [blame] | 414 | .. attribute:: rtsToggle |
| 415 | |
| 416 | :platform: Windows |
| 417 | |
| 418 | Attribute to configure RTS toggle control setting. When enabled and |
| 419 | supported by OS, RTS will be active when data is available and inactive |
| 420 | if no data is available. |
| 421 | |
| 422 | .. versionadded:: 2.6 |
| 423 | |
| 424 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 425 | .. note:: |
| 426 | |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 427 | For systems that provide the :py:mod:`io` library (Python 2.6 and newer), the |
| 428 | class :class:`Serial` will derive from :py:class:`io.RawIOBase`. For all |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 429 | others from :class:`FileLike`. |
| 430 | |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 431 | Implementation detail: some attributes and functions are provided by the |
| 432 | class :class:`SerialBase` and some by the platform specific class and |
| 433 | others by the base class mentioned above. |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 434 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 435 | .. class:: FileLike |
| 436 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 437 | An abstract file like class. It is used as base class for :class:`Serial` |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 438 | when no :py:mod:`io` module is available. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 439 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 440 | This class implements :meth:`readline` and :meth:`readlines` based on |
| 441 | :meth:`read` and :meth:`writelines` based on :meth:`write`. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 442 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 443 | Note that when the serial port was opened with no timeout, that |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 444 | :meth:`readline` blocks until it sees a newline (or the specified size is |
| 445 | reached) and that :meth:`readlines` would never return and therefore |
| 446 | refuses to work (it raises an exception in this case)! |
| 447 | |
| 448 | .. method:: writelines(sequence) |
| 449 | |
| 450 | Write a list of strings to the port. |
| 451 | |
| 452 | |
| 453 | The following three methods are overridden in :class:`Serial`. |
| 454 | |
| 455 | .. method:: flush() |
| 456 | |
| 457 | Flush of file like objects. It's a no-op in this class, may be overridden. |
| 458 | |
| 459 | .. method:: read() |
| 460 | |
| 461 | Raises NotImplementedError, needs to be overridden by subclass. |
| 462 | |
| 463 | .. method:: write(data) |
| 464 | |
| 465 | Raises NotImplementedError, needs to be overridden by subclass. |
| 466 | |
| 467 | The following functions are implemented for compatibility with other |
| 468 | file-like objects, however serial ports are not seekable. |
| 469 | |
| 470 | |
| 471 | .. method:: seek(pos, whence=0) |
| 472 | |
| 473 | :exception IOError: always, as method is not supported on serial port |
| 474 | |
| 475 | .. versionadded:: 2.5 |
| 476 | |
| 477 | .. method:: tell() |
| 478 | |
| 479 | :exception IOError: always, as method is not supported on serial port |
| 480 | |
| 481 | .. versionadded:: 2.5 |
| 482 | |
| 483 | .. method:: truncate(self, n=None) |
| 484 | |
| 485 | :exception IOError: always, as method is not supported on serial port |
| 486 | |
| 487 | .. versionadded:: 2.5 |
| 488 | |
| 489 | .. method:: isatty() |
| 490 | |
| 491 | :exception IOError: always, as method is not supported on serial port |
| 492 | |
| 493 | .. versionadded:: 2.5 |
| 494 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 495 | To be able to use the file like object as iterator for e.g. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 496 | ``for line in Serial(0): ...`` usage: |
| 497 | |
| 498 | .. method:: next() |
| 499 | |
| 500 | Return the next line by calling :meth:`readline`. |
| 501 | |
| 502 | .. method:: __iter__() |
| 503 | |
| 504 | Returns self. |
| 505 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 506 | Other high level access functions. |
| 507 | |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 508 | .. method:: readline(size=None, eol='\\n') |
| 509 | |
| 510 | :param size: Max number of bytes to read, ``None`` -> no limit. |
| 511 | :param eol: The end of line character. |
| 512 | |
| 513 | Read a line which is terminated with end-of-line (*eol*) character |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 514 | (``\n`` by default) or until timeout. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 515 | |
| 516 | .. method:: readlines(sizehint=None, eol='\\n') |
| 517 | |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 518 | :param sizehint: Ignored parameter. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 519 | :param eol: The end of line character. |
| 520 | |
| 521 | Read a list of lines, until timeout. *sizehint* is ignored and only |
| 522 | present for API compatibility with built-in File objects. |
| 523 | |
| 524 | Note that this function only returns on a timeout. |
| 525 | |
| 526 | .. method:: xreadlines(sizehint=None) |
| 527 | |
| 528 | Read lines, implemented as generator. Unlike *readlines* (that only |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 529 | returns on a timeout) is this function yielding lines as they are |
| 530 | received. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 531 | |
| 532 | .. deprecated:: 2.5 |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 533 | Use ``for line in Serial(...): ...`` instead. This method is not |
| 534 | available in Python 2.6 and newer where the :mod:`io` library is |
| 535 | available and pySerial bases on it. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 536 | |
| 537 | .. versionchanged:: 2.5 |
| 538 | Implement as generator. |
| 539 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 540 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 541 | :rfc:`2217` Network ports |
| 542 | ------------------------- |
| 543 | |
| 544 | .. warning:: This implementation is currently in an experimental state. Use |
| 545 | at your own risk. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 546 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 547 | .. class:: rfc2217.Serial |
| 548 | |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 549 | This implements a :rfc:`2217` compatible client. Port names are URLs_ in the |
| 550 | form: ``rfc2217://<host>:<port>[/<option>[/<option>]]`` |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 551 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 552 | This class API is compatible to :class:`Serial` with a few exceptions: |
| 553 | |
| 554 | - numbers as port name are not allowed, only URLs in the form described |
| 555 | above. |
| 556 | - writeTimeout is not implemented |
| 557 | - The current implementation starts a thread that keeps reading from the |
| 558 | (internal) socket. The thread is managed automatically by the |
| 559 | :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`. |
| 560 | However it may be a problem for user applications that like to use select |
| 561 | instead of threads. |
| 562 | |
| 563 | Due to the nature of the network and protocol involved there are a few |
| 564 | extra points to keep in mind: |
| 565 | |
| 566 | - All operations have an additional latency time. |
| 567 | - Setting control lines (RTS/CTS) needs more time. |
| 568 | - Reading the status lines (DSR/DTR etc.) returns a cached value. When that |
| 569 | cache is updated depends entirely on the server. The server itself may |
| 570 | implement a polling at a certain rate and quick changes may be invisible. |
| 571 | - The network layer also has buffers. This means that :meth:`flush`, |
| 572 | :meth:`flushInput` and :meth:`flushOutput` may work with additional delay. |
| 573 | Likewise :meth:`inWaiting` returns the size of the data arrived at the |
| 574 | object internal buffer and excludes any bytes in the network buffers or |
| 575 | any server side buffer. |
| 576 | - Closing and immediately reopening the same port may fail due to time |
| 577 | needed by the server to get ready again. |
| 578 | |
| 579 | Not implemented yet / Possible problems with the implementation: |
| 580 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 581 | - :rfc:`2217` flow control between client and server (objects internal |
| 582 | buffer may eat all your memory when never read). |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 583 | - No authentication support (servers may not prompt for a password etc.) |
| 584 | - No encryption. |
| 585 | |
| 586 | Due to lack of authentication and encryption it is not suitable to use this |
| 587 | client for connections across the internet and should only be used in |
| 588 | controlled environments. |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 589 | |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 590 | .. versionadded:: 2.5 |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 591 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 592 | |
| 593 | .. class:: rfc2217.PortManager |
| 594 | |
| 595 | This class provides helper functions for implementing :rfc:`2217` |
| 596 | compatible servers. |
| 597 | |
| 598 | Basically, it implements every thing needed for the :rfc:`2217` protocol. |
| 599 | It just does not open sockets and read/write to serial ports (though it |
| 600 | changes other port settings). The user of this class must take care of the |
| 601 | data transmission itself. The reason for that is, that this way, this class |
| 602 | supports all programming models such as threads and select. |
| 603 | |
| 604 | Usage examples can be found in the examples where two TCP/IP - serial |
| 605 | converters are shown, one using threads (the single port server) and an |
| 606 | other using select (the multi port server). |
| 607 | |
| 608 | .. note:: Each new client connection must create a new instance as this |
| 609 | object (and the :rfc:`2217` protocol) has internal state. |
| 610 | |
| 611 | .. method:: __init__(serial_port, connection, debug_output=False) |
| 612 | |
| 613 | :param serial_port: a :class:`Serial` instance that is managed. |
| 614 | :param connection: an object implementing :meth:`write`, used to write |
| 615 | to the network. |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 616 | :param debug_output: enables debug messages: a :class:`logging.Logger` |
| 617 | instance or None. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 618 | |
| 619 | Initializes the Manager and starts negotiating with client in Telnet |
| 620 | and :rfc:`2217` protocol. The negotiation starts immediately so that |
| 621 | the class should be instantiated in the moment the client connects. |
| 622 | |
| 623 | The *serial_port* can be controlled by :rfc:`2217` commands. This |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 624 | object will modify the port settings (baud rate etc.) and control lines |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 625 | (RTS/DTR) send BREAK etc. when the corresponding commands are found by |
| 626 | the :meth:`filter` method. |
| 627 | |
| 628 | The *connection* object must implement a :meth:`write(data)` function. |
| 629 | This function must ensure that *data* is written at once (no user data |
| 630 | mixed in, i.e. it must be thread-safe). All data must be sent in its |
| 631 | raw form (:meth:`escape` must not be used) as it is used to send Telnet |
| 632 | and :rfc:`2217` control commands. |
| 633 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 634 | For diagnostics of the connection or the implementation, *debug_output* |
| 635 | can be set to an instance of a :class:`logging.Logger` (e.g. |
| 636 | ``logging.getLogger('rfc2217.server')``). The caller should configure |
| 637 | the logger using ``setLevel`` for the desired detail level of the logs. |
| 638 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 639 | .. method:: escape(data) |
| 640 | |
| 641 | :param data: data to be sent over the network. |
| 642 | :return: data, escaped for Telnet/:rfc:`2217` |
| 643 | |
| 644 | A generator that escapes all data to be compatible with :rfc:`2217`. |
| 645 | Implementors of servers should use this function to process all data |
| 646 | sent over the network. |
| 647 | |
| 648 | The function returns a generator which can be used in ``for`` loops. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 649 | It can be converted to bytes using :func:`serial.to_bytes`. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 650 | |
| 651 | .. method:: filter(data) |
| 652 | |
| 653 | :param data: data read from the network, including Telnet and |
| 654 | :rfc:`2217` controls. |
| 655 | :return: data, free from Telnet and :rfc:`2217` controls. |
| 656 | |
| 657 | A generator that filters and processes all data related to :rfc:`2217`. |
| 658 | Implementors of servers should use this function to process all data |
| 659 | received from the network. |
| 660 | |
| 661 | The function returns a generator which can be used in ``for`` loops. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 662 | It can be converted to bytes using :func:`serial.to_bytes`. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 663 | |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 664 | .. method:: check_modem_lines(force_notification=False) |
| 665 | |
| 666 | :param force_notification: Set to false. Parameter is for internal use. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 667 | |
| 668 | This function needs to be called periodically (e.g. every second) when |
| 669 | the server wants to send NOTIFY_MODEMSTATE messages. This is required |
| 670 | to support the client for reading CTS/DSR/RI/CD status lines. |
| 671 | |
| 672 | The function reads the status line and issues the notifications |
| 673 | automatically. |
| 674 | |
| 675 | .. versionadded:: 2.5 |
| 676 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 677 | .. seealso:: |
| 678 | |
| 679 | :rfc:`2217` - Telnet Com Port Control Option |
| 680 | |
| 681 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 682 | Exceptions |
| 683 | ========== |
| 684 | |
| 685 | .. exception:: SerialException |
| 686 | |
| 687 | Base class for serial port exceptions. |
| 688 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 689 | .. versionchanged:: 2.5 |
cliechti | 4cb9466 | 2013-10-17 03:17:50 +0000 | [diff] [blame] | 690 | Now derives from :exc:`IOError` instead of :exc:`Exception` |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 691 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 692 | .. exception:: SerialTimeoutException |
| 693 | |
| 694 | Exception that is raised on write timeouts. |
| 695 | |
| 696 | |
| 697 | Constants |
| 698 | ========= |
| 699 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 700 | *Parity* |
| 701 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 702 | .. data:: PARITY_NONE |
| 703 | .. data:: PARITY_EVEN |
| 704 | .. data:: PARITY_ODD |
| 705 | .. data:: PARITY_MARK |
| 706 | .. data:: PARITY_SPACE |
| 707 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 708 | *Stop bits* |
| 709 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 710 | .. data:: STOPBITS_ONE |
| 711 | .. data:: STOPBITS_ONE_POINT_FIVE |
| 712 | .. data:: STOPBITS_TWO |
| 713 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 714 | 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] | 715 | bits. |
| 716 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 717 | *Byte size* |
| 718 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 719 | .. data:: FIVEBITS |
| 720 | .. data:: SIXBITS |
| 721 | .. data:: SEVENBITS |
| 722 | .. data:: EIGHTBITS |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 723 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 724 | |
| 725 | *Others* |
| 726 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 727 | Default control characters (instances of :class:`bytes` for Python 3.0+) for |
cliechti | daf47ba | 2009-07-28 01:28:16 +0000 | [diff] [blame] | 728 | software flow control: |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 729 | |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 730 | .. data:: XON |
| 731 | .. data:: XOFF |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 732 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 733 | Module version: |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 734 | |
| 735 | .. data:: VERSION |
| 736 | |
| 737 | A string indicating the pySerial version, such as ``2.5``. |
| 738 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 739 | .. versionadded:: 2.3 |
| 740 | |
cliechti | 44eb98f | 2011-03-21 21:41:10 +0000 | [diff] [blame] | 741 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 742 | Module functions and attributes |
| 743 | =============================== |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 744 | |
| 745 | .. function:: device(number) |
| 746 | |
| 747 | :param number: Port number. |
| 748 | :return: String containing device name. |
| 749 | :deprecated: Use device names directly. |
| 750 | |
| 751 | Convert a port number to a platform dependent device name. Unfortunately |
| 752 | this does not work well for all platforms; e.g. some may miss USB-Serial |
| 753 | converters and enumerate only internal serial ports. |
| 754 | |
| 755 | The conversion may be made off-line, that is, there is no guarantee that |
| 756 | the returned device name really exists on the system. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 757 | |
| 758 | |
cliechti | e3ab353 | 2009-08-05 12:40:38 +0000 | [diff] [blame] | 759 | .. function:: serial_for_url(url, \*args, \*\*kwargs) |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 760 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 761 | :param url: Device name, number or :ref:`URL <URLs>` |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 762 | :param do_not_open: When set to true, the serial port is not opened. |
| 763 | :return: an instance of :class:`Serial` or a compatible object. |
| 764 | |
| 765 | Get a native or a :rfc:`2217` implementation of the Serial class, depending |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 766 | on port/url. This factory function is useful when an application wants |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 767 | to support both, local ports and remote ports. There is also support |
| 768 | for other types, see :ref:`URL <URLs>` section below. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 769 | |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 770 | The port is not opened when a keyword parameter called *do_not_open* is |
| 771 | given and true, by default it is opened. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 772 | |
| 773 | .. versionadded:: 2.5 |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 774 | |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 775 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 776 | .. attribute:: protocol_handler_packages |
| 777 | |
| 778 | This attribute is a list of package names (strings) that is searched for |
| 779 | protocol handlers. |
| 780 | |
| 781 | e.g. we want to support a URL ``foobar://``. A module |
| 782 | ``my_handlers.protocol_foobar`` is provided by the user:: |
| 783 | |
| 784 | serial.protocol_handler_packages.append("my_handlers") |
| 785 | s = serial.serial_for_url("foobar://") |
| 786 | |
| 787 | For an URL starting with ``XY://`` is the function :func:`serial_for_url` |
| 788 | attempts to import ``PACKAGE.protocol_XY`` with each candidate for |
| 789 | ``PACKAGE`` from this list. |
| 790 | |
| 791 | .. versionadded:: 2.6 |
| 792 | |
| 793 | |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 794 | .. function:: to_bytes(sequence) |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 795 | |
| 796 | :param sequence: String or list of integers |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 797 | :returns: an instance of ``bytes`` |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 798 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 799 | 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] | 800 | compatible to Python 2.x and 3.x. |
| 801 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 802 | 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] | 803 | ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 804 | ``bytes(sequence)`` doesn't work for all versions of Python. |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 805 | |
| 806 | This function is used internally and in the unit tests. |
| 807 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 808 | .. versionadded:: 2.5 |
| 809 | |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 810 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 811 | .. _URLs: |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 812 | |
| 813 | URLs |
| 814 | ---- |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 815 | The function :func:`serial_for_url` accepts the following types of URLs: |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 816 | |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 817 | - ``rfc2217://<host>:<port>[/<option>[/<option>]]`` |
| 818 | - ``socket://<host>:<port>[/<option>[/<option>]]`` |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 819 | - ``loop://[<option>[/<option>]]`` |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 820 | |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 821 | Device names are also supported, e.g.: |
cliechti | 8768624 | 2009-08-18 00:58:31 +0000 | [diff] [blame] | 822 | |
| 823 | - ``/dev/ttyUSB0`` (Linux) |
| 824 | - ``COM3`` (Windows) |
| 825 | |
cliechti | e542b36 | 2011-03-18 00:49:16 +0000 | [diff] [blame] | 826 | Future releases of pySerial might add more types. Since pySerial 2.6 it is also |
| 827 | possible for the user to add protocol handlers using |
| 828 | :attr:`protocol_handler_packages`. |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 829 | |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 830 | ``rfc2217://`` |
| 831 | Used to connect to :rfc:`2217` compatible servers. All serial port |
cliechti | 25375b5 | 2010-07-21 23:27:13 +0000 | [diff] [blame] | 832 | functions are supported. Implemented by :class:`rfc2217.Serial`. |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 833 | |
| 834 | Supported options in the URL are: |
| 835 | |
| 836 | - ``ign_set_control`` does not wait for acknowledges to SET_CONTROL. This |
| 837 | option can be used for non compliant servers (i.e. when getting an |
| 838 | ``remote rejected value for option 'control'`` error when connecting). |
| 839 | |
| 840 | - ``poll_modem``: The client issues NOTIFY_MODEMSTATE requests when status |
| 841 | lines are read (CTS/DTR/RI/CD). Without this option it relies on the server |
| 842 | sending the notifications automatically (that's what the RFC suggests and |
| 843 | most servers do). Enable this option when :meth:`getCTS` does not work as |
| 844 | expected, i.e. for servers that do not send notifications. |
| 845 | |
cliechti | dfe2d27 | 2009-08-10 22:19:41 +0000 | [diff] [blame] | 846 | - ``timeout=<value>``: Change network timeout (default 3 seconds). This is |
| 847 | useful when the server takes a little more time to send its answers. The |
| 848 | timeout applies to the initial Telnet / :rfc:`2271` negotiation as well |
| 849 | as changing port settings or control line change commands. |
| 850 | |
cliechti | 86844e8 | 2009-08-12 00:05:33 +0000 | [diff] [blame] | 851 | - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 852 | useful for end users). It uses the logging module and a logger called |
| 853 | ``pySerial.rfc2217`` so that the application can setup up logging |
| 854 | handlers etc. It will call :meth:`logging.basicConfig` which initializes |
| 855 | for output on ``sys.stderr`` (if no logging was set up already). |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 856 | |
| 857 | ``socket://`` |
| 858 | The purpose of this connection type is that applications using pySerial can |
| 859 | connect to TCP/IP to serial port converters that do not support :rfc:`2217`. |
| 860 | |
| 861 | Uses a TCP/IP socket. All serial port settings, control and status lines |
| 862 | are ignored. Only data is transmitted and received. |
| 863 | |
| 864 | Supported options in the URL are: |
| 865 | |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 866 | - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not |
| 867 | useful for end users). It uses the logging module and a logger called |
| 868 | ``pySerial.socket`` so that the application can setup up logging handlers |
| 869 | etc. It will call :meth:`logging.basicConfig` which initializes for |
| 870 | output on ``sys.stderr`` (if no logging was set up already). |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame] | 871 | |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 872 | ``loop://`` |
cliechti | e214ff8 | 2010-07-21 15:48:47 +0000 | [diff] [blame] | 873 | The least useful type. It simulates a loop back connection |
| 874 | (``RX<->TX`` ``RTS<->CTS`` ``DTR<->DSR``). It could be used to test |
| 875 | applications or run the unit tests. |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 876 | |
| 877 | Supported options in the URL are: |
| 878 | |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 879 | - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not |
| 880 | useful for end users). It uses the logging module and a logger called |
| 881 | ``pySerial.loop`` so that the application can setup up logging handlers |
| 882 | etc. It will call :meth:`logging.basicConfig` which initializes for |
| 883 | output on ``sys.stderr`` (if no logging was set up already). |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 884 | |
cliechti | cdc660c | 2011-11-03 23:24:31 +0000 | [diff] [blame] | 885 | ``hwgrep://`` |
| 886 | This type uses :mod:`serial.tools.list_ports` to obtain a list of ports and |
| 887 | searches the list for matches by a regexp (see :py:mod:`re`) that follows |
| 888 | the slashes. |
| 889 | |
| 890 | Depending on the capabilities of the list_ports module on the system, it is |
| 891 | possible to search for the description or hardware ID of a device, e.g. USB |
| 892 | VID:PID or texts. |
| 893 | |
| 894 | Unfortunately, on some systems list_ports only lists a subset of the port |
| 895 | names with no additional information. Currently, on Windows and Linux it |
| 896 | should find additional information. |
| 897 | |
| 898 | |
cliechti | 41973a9 | 2009-08-06 02:18:21 +0000 | [diff] [blame] | 899 | |
cliechti | dfe2d27 | 2009-08-10 22:19:41 +0000 | [diff] [blame] | 900 | Examples: |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 901 | |
cliechti | dfe2d27 | 2009-08-10 22:19:41 +0000 | [diff] [blame] | 902 | - ``rfc2217://localhost:7000`` |
| 903 | - ``rfc2217://localhost:7000/poll_modem`` |
| 904 | - ``rfc2217://localhost:7000/ign_set_control/timeout=5.5`` |
| 905 | - ``socket://localhost:7777`` |
cliechti | c64ba69 | 2009-08-12 00:32:47 +0000 | [diff] [blame] | 906 | - ``loop://logging=debug`` |
cliechti | cdc660c | 2011-11-03 23:24:31 +0000 | [diff] [blame] | 907 | - ``hwgrep://0451:f432`` (USB VID:PID) |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 908 | |
| 909 | Tools |
| 910 | ===== |
| 911 | |
| 912 | |
| 913 | serial.tools.list_ports |
| 914 | ----------------------- |
| 915 | .. module:: serial.tools.list_ports |
| 916 | .. versionadded:: 2.6 |
| 917 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 918 | This module can be executed to get a list of ports (``python -m |
| 919 | serial.tools.list_ports``). It also contains the following functions. |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 920 | |
| 921 | |
| 922 | .. function:: comports() |
| 923 | |
| 924 | :return: an iterable. |
| 925 | |
| 926 | The function returns an iterable that yields tuples of three strings: |
| 927 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 928 | - port name as it can be passed to :class:`serial.Serial` or |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 929 | :func:`serial.serial_for_url` |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 930 | - description in human readable form |
| 931 | - 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] | 932 | |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 933 | Items are returned in no particular order. It may make sense to sort the |
| 934 | items. Also note that the reported strings are different across platforms |
| 935 | and operating systems, even for the same device. |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 936 | |
| 937 | .. note:: Support is limited to a number of operating systems. On some |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 938 | systems description and hardware ID will not be available |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 939 | (``None``). |
| 940 | |
| 941 | :platform: Posix (/dev files) |
| 942 | :platform: Linux (/dev files, sysfs and lsusb) |
cliechti | 4cb9466 | 2013-10-17 03:17:50 +0000 | [diff] [blame] | 943 | :platform: OSX (iokit) |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 944 | :platform: Windows (setupapi, registry) |
| 945 | |
| 946 | |
| 947 | .. function:: grep(regexp) |
| 948 | |
| 949 | :param regexp: regular expression (see stdlib :mod:`re`) |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 950 | :return: filtered sequence, see :func:`comports`. |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 951 | |
| 952 | Search for ports using a regular expression. Port name, description and |
cliechti | 06281be | 2011-08-25 23:08:29 +0000 | [diff] [blame] | 953 | hardware ID are searched (case insensitive). The function returns an |
| 954 | iterable that contains the same tuples that :func:`comport` generates but |
| 955 | only those that match the regexp. |
cliechti | c56e41d | 2011-08-25 22:06:38 +0000 | [diff] [blame] | 956 | |
| 957 | |
| 958 | serial.tools.miniterm |
| 959 | ----------------------- |
| 960 | .. module:: serial.tools.miniterm |
| 961 | .. versionadded:: 2.6 |
| 962 | |
| 963 | Miniterm is now available as module instead of example. |
| 964 | see :ref:`miniterm` for details. |
| 965 | |