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 | |
| 10 | .. class:: Serial |
| 11 | |
| 12 | .. method:: __init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=0, rtscts=0, interCharTimeout=None) |
| 13 | |
| 14 | :param port: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 15 | Device name or port number number or None. |
| 16 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 17 | :param baudrate: |
| 18 | Baud rate such as 9600 or 115200 etc. |
| 19 | |
| 20 | :param bytesize: |
| 21 | Number of data bits. Possible values: FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS |
| 22 | |
| 23 | :param parity: |
| 24 | Enable parity checking. Possible values: PARITY_NONE PARITY_EVEN PARITY_ODD PARITY_MARK PARITY_SPACE |
| 25 | |
| 26 | :param stopbits: |
| 27 | Number of stop bits. Possible values: STOPBITS_ONE STOPBITS_ONE_POINT_FIVE STOPBITS_TWO |
| 28 | |
| 29 | :param timeout: |
| 30 | Set a read timeout value. |
| 31 | |
| 32 | :param xonxoff: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 33 | Enable software flow control. |
| 34 | |
| 35 | :param rtscts: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 36 | Enable hardware (RTS/CTS) flow control. |
| 37 | |
| 38 | :param interCharTimeout: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 39 | Inter-character timeout, None to disable. |
| 40 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 41 | The port is immediately opened on object creation, when a ``port`` is |
| 42 | given. It is not opened when port is None. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 43 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 44 | - Number: number of device, numbering starts at zero. |
| 45 | - Device name: depending on operating system. e.g. ``/dev/ttyUSB0`` |
| 46 | on GNU/Linux or ``COM3`` on Windows. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 47 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 48 | Possible values for the parameter ``timeout``: |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 49 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 50 | - ``timeout = None``: wait forever |
| 51 | - ``timeout = 0``: non-blocking mode (return immediately on read) |
| 52 | - ``timeout = x``: set timeout to x seconds (float allowed) |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | .. method:: open() |
| 56 | |
| 57 | Open port. |
| 58 | |
| 59 | .. method:: close() |
| 60 | |
| 61 | Close port immediately. |
| 62 | |
| 63 | .. method:: setBaudrate(baudrate) |
| 64 | |
| 65 | Change baud rate on an open port. |
| 66 | |
| 67 | .. method:: inWaiting() |
| 68 | |
| 69 | Return the number of chars in the receive buffer. |
| 70 | |
| 71 | .. method:: read(size=1) |
| 72 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 73 | :param size: Number of bytes to read. |
| 74 | :return: Bytes read from the port. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 75 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 76 | Read ``size`` bytes from the serial port. If a timeout is set it may |
| 77 | return less characters as requested. With no timeout it will block |
| 78 | until the requested number of bytes is read. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 79 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 80 | .. method:: write(data) |
| 81 | |
| 82 | :param data: Data to send. |
| 83 | |
| 84 | Write the string ``data`` to the port. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 85 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 86 | .. method:: flush(): |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 87 | |
| 88 | Flush of file like objects. In this case, wait until all data is |
| 89 | written. |
| 90 | |
| 91 | .. method:: flushInput() |
| 92 | |
| 93 | Flush input buffer, discarding all it's contents. |
| 94 | |
| 95 | .. method:: flushOutput() |
| 96 | |
| 97 | Clear output buffer, aborting the current output and |
| 98 | discarding all that is in the buffer. |
| 99 | |
| 100 | .. method:: sendBreak(duration=0.25) |
| 101 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 102 | :param duration: Time (float) to activate the BREAK condition. |
| 103 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 104 | Send break condition. Timed, returns to idle state after given |
| 105 | duration. |
| 106 | |
| 107 | .. method:: setBreak(level=True) |
| 108 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 109 | :param level: when true activate BREAK condition, else disable. |
| 110 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 111 | Set break: Controls TXD. When active, no transmitting is possible. |
| 112 | |
| 113 | .. method:: setRTS(level=True) |
| 114 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 115 | :param level: Set control line to logic level. |
| 116 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 117 | Set RTS line to specified logic level. |
| 118 | |
| 119 | .. method:: setDTR(level=True) |
| 120 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 121 | :param level: Set control line to logic level. |
| 122 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 123 | Set DTR line to specified logic level. |
| 124 | |
| 125 | .. method:: getCTS() |
| 126 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 127 | :return: Current state (boolean) |
| 128 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 129 | Return the state of the CTS line. |
| 130 | |
| 131 | .. method:: getDSR() |
| 132 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 133 | :return: Current state (boolean) |
| 134 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 135 | Return the state of the DSR line. |
| 136 | |
| 137 | .. method:: getRI() |
| 138 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 139 | :return: Current state (boolean) |
| 140 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 141 | Return the state of the RI line. |
| 142 | |
| 143 | .. method:: getCD() |
| 144 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 145 | :return: Current state (boolean) |
| 146 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 147 | Return the state of the CD line |
| 148 | |
| 149 | Read-only attributes: |
| 150 | |
| 151 | .. attribute:: portstr |
| 152 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 153 | Device name. This is always the device name even if the |
| 154 | port was opened by a number. (Read Only). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 155 | |
| 156 | .. attribute:: BAUDRATES |
| 157 | |
| 158 | A list of valid baud rates. The list may be incomplete such that higher |
| 159 | baud rates may be supported by the device and that values in between the |
| 160 | standard baud rates are supported. (Read Only). |
| 161 | |
| 162 | .. attribute:: BYTESIZES |
| 163 | |
| 164 | A list of valid byte sizes for the device (Read Only). |
| 165 | |
| 166 | .. attribute:: PARITIES |
| 167 | |
| 168 | A list of valid parities for the device (Read Only). |
| 169 | |
| 170 | .. attribute:: STOPBITS |
| 171 | |
| 172 | A list of valid stop bit widths for the device (Read Only). |
| 173 | |
| 174 | |
| 175 | New values can be assigned to the following attributes, the port will be reconfigured, even if it's opened at that time: |
| 176 | |
| 177 | .. attribute:: port |
| 178 | |
| 179 | Port name/number as set by the user. |
| 180 | |
| 181 | .. attribute:: baudrate |
| 182 | |
| 183 | Current baud rate setting. |
| 184 | |
| 185 | .. attribute:: bytesize |
| 186 | |
| 187 | Byte size in bits. |
| 188 | |
| 189 | .. attribute:: parity |
| 190 | |
| 191 | Parity setting. |
| 192 | |
| 193 | .. attribute:: stopbits |
| 194 | |
| 195 | Stop bit with. |
| 196 | |
| 197 | .. attribute:: timeout |
| 198 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 199 | Timeout setting (seconds, float). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 200 | |
| 201 | .. attribute:: xonxoff |
| 202 | |
| 203 | If Xon/Xoff flow control is enabled. |
| 204 | |
| 205 | .. attribute:: rtscts |
| 206 | |
| 207 | If hardware flow control is enabled. |
| 208 | |
| 209 | Platform specific methods. |
| 210 | |
| 211 | .. warning:: Programs using the following methods are not portable to other platforms! |
| 212 | |
| 213 | .. method:: nonblocking() |
| 214 | |
| 215 | :platform: Unix |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 216 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 217 | Configure the device for nonblocking operations. This can be useful if |
| 218 | the port is used with ``select``. |
| 219 | |
| 220 | .. method:: fileno() |
| 221 | |
| 222 | :platform: Unix |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 223 | :return: File descriptor. |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 224 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 225 | Return file descriptor number for the port that is opened by this object. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 226 | |
| 227 | .. method:: setXON(level=True) |
| 228 | |
| 229 | :platform: Windows |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 230 | :param level: Set flow control state. |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 231 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 232 | Set software flow control state. |
| 233 | |
| 234 | |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 235 | .. class:: FileLike |
| 236 | |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 237 | An abstract file like class. It is used as base class for :class:`Serial`. |
| 238 | |
| 239 | This class implements readline and readlines based on read and |
| 240 | writelines based on write. |
| 241 | This class is used to provide the above functions for to Serial |
| 242 | port objects. |
| 243 | |
| 244 | Note that when the serial port was opened with _NO_ timeout that |
| 245 | readline blocks until it sees a newline (or the specified size is |
| 246 | reached) and that readlines would never return and therefore |
| 247 | refuses to work (it raises an exception in this case)! |
| 248 | |
| 249 | .. method:: readline(size=None, eol='\n') |
| 250 | |
| 251 | :param size: Max number of bytes to read, ``None`` -> no limit. |
| 252 | :param eol: The end of line character. |
| 253 | |
| 254 | Read a line which is terminated with end-of-line (eol) character |
| 255 | ('\n' by default) or until timeout. |
| 256 | |
| 257 | .. method:: readlines(sizehint=None, eol='\n') |
| 258 | |
| 259 | :param size: Ignored parameter. |
| 260 | :param eol: The end of line character. |
| 261 | |
| 262 | Read a list of lines, until timeout. ``sizehint`` is ignored and only |
| 263 | present for API compatibility with built-in File objects. |
| 264 | |
| 265 | .. method:: xreadlines(sizehint=None) |
| 266 | |
| 267 | Just calls ``readlines`` - here for compatibility. |
| 268 | |
| 269 | .. method:: writelines(sequence) |
| 270 | |
| 271 | Write a list of strings to the port. |
| 272 | |
| 273 | .. method:: flush() |
| 274 | |
| 275 | Flush of file like objects. It's a no-op in this class, may be overridden. |
| 276 | |
| 277 | .. method:: read() |
| 278 | |
| 279 | Raises NotImplementedError, needs to be overridden by subclass. |
| 280 | |
cliechti | bb5c3c5 | 2009-07-23 02:29:27 +0000 | [diff] [blame^] | 281 | .. method:: write(data) |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 282 | |
| 283 | Raises NotImplementedError, needs to be overridden by subclass. |
| 284 | |
| 285 | |
| 286 | To be able to use the file like object as iterator for e.g. |
| 287 | ``for line in Serial(0): ...`` usage: |
| 288 | |
| 289 | .. method:: next() |
| 290 | |
| 291 | Return the next line by calling :meth:`readline`. |
| 292 | |
| 293 | .. method:: __iter__() |
| 294 | |
| 295 | Returns self. |
| 296 | |
| 297 | |
cliechti | edcdbe4 | 2009-07-22 00:48:34 +0000 | [diff] [blame] | 298 | .. class:: SerialBase |
| 299 | |
| 300 | The following attributes are implemented as properties. They work with open |
| 301 | and closed ports. |
| 302 | |
| 303 | .. attribute:: port |
| 304 | |
| 305 | Read or write port. When the port is already open, it will be closed |
| 306 | and reopened with the new setting. |
| 307 | |
| 308 | .. attribute:: baudrate |
| 309 | |
| 310 | Read or write current baud rate setting. It is possible to change this |
| 311 | on an opened port. |
| 312 | |
| 313 | .. attribute:: bytesize |
| 314 | |
| 315 | Read or write current data byte size setting. It is possible to change |
| 316 | this on an opened port. |
| 317 | |
| 318 | .. attribute:: parity |
| 319 | |
| 320 | Read or write current parity setting. It is possible to change this on |
| 321 | an opened port. |
| 322 | |
| 323 | .. attribute:: stopbits |
| 324 | |
| 325 | Read or write current stop bit width setting. It is possible to change |
| 326 | this on an opened port. |
| 327 | |
| 328 | .. attribute:: timeout |
| 329 | |
| 330 | Read or write current read timeout setting. It is possible to change |
| 331 | this on an opened port. |
| 332 | |
| 333 | .. attribute:: writeTimeout |
| 334 | |
| 335 | Read or write current write timeout setting. It is possible to change |
| 336 | this on an opened port. |
| 337 | |
| 338 | .. attribute:: xonxoff |
| 339 | |
| 340 | Read or write current software flow control rate setting. It is |
| 341 | possible to change this on an opened port. |
| 342 | |
| 343 | .. attribute:: rtscts |
| 344 | |
| 345 | Read or write current hardware flow control setting. It is possible to |
| 346 | change this on an opened port. |
| 347 | |
| 348 | .. attribute:: dsrdtr |
| 349 | |
| 350 | Read or write current hardware flow control setting. It is possible to |
| 351 | change this on an opened port. |
| 352 | |
| 353 | .. attribute:: interCharTimeout |
| 354 | |
| 355 | Read or write current inter character timeout setting. It is possible |
| 356 | to change this on an opened port. |
| 357 | |
| 358 | The following constants are also provided: |
| 359 | |
| 360 | .. attribute:: BAUDRATES |
| 361 | |
| 362 | A tuple of standard baud rate values. The actual device may support more |
| 363 | or less... |
| 364 | |
| 365 | .. attribute:: BYTESIZES |
| 366 | |
| 367 | A tuple of supported byte size values. |
| 368 | |
| 369 | .. attribute:: PARITIES |
| 370 | |
| 371 | A tuple of supported parity settings. |
| 372 | |
| 373 | .. attribute:: STOPBITS |
| 374 | |
| 375 | A tuple of supported stop bit settings. |
| 376 | |
| 377 | |
| 378 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 379 | Exceptions |
| 380 | ========== |
| 381 | |
| 382 | .. exception:: SerialException |
| 383 | |
| 384 | Base class for serial port exceptions. |
| 385 | |
| 386 | .. exception:: SerialTimeoutException |
| 387 | |
| 388 | Exception that is raised on write timeouts. |
| 389 | |
| 390 | |
| 391 | Constants |
| 392 | ========= |
| 393 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 394 | Parity |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 395 | ------ |
| 396 | .. data:: PARITY_NONE |
| 397 | .. data:: PARITY_EVEN |
| 398 | .. data:: PARITY_ODD |
| 399 | .. data:: PARITY_MARK |
| 400 | .. data:: PARITY_SPACE |
| 401 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 402 | Stopbits |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 403 | -------- |
| 404 | .. data:: STOPBITS_ONE |
| 405 | .. data:: STOPBITS_ONE_POINT_FIVE |
| 406 | .. data:: STOPBITS_TWO |
| 407 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 408 | Bytesize |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 409 | -------- |
| 410 | .. data:: FIVEBITS |
| 411 | .. data:: SIXBITS |
| 412 | .. data:: SEVENBITS |
| 413 | .. data:: EIGHTBITS |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 414 | |
| 415 | Others |
| 416 | ------- |
| 417 | .. data:: XON |
| 418 | .. data:: XOFF |