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