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 | |
| 15 | .. method:: __init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=0, rtscts=0, interCharTimeout=None) |
| 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: |
| 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: |
| 35 | :const:`STOPBITS_ONE` :const:`STOPBITS_ONE_POINT_FIVE` |
| 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 | |
| 47 | :param interCharTimeout: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 48 | Inter-character timeout, :const:`None` to disable (default). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 49 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 50 | :exception ValueError: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 51 | 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] | 52 | |
| 53 | :exception SerialException: |
| 54 | In case the device can not be found or can not be configured. |
| 55 | |
| 56 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 57 | The port is immediately opened on object creation, when a *port* is |
| 58 | given. It is not opened when *port* is :const:`None` and a successive call |
| 59 | to :meth:`open` will be needed. |
| 60 | |
| 61 | Possible values for the parameter *port*: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 62 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 63 | - Number: number of device, numbering starts at zero. |
| 64 | - Device name: depending on operating system. e.g. ``/dev/ttyUSB0`` |
| 65 | on GNU/Linux or ``COM3`` on Windows. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 66 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 67 | Possible values for the parameter *timeout*: |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 68 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 69 | - ``timeout = None``: wait forever |
| 70 | - ``timeout = 0``: non-blocking mode (return immediately on read) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 71 | - ``timeout = x``: set timeout to ``x`` seconds (float allowed) |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 72 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 73 | Note that enabling both flow control methods (*xonxoff* and *rtscts*) |
| 74 | together may not be supported. It is common to use one of the methods |
| 75 | at once, not both. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 76 | |
| 77 | .. method:: open() |
| 78 | |
| 79 | Open port. |
| 80 | |
| 81 | .. method:: close() |
| 82 | |
| 83 | Close port immediately. |
| 84 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 85 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 86 | The following methods may raise :exc:`ValueError` when applied to a closed |
| 87 | port. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 88 | |
| 89 | .. method:: read(size=1) |
| 90 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 91 | :param size: Number of bytes to read. |
| 92 | :return: Bytes read from the port. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 93 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 94 | 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] | 95 | return less characters as requested. With no timeout it will block |
| 96 | until the requested number of bytes is read. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 97 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 98 | .. versionchanged:: 2.5 |
| 99 | Returns an instance of :class:`bytes` when available (Python 2.6 |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 100 | and newer) and :class:`str` otherwise. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 101 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 102 | .. method:: write(data) |
| 103 | |
| 104 | :param data: Data to send. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 105 | :return: Number of bytes written. |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 106 | :exception SerialTimeoutException: |
| 107 | In case a write timeout is configured for the port and the time is |
| 108 | exceeded. |
| 109 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 110 | Write the string *data* to the port. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 111 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 112 | .. versionchanged:: 2.5 |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 113 | Accepts instances of :class:`bytes` and :class:`bytearray` when |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 114 | available (Python 2.6 and newer) and :class:`str` otherwise. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 115 | |
| 116 | .. method:: inWaiting() |
| 117 | |
| 118 | Return the number of chars in the receive buffer. |
| 119 | |
| 120 | .. method:: flush() |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 121 | |
| 122 | Flush of file like objects. In this case, wait until all data is |
| 123 | written. |
| 124 | |
| 125 | .. method:: flushInput() |
| 126 | |
| 127 | Flush input buffer, discarding all it's contents. |
| 128 | |
| 129 | .. method:: flushOutput() |
| 130 | |
| 131 | Clear output buffer, aborting the current output and |
| 132 | discarding all that is in the buffer. |
| 133 | |
| 134 | .. method:: sendBreak(duration=0.25) |
| 135 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 136 | :param duration: Time (float) to activate the BREAK condition. |
| 137 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 138 | Send break condition. Timed, returns to idle state after given |
| 139 | duration. |
| 140 | |
| 141 | .. method:: setBreak(level=True) |
| 142 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 143 | :param level: when true activate BREAK condition, else disable. |
| 144 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 145 | Set break: Controls TXD. When active, no transmitting is possible. |
| 146 | |
| 147 | .. method:: setRTS(level=True) |
| 148 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 149 | :param level: Set control line to logic level. |
| 150 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 151 | Set RTS line to specified logic level. |
| 152 | |
| 153 | .. method:: setDTR(level=True) |
| 154 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 155 | :param level: Set control line to logic level. |
| 156 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 157 | Set DTR line to specified logic level. |
| 158 | |
| 159 | .. method:: getCTS() |
| 160 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 161 | :return: Current state (boolean) |
| 162 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 163 | Return the state of the CTS line. |
| 164 | |
| 165 | .. method:: getDSR() |
| 166 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 167 | :return: Current state (boolean) |
| 168 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 169 | Return the state of the DSR line. |
| 170 | |
| 171 | .. method:: getRI() |
| 172 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 173 | :return: Current state (boolean) |
| 174 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 175 | Return the state of the RI line. |
| 176 | |
| 177 | .. method:: getCD() |
| 178 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 179 | :return: Current state (boolean) |
| 180 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 181 | Return the state of the CD line |
| 182 | |
| 183 | Read-only attributes: |
| 184 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 185 | .. attribute:: name |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 186 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 187 | Device name. This is always the device name even if the |
| 188 | port was opened by a number. (Read Only). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 189 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 190 | .. versionadded:: 2.5 |
| 191 | |
| 192 | .. attribute:: portstr |
| 193 | |
| 194 | :deprecated: use :attr:`name` instead |
| 195 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 196 | .. attribute:: BAUDRATES |
| 197 | |
| 198 | A list of valid baud rates. The list may be incomplete such that higher |
| 199 | baud rates may be supported by the device and that values in between the |
| 200 | standard baud rates are supported. (Read Only). |
| 201 | |
| 202 | .. attribute:: BYTESIZES |
| 203 | |
| 204 | A list of valid byte sizes for the device (Read Only). |
| 205 | |
| 206 | .. attribute:: PARITIES |
| 207 | |
| 208 | A list of valid parities for the device (Read Only). |
| 209 | |
| 210 | .. attribute:: STOPBITS |
| 211 | |
| 212 | A list of valid stop bit widths for the device (Read Only). |
| 213 | |
| 214 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 215 | New values can be assigned to the following attributes, the port will be |
| 216 | reconfigured, even if it's opened at that time: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 217 | |
| 218 | .. attribute:: port |
| 219 | |
| 220 | Port name/number as set by the user. |
| 221 | |
| 222 | .. attribute:: baudrate |
| 223 | |
| 224 | Current baud rate setting. |
| 225 | |
| 226 | .. attribute:: bytesize |
| 227 | |
| 228 | Byte size in bits. |
| 229 | |
| 230 | .. attribute:: parity |
| 231 | |
| 232 | Parity setting. |
| 233 | |
| 234 | .. attribute:: stopbits |
| 235 | |
| 236 | Stop bit with. |
| 237 | |
| 238 | .. attribute:: timeout |
| 239 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 240 | Timeout setting (seconds, float). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 241 | |
| 242 | .. attribute:: xonxoff |
| 243 | |
| 244 | If Xon/Xoff flow control is enabled. |
| 245 | |
| 246 | .. attribute:: rtscts |
| 247 | |
| 248 | If hardware flow control is enabled. |
| 249 | |
| 250 | Platform specific methods. |
| 251 | |
| 252 | .. warning:: Programs using the following methods are not portable to other platforms! |
| 253 | |
| 254 | .. method:: nonblocking() |
| 255 | |
| 256 | :platform: Unix |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 257 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 258 | Configure the device for nonblocking operations. This can be useful if |
| 259 | the port is used with ``select``. |
| 260 | |
| 261 | .. method:: fileno() |
| 262 | |
| 263 | :platform: Unix |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 264 | :return: File descriptor. |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 265 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 266 | Return file descriptor number for the port that is opened by this object. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 267 | |
| 268 | .. method:: setXON(level=True) |
| 269 | |
| 270 | :platform: Windows |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame] | 271 | :param level: Set flow control state. |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 272 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 273 | Set software flow control state. |
| 274 | |
| 275 | |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 276 | .. class:: SerialBase |
| 277 | |
| 278 | The following attributes are implemented as properties. They work with open |
| 279 | and closed ports. |
| 280 | |
| 281 | .. attribute:: port |
| 282 | |
| 283 | Read or write port. When the port is already open, it will be closed |
| 284 | and reopened with the new setting. |
| 285 | |
| 286 | .. attribute:: baudrate |
| 287 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 288 | Read or write current baud rate setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 289 | |
| 290 | .. attribute:: bytesize |
| 291 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 292 | Read or write current data byte size setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 293 | |
| 294 | .. attribute:: parity |
| 295 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 296 | Read or write current parity setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 297 | |
| 298 | .. attribute:: stopbits |
| 299 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 300 | Read or write current stop bit width setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 301 | |
| 302 | .. attribute:: timeout |
| 303 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 304 | Read or write current read timeout setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 305 | |
| 306 | .. attribute:: writeTimeout |
| 307 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 308 | Read or write current write timeout setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 309 | |
| 310 | .. attribute:: xonxoff |
| 311 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 312 | Read or write current software flow control rate setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 313 | |
| 314 | .. attribute:: rtscts |
| 315 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 316 | Read or write current hardware flow control setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 317 | |
| 318 | .. attribute:: dsrdtr |
| 319 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 320 | Read or write current hardware flow control setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 321 | |
| 322 | .. attribute:: interCharTimeout |
| 323 | |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 324 | Read or write current inter character timeout setting. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 325 | |
| 326 | The following constants are also provided: |
| 327 | |
| 328 | .. attribute:: BAUDRATES |
| 329 | |
| 330 | A tuple of standard baud rate values. The actual device may support more |
| 331 | or less... |
| 332 | |
| 333 | .. attribute:: BYTESIZES |
| 334 | |
| 335 | A tuple of supported byte size values. |
| 336 | |
| 337 | .. attribute:: PARITIES |
| 338 | |
| 339 | A tuple of supported parity settings. |
| 340 | |
| 341 | .. attribute:: STOPBITS |
| 342 | |
| 343 | A tuple of supported stop bit settings. |
| 344 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 345 | .. method:: readline(size=None, eol='\\n') |
| 346 | |
| 347 | :param size: Max number of bytes to read, ``None`` -> no limit. |
| 348 | :param eol: The end of line character. |
| 349 | |
| 350 | Read a line which is terminated with end-of-line (*eol*) character |
| 351 | (``\\n`` by default) or until timeout. |
| 352 | |
| 353 | .. method:: readlines(sizehint=None, eol='\\n') |
| 354 | |
| 355 | :param size: Ignored parameter. |
| 356 | :param eol: The end of line character. |
| 357 | |
| 358 | Read a list of lines, until timeout. *sizehint* is ignored and only |
| 359 | present for API compatibility with built-in File objects. |
| 360 | |
| 361 | .. method:: xreadlines(sizehint=None) |
| 362 | |
| 363 | Just calls :meth:`readlines` - here for compatibility. |
| 364 | |
| 365 | For compatibility with the :mod:`io` library are the following methods. |
| 366 | |
| 367 | .. method:: readable() |
| 368 | |
| 369 | :return: True |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 370 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 371 | .. versionadded:: 2.5 |
| 372 | |
| 373 | .. method:: writable() |
| 374 | |
| 375 | :return: True |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 376 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 377 | .. versionadded:: 2.5 |
| 378 | |
| 379 | .. method:: seekable() |
| 380 | |
| 381 | :return: False |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 382 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 383 | .. versionadded:: 2.5 |
| 384 | |
| 385 | .. method:: readinto(b) |
| 386 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 387 | :param b: bytearray or array instance |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 388 | :return: Number of byte read |
| 389 | |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 390 | Read up to len(b) bytes into :class:`bytearray` *b* and return the |
| 391 | number of bytes read. |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 392 | |
| 393 | .. versionadded:: 2.5 |
| 394 | |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 395 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 396 | .. note:: |
| 397 | |
| 398 | For systems that provide the :mod:`io` library (Python 2.6 and newer), the |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 399 | class :class:`Serial` will derive from :class:`io.RawIOBase`. For all |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 400 | others from :class:`FileLike`. |
| 401 | |
| 402 | .. class:: FileLike |
| 403 | |
| 404 | An abstract file like class. It is used as base class for :class:`Serial`. |
| 405 | |
| 406 | This class implements :meth:`readline` and :meth:`readlines` based on read |
| 407 | and :meth:`writelines` based on write. This class is used to provide the |
| 408 | above functions for to Serial port objects. |
| 409 | |
| 410 | Note that when the serial port was opened with no timeout that |
| 411 | :meth:`readline` blocks until it sees a newline (or the specified size is |
| 412 | reached) and that :meth:`readlines` would never return and therefore |
| 413 | refuses to work (it raises an exception in this case)! |
| 414 | |
| 415 | .. method:: writelines(sequence) |
| 416 | |
| 417 | Write a list of strings to the port. |
| 418 | |
| 419 | |
| 420 | The following three methods are overridden in :class:`Serial`. |
| 421 | |
| 422 | .. method:: flush() |
| 423 | |
| 424 | Flush of file like objects. It's a no-op in this class, may be overridden. |
| 425 | |
| 426 | .. method:: read() |
| 427 | |
| 428 | Raises NotImplementedError, needs to be overridden by subclass. |
| 429 | |
| 430 | .. method:: write(data) |
| 431 | |
| 432 | Raises NotImplementedError, needs to be overridden by subclass. |
| 433 | |
| 434 | The following functions are implemented for compatibility with other |
| 435 | file-like objects, however serial ports are not seekable. |
| 436 | |
| 437 | |
| 438 | .. method:: seek(pos, whence=0) |
| 439 | |
| 440 | :exception IOError: always, as method is not supported on serial port |
| 441 | |
| 442 | .. versionadded:: 2.5 |
| 443 | |
| 444 | .. method:: tell() |
| 445 | |
| 446 | :exception IOError: always, as method is not supported on serial port |
| 447 | |
| 448 | .. versionadded:: 2.5 |
| 449 | |
| 450 | .. method:: truncate(self, n=None) |
| 451 | |
| 452 | :exception IOError: always, as method is not supported on serial port |
| 453 | |
| 454 | .. versionadded:: 2.5 |
| 455 | |
| 456 | .. method:: isatty() |
| 457 | |
| 458 | :exception IOError: always, as method is not supported on serial port |
| 459 | |
| 460 | .. versionadded:: 2.5 |
| 461 | |
| 462 | To be able to use the file like object as iterator for e.g. |
| 463 | ``for line in Serial(0): ...`` usage: |
| 464 | |
| 465 | .. method:: next() |
| 466 | |
| 467 | Return the next line by calling :meth:`readline`. |
| 468 | |
| 469 | .. method:: __iter__() |
| 470 | |
| 471 | Returns self. |
| 472 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 473 | :rfc:`2217` Network ports |
| 474 | ------------------------- |
| 475 | |
| 476 | .. warning:: This implementation is currently in an experimental state. Use |
| 477 | at your own risk. |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 478 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 479 | .. class:: rfc2217.Serial |
| 480 | |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 481 | This implements a :rfc:`2217` compatible client. Port names are URLs_ in the |
| 482 | form: ``rfc2217://<host>:<port>[/<option>[/<option>]]`` |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 483 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 484 | This class API is compatible to :class:`Serial` with a few exceptions: |
| 485 | |
| 486 | - numbers as port name are not allowed, only URLs in the form described |
| 487 | above. |
| 488 | - writeTimeout is not implemented |
| 489 | - The current implementation starts a thread that keeps reading from the |
| 490 | (internal) socket. The thread is managed automatically by the |
| 491 | :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`. |
| 492 | However it may be a problem for user applications that like to use select |
| 493 | instead of threads. |
| 494 | |
| 495 | Due to the nature of the network and protocol involved there are a few |
| 496 | extra points to keep in mind: |
| 497 | |
| 498 | - All operations have an additional latency time. |
| 499 | - Setting control lines (RTS/CTS) needs more time. |
| 500 | - Reading the status lines (DSR/DTR etc.) returns a cached value. When that |
| 501 | cache is updated depends entirely on the server. The server itself may |
| 502 | implement a polling at a certain rate and quick changes may be invisible. |
| 503 | - The network layer also has buffers. This means that :meth:`flush`, |
| 504 | :meth:`flushInput` and :meth:`flushOutput` may work with additional delay. |
| 505 | Likewise :meth:`inWaiting` returns the size of the data arrived at the |
| 506 | object internal buffer and excludes any bytes in the network buffers or |
| 507 | any server side buffer. |
| 508 | - Closing and immediately reopening the same port may fail due to time |
| 509 | needed by the server to get ready again. |
| 510 | |
| 511 | Not implemented yet / Possible problems with the implementation: |
| 512 | |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 513 | - :rfc:`2217` flow control between client and server (objects internal |
| 514 | buffer may eat all your memory when never read). |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 515 | - No authentication support (servers may not prompt for a password etc.) |
| 516 | - No encryption. |
| 517 | |
| 518 | Due to lack of authentication and encryption it is not suitable to use this |
| 519 | client for connections across the internet and should only be used in |
| 520 | controlled environments. |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 521 | |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 522 | .. versionadded:: 2.5 |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 523 | |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 524 | |
| 525 | .. class:: rfc2217.PortManager |
| 526 | |
| 527 | This class provides helper functions for implementing :rfc:`2217` |
| 528 | compatible servers. |
| 529 | |
| 530 | Basically, it implements every thing needed for the :rfc:`2217` protocol. |
| 531 | It just does not open sockets and read/write to serial ports (though it |
| 532 | changes other port settings). The user of this class must take care of the |
| 533 | data transmission itself. The reason for that is, that this way, this class |
| 534 | supports all programming models such as threads and select. |
| 535 | |
| 536 | Usage examples can be found in the examples where two TCP/IP - serial |
| 537 | converters are shown, one using threads (the single port server) and an |
| 538 | other using select (the multi port server). |
| 539 | |
| 540 | .. note:: Each new client connection must create a new instance as this |
| 541 | object (and the :rfc:`2217` protocol) has internal state. |
| 542 | |
| 543 | .. method:: __init__(serial_port, connection, debug_output=False) |
| 544 | |
| 545 | :param serial_port: a :class:`Serial` instance that is managed. |
| 546 | :param connection: an object implementing :meth:`write`, used to write |
| 547 | to the network. |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 548 | :param debug_output: used for development please set for False |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 549 | |
| 550 | Initializes the Manager and starts negotiating with client in Telnet |
| 551 | and :rfc:`2217` protocol. The negotiation starts immediately so that |
| 552 | the class should be instantiated in the moment the client connects. |
| 553 | |
| 554 | The *serial_port* can be controlled by :rfc:`2217` commands. This |
| 555 | object will modify the port settings (baud rate etc) and control lines |
| 556 | (RTS/DTR) send BREAK etc. when the corresponding commands are found by |
| 557 | the :meth:`filter` method. |
| 558 | |
| 559 | The *connection* object must implement a :meth:`write(data)` function. |
| 560 | This function must ensure that *data* is written at once (no user data |
| 561 | mixed in, i.e. it must be thread-safe). All data must be sent in its |
| 562 | raw form (:meth:`escape` must not be used) as it is used to send Telnet |
| 563 | and :rfc:`2217` control commands. |
| 564 | |
| 565 | .. method:: escape(data) |
| 566 | |
| 567 | :param data: data to be sent over the network. |
| 568 | :return: data, escaped for Telnet/:rfc:`2217` |
| 569 | |
| 570 | A generator that escapes all data to be compatible with :rfc:`2217`. |
| 571 | Implementors of servers should use this function to process all data |
| 572 | sent over the network. |
| 573 | |
| 574 | The function returns a generator which can be used in ``for`` loops. |
| 575 | It can be converted to bytes using ``serial.to_bytes``. |
| 576 | |
| 577 | .. method:: filter(data) |
| 578 | |
| 579 | :param data: data read from the network, including Telnet and |
| 580 | :rfc:`2217` controls. |
| 581 | :return: data, free from Telnet and :rfc:`2217` controls. |
| 582 | |
| 583 | A generator that filters and processes all data related to :rfc:`2217`. |
| 584 | Implementors of servers should use this function to process all data |
| 585 | received from the network. |
| 586 | |
| 587 | The function returns a generator which can be used in ``for`` loops. |
| 588 | It can be converted to bytes using ``serial.to_bytes``. |
| 589 | |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 590 | .. method:: check_modem_lines(force_notification=False) |
| 591 | |
| 592 | :param force_notification: Set to false. Parameter is for internal use. |
cliechti | 7aed833 | 2009-08-05 14:19:31 +0000 | [diff] [blame] | 593 | |
| 594 | This function needs to be called periodically (e.g. every second) when |
| 595 | the server wants to send NOTIFY_MODEMSTATE messages. This is required |
| 596 | to support the client for reading CTS/DSR/RI/CD status lines. |
| 597 | |
| 598 | The function reads the status line and issues the notifications |
| 599 | automatically. |
| 600 | |
| 601 | .. versionadded:: 2.5 |
| 602 | |
cliechti | ec4ac1b | 2009-08-02 00:47:21 +0000 | [diff] [blame] | 603 | .. seealso:: |
| 604 | |
| 605 | :rfc:`2217` - Telnet Com Port Control Option |
| 606 | |
| 607 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 608 | Exceptions |
| 609 | ========== |
| 610 | |
| 611 | .. exception:: SerialException |
| 612 | |
| 613 | Base class for serial port exceptions. |
| 614 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 615 | .. versionchanged:: 2.5 |
| 616 | Now derrives from :exc:`IOError` instead of :exc:`Exception` |
| 617 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 618 | .. exception:: SerialTimeoutException |
| 619 | |
| 620 | Exception that is raised on write timeouts. |
| 621 | |
| 622 | |
| 623 | Constants |
| 624 | ========= |
| 625 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 626 | Parity |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 627 | ------ |
| 628 | .. data:: PARITY_NONE |
| 629 | .. data:: PARITY_EVEN |
| 630 | .. data:: PARITY_ODD |
| 631 | .. data:: PARITY_MARK |
| 632 | .. data:: PARITY_SPACE |
| 633 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 634 | Stop bits |
| 635 | --------- |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 636 | .. data:: STOPBITS_ONE |
| 637 | .. data:: STOPBITS_ONE_POINT_FIVE |
| 638 | .. data:: STOPBITS_TWO |
| 639 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 640 | Byte size |
| 641 | --------- |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 642 | .. data:: FIVEBITS |
| 643 | .. data:: SIXBITS |
| 644 | .. data:: SEVENBITS |
| 645 | .. data:: EIGHTBITS |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 646 | |
| 647 | Others |
| 648 | ------- |
cliechti | 8611bf4 | 2009-08-03 00:34:03 +0000 | [diff] [blame] | 649 | Default control characters (instances of :class:`bytes` for Python 3.0+) for |
cliechti | daf47ba | 2009-07-28 01:28:16 +0000 | [diff] [blame] | 650 | software flow control: |
cliechti | 6066f84 | 2009-07-24 00:05:45 +0000 | [diff] [blame] | 651 | |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 652 | .. data:: XON |
| 653 | .. data:: XOFF |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 654 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 655 | Module version: |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 656 | |
| 657 | .. data:: VERSION |
| 658 | |
| 659 | A string indicating the pySerial version, such as ``2.5``. |
| 660 | |
| 661 | Functions: |
| 662 | |
| 663 | .. function:: device(number) |
| 664 | |
| 665 | :param number: Port number. |
| 666 | :return: String containing device name. |
| 667 | :deprecated: Use device names directly. |
| 668 | |
| 669 | Convert a port number to a platform dependent device name. Unfortunately |
| 670 | this does not work well for all platforms; e.g. some may miss USB-Serial |
| 671 | converters and enumerate only internal serial ports. |
| 672 | |
| 673 | The conversion may be made off-line, that is, there is no guarantee that |
| 674 | the returned device name really exists on the system. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 675 | |
| 676 | |
cliechti | e3ab353 | 2009-08-05 12:40:38 +0000 | [diff] [blame] | 677 | .. function:: serial_for_url(url, \*args, \*\*kwargs) |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 678 | |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 679 | :param url: Device name, number or URL_ |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 680 | :param do_not_open: When set to true, the serial port is not opened. |
| 681 | :return: an instance of :class:`Serial` or a compatible object. |
| 682 | |
| 683 | Get a native or a :rfc:`2217` implementation of the Serial class, depending |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 684 | on port/url. This factory function is useful when an application wants |
| 685 | to support both, local ports and remote ports. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 686 | |
| 687 | When *url* matches the form ``rfc2217://<host>:<port>`` an instance of |
| 688 | :class:`rfc2217.Serial` is returned. In all other cases the native (system |
| 689 | dependant) :class:`Serial` instance is returned. |
| 690 | |
cliechti | d7e7ce2 | 2009-08-03 02:01:07 +0000 | [diff] [blame] | 691 | The port is not opened when a keyword parameter called *do_not_open* is |
| 692 | given and true, by default it is opened. |
cliechti | 7c640ed | 2009-08-02 00:54:51 +0000 | [diff] [blame] | 693 | |
| 694 | .. versionadded:: 2.5 |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 695 | |
| 696 | .. _URL: URLs_ |
| 697 | |
| 698 | URLs |
| 699 | ---- |
cliechti | e3ab353 | 2009-08-05 12:40:38 +0000 | [diff] [blame] | 700 | The class :class:`rfc2217.Serial` and the function :func:`serial_for_url` |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame^] | 701 | accept the following types URL: |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 702 | |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame^] | 703 | - ``rfc2217://<host>:<port>[/<option>[/<option>]]`` |
| 704 | - ``socket://<host>:<port>[/<option>[/<option>]]`` |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 705 | |
| 706 | (Future releases of pySerial might add more types). |
| 707 | |
cliechti | ab90e07 | 2009-08-06 01:44:34 +0000 | [diff] [blame^] | 708 | ``rfc2217://`` |
| 709 | Used to connect to :rfc:`2217` compatible servers. All serial port |
| 710 | functions are supported. |
| 711 | |
| 712 | Supported options in the URL are: |
| 713 | |
| 714 | - ``ign_set_control`` does not wait for acknowledges to SET_CONTROL. This |
| 715 | option can be used for non compliant servers (i.e. when getting an |
| 716 | ``remote rejected value for option 'control'`` error when connecting). |
| 717 | |
| 718 | - ``poll_modem``: The client issues NOTIFY_MODEMSTATE requests when status |
| 719 | lines are read (CTS/DTR/RI/CD). Without this option it relies on the server |
| 720 | sending the notifications automatically (that's what the RFC suggests and |
| 721 | most servers do). Enable this option when :meth:`getCTS` does not work as |
| 722 | expected, i.e. for servers that do not send notifications. |
| 723 | |
| 724 | - ``debug``: Prints diagnostic messages (not useful for end users). |
| 725 | |
| 726 | ``socket://`` |
| 727 | The purpose of this connection type is that applications using pySerial can |
| 728 | connect to TCP/IP to serial port converters that do not support :rfc:`2217`. |
| 729 | |
| 730 | Uses a TCP/IP socket. All serial port settings, control and status lines |
| 731 | are ignored. Only data is transmitted and received. |
| 732 | |
| 733 | Supported options in the URL are: |
| 734 | |
| 735 | - ``debug``: Prints diagnostic messages (not useful for end users). |
| 736 | |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 737 | Examples:: |
| 738 | |
| 739 | rfc2217://localhost:7000 |
cliechti | 7cb78e8 | 2009-08-05 15:47:57 +0000 | [diff] [blame] | 740 | rfc2217://localhost:7000/poll_modem |
cliechti | f456634 | 2009-08-04 00:07:19 +0000 | [diff] [blame] | 741 | rfc2217://localhost:7000/ign_set_control/debug |