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 | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 48 | Possible values for the parameter ``timeout``:: |
| 49 | |
| 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 | |
| 73 | Read size bytes from the serial port. If a timeout is set it may return |
| 74 | less characters as requested. With no timeout it will block until the |
| 75 | requested number of bytes is read. |
| 76 | |
| 77 | .. method:: write(s) |
| 78 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 79 | Write the string `s` to the port. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 80 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 81 | .. method:: flush(): |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 82 | |
| 83 | Flush of file like objects. In this case, wait until all data is |
| 84 | written. |
| 85 | |
| 86 | .. method:: flushInput() |
| 87 | |
| 88 | Flush input buffer, discarding all it's contents. |
| 89 | |
| 90 | .. method:: flushOutput() |
| 91 | |
| 92 | Clear output buffer, aborting the current output and |
| 93 | discarding all that is in the buffer. |
| 94 | |
| 95 | .. method:: sendBreak(duration=0.25) |
| 96 | |
| 97 | Send break condition. Timed, returns to idle state after given |
| 98 | duration. |
| 99 | |
| 100 | .. method:: setBreak(level=True) |
| 101 | |
| 102 | Set break: Controls TXD. When active, no transmitting is possible. |
| 103 | |
| 104 | .. method:: setRTS(level=True) |
| 105 | |
| 106 | Set RTS line to specified logic level. |
| 107 | |
| 108 | .. method:: setDTR(level=True) |
| 109 | |
| 110 | Set DTR line to specified logic level. |
| 111 | |
| 112 | .. method:: getCTS() |
| 113 | |
| 114 | Return the state of the CTS line. |
| 115 | |
| 116 | .. method:: getDSR() |
| 117 | |
| 118 | Return the state of the DSR line. |
| 119 | |
| 120 | .. method:: getRI() |
| 121 | |
| 122 | Return the state of the RI line. |
| 123 | |
| 124 | .. method:: getCD() |
| 125 | |
| 126 | Return the state of the CD line |
| 127 | |
| 128 | Read-only attributes: |
| 129 | |
| 130 | .. attribute:: portstr |
| 131 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 132 | Device name. This is always the device name even if the |
| 133 | port was opened by a number. (Read Only). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 134 | |
| 135 | .. attribute:: BAUDRATES |
| 136 | |
| 137 | A list of valid baud rates. The list may be incomplete such that higher |
| 138 | baud rates may be supported by the device and that values in between the |
| 139 | standard baud rates are supported. (Read Only). |
| 140 | |
| 141 | .. attribute:: BYTESIZES |
| 142 | |
| 143 | A list of valid byte sizes for the device (Read Only). |
| 144 | |
| 145 | .. attribute:: PARITIES |
| 146 | |
| 147 | A list of valid parities for the device (Read Only). |
| 148 | |
| 149 | .. attribute:: STOPBITS |
| 150 | |
| 151 | A list of valid stop bit widths for the device (Read Only). |
| 152 | |
| 153 | |
| 154 | New values can be assigned to the following attributes, the port will be reconfigured, even if it's opened at that time: |
| 155 | |
| 156 | .. attribute:: port |
| 157 | |
| 158 | Port name/number as set by the user. |
| 159 | |
| 160 | .. attribute:: baudrate |
| 161 | |
| 162 | Current baud rate setting. |
| 163 | |
| 164 | .. attribute:: bytesize |
| 165 | |
| 166 | Byte size in bits. |
| 167 | |
| 168 | .. attribute:: parity |
| 169 | |
| 170 | Parity setting. |
| 171 | |
| 172 | .. attribute:: stopbits |
| 173 | |
| 174 | Stop bit with. |
| 175 | |
| 176 | .. attribute:: timeout |
| 177 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 178 | Timeout setting (seconds, float). |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 179 | |
| 180 | .. attribute:: xonxoff |
| 181 | |
| 182 | If Xon/Xoff flow control is enabled. |
| 183 | |
| 184 | .. attribute:: rtscts |
| 185 | |
| 186 | If hardware flow control is enabled. |
| 187 | |
| 188 | Platform specific methods. |
| 189 | |
| 190 | .. warning:: Programs using the following methods are not portable to other platforms! |
| 191 | |
| 192 | .. method:: nonblocking() |
| 193 | |
| 194 | :platform: Unix |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 195 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 196 | Configure the device for nonblocking operations. This can be useful if |
| 197 | the port is used with ``select``. |
| 198 | |
| 199 | .. method:: fileno() |
| 200 | |
| 201 | :platform: Unix |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 202 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 203 | Return file descriptor number. |
| 204 | |
| 205 | |
| 206 | .. method:: setXON(level=True) |
| 207 | |
| 208 | :platform: Windows |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame] | 209 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 210 | Set software flow control state. |
| 211 | |
| 212 | |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 213 | .. class:: FileLike |
| 214 | |
| 215 | .. method:: readline() |
| 216 | |
| 217 | An abstract file like class. It is used as base class for :class:`Serial`. |
| 218 | |
| 219 | This class implements readline and readlines based on read and |
| 220 | writelines based on write. |
| 221 | This class is used to provide the above functions for to Serial |
| 222 | port objects. |
| 223 | |
| 224 | Note that when the serial port was opened with _NO_ timeout that |
| 225 | readline blocks until it sees a newline (or the specified size is |
| 226 | reached) and that readlines would never return and therefore |
| 227 | refuses to work (it raises an exception in this case)! |
| 228 | |
| 229 | .. method:: readline(size=None, eol='\n') |
| 230 | |
| 231 | :param size: Max number of bytes to read, ``None`` -> no limit. |
| 232 | :param eol: The end of line character. |
| 233 | |
| 234 | Read a line which is terminated with end-of-line (eol) character |
| 235 | ('\n' by default) or until timeout. |
| 236 | |
| 237 | .. method:: readlines(sizehint=None, eol='\n') |
| 238 | |
| 239 | :param size: Ignored parameter. |
| 240 | :param eol: The end of line character. |
| 241 | |
| 242 | Read a list of lines, until timeout. ``sizehint`` is ignored and only |
| 243 | present for API compatibility with built-in File objects. |
| 244 | |
| 245 | .. method:: xreadlines(sizehint=None) |
| 246 | |
| 247 | Just calls ``readlines`` - here for compatibility. |
| 248 | |
| 249 | .. method:: writelines(sequence) |
| 250 | |
| 251 | Write a list of strings to the port. |
| 252 | |
| 253 | .. method:: flush() |
| 254 | |
| 255 | Flush of file like objects. It's a no-op in this class, may be overridden. |
| 256 | |
| 257 | .. method:: read() |
| 258 | |
| 259 | Raises NotImplementedError, needs to be overridden by subclass. |
| 260 | |
| 261 | .. method:: write() |
| 262 | |
| 263 | Raises NotImplementedError, needs to be overridden by subclass. |
| 264 | |
| 265 | |
| 266 | To be able to use the file like object as iterator for e.g. |
| 267 | ``for line in Serial(0): ...`` usage: |
| 268 | |
| 269 | .. method:: next() |
| 270 | |
| 271 | Return the next line by calling :meth:`readline`. |
| 272 | |
| 273 | .. method:: __iter__() |
| 274 | |
| 275 | Returns self. |
| 276 | |
| 277 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 278 | Exceptions |
| 279 | ========== |
| 280 | |
| 281 | .. exception:: SerialException |
| 282 | |
| 283 | Base class for serial port exceptions. |
| 284 | |
| 285 | .. exception:: SerialTimeoutException |
| 286 | |
| 287 | Exception that is raised on write timeouts. |
| 288 | |
| 289 | |
| 290 | Constants |
| 291 | ========= |
| 292 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 293 | Parity |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 294 | ------ |
| 295 | .. data:: PARITY_NONE |
| 296 | .. data:: PARITY_EVEN |
| 297 | .. data:: PARITY_ODD |
| 298 | .. data:: PARITY_MARK |
| 299 | .. data:: PARITY_SPACE |
| 300 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 301 | Stopbits |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 302 | -------- |
| 303 | .. data:: STOPBITS_ONE |
| 304 | .. data:: STOPBITS_ONE_POINT_FIVE |
| 305 | .. data:: STOPBITS_TWO |
| 306 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 307 | Bytesize |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 308 | -------- |
| 309 | .. data:: FIVEBITS |
| 310 | .. data:: SIXBITS |
| 311 | .. data:: SEVENBITS |
| 312 | .. data:: EIGHTBITS |
cliechti | 5b7d16a | 2009-07-21 21:53:59 +0000 | [diff] [blame] | 313 | |
| 314 | Others |
| 315 | ------- |
| 316 | .. data:: XON |
| 317 | .. data:: XOFF |