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: |
| 15 | |
| 16 | Device name or port number number or None. |
| 17 | |
| 18 | Number: number of device, numbering starts at zero |
| 19 | Device name: depending on operating system. e.g. ``/dev/ttyUSB0`` |
| 20 | on GNU/Linux or ``COM3`` on Windows. |
| 21 | |
| 22 | :param baudrate: |
| 23 | Baud rate such as 9600 or 115200 etc. |
| 24 | |
| 25 | :param bytesize: |
| 26 | Number of data bits. Possible values: FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS |
| 27 | |
| 28 | :param parity: |
| 29 | Enable parity checking. Possible values: PARITY_NONE PARITY_EVEN PARITY_ODD PARITY_MARK PARITY_SPACE |
| 30 | |
| 31 | :param stopbits: |
| 32 | Number of stop bits. Possible values: STOPBITS_ONE STOPBITS_ONE_POINT_FIVE STOPBITS_TWO |
| 33 | |
| 34 | :param timeout: |
| 35 | Set a read timeout value. |
| 36 | |
| 37 | :param xonxoff: |
| 38 | |
| 39 | Enable software flow control. |
| 40 | |
| 41 | :param rtscts: |
| 42 | |
| 43 | Enable hardware (RTS/CTS) flow control. |
| 44 | |
| 45 | :param interCharTimeout: |
| 46 | |
| 47 | Inter-character timeout, None to disable. |
| 48 | |
| 49 | The port is immediately opened on object creation, when a port is given. It |
| 50 | is not opened when port is None. |
| 51 | |
| 52 | Possible values for :param:`timeout`:: |
| 53 | |
| 54 | timeout=None # wait forever |
| 55 | timeout=0 # non-blocking mode (return immediately on read) |
| 56 | timeout=x # set timeout to x seconds (float allowed) |
| 57 | |
| 58 | |
| 59 | .. method:: open() |
| 60 | |
| 61 | Open port. |
| 62 | |
| 63 | .. method:: close() |
| 64 | |
| 65 | Close port immediately. |
| 66 | |
| 67 | .. method:: setBaudrate(baudrate) |
| 68 | |
| 69 | Change baud rate on an open port. |
| 70 | |
| 71 | .. method:: inWaiting() |
| 72 | |
| 73 | Return the number of chars in the receive buffer. |
| 74 | |
| 75 | .. method:: read(size=1) |
| 76 | |
| 77 | Read size bytes from the serial port. If a timeout is set it may return |
| 78 | less characters as requested. With no timeout it will block until the |
| 79 | requested number of bytes is read. |
| 80 | |
| 81 | .. method:: write(s) |
| 82 | |
| 83 | Write the string :param:`s` to the port. |
| 84 | |
| 85 | .. method:: flush(self): |
| 86 | |
| 87 | Flush of file like objects. In this case, wait until all data is |
| 88 | written. |
| 89 | |
| 90 | .. method:: flushInput() |
| 91 | |
| 92 | Flush input buffer, discarding all it's contents. |
| 93 | |
| 94 | .. method:: flushOutput() |
| 95 | |
| 96 | Clear output buffer, aborting the current output and |
| 97 | discarding all that is in the buffer. |
| 98 | |
| 99 | .. method:: sendBreak(duration=0.25) |
| 100 | |
| 101 | Send break condition. Timed, returns to idle state after given |
| 102 | duration. |
| 103 | |
| 104 | .. method:: setBreak(level=True) |
| 105 | |
| 106 | Set break: Controls TXD. When active, no transmitting is possible. |
| 107 | |
| 108 | .. method:: setRTS(level=True) |
| 109 | |
| 110 | Set RTS line to specified logic level. |
| 111 | |
| 112 | .. method:: setDTR(level=True) |
| 113 | |
| 114 | Set DTR line to specified logic level. |
| 115 | |
| 116 | .. method:: getCTS() |
| 117 | |
| 118 | Return the state of the CTS line. |
| 119 | |
| 120 | .. method:: getDSR() |
| 121 | |
| 122 | Return the state of the DSR line. |
| 123 | |
| 124 | .. method:: getRI() |
| 125 | |
| 126 | Return the state of the RI line. |
| 127 | |
| 128 | .. method:: getCD() |
| 129 | |
| 130 | Return the state of the CD line |
| 131 | |
| 132 | Read-only attributes: |
| 133 | |
| 134 | .. attribute:: portstr |
| 135 | |
| 136 | Device name (Read Only). This is always the device name even if the |
| 137 | port was opened by a numeber. |
| 138 | |
| 139 | .. attribute:: BAUDRATES |
| 140 | |
| 141 | A list of valid baud rates. The list may be incomplete such that higher |
| 142 | baud rates may be supported by the device and that values in between the |
| 143 | standard baud rates are supported. (Read Only). |
| 144 | |
| 145 | .. attribute:: BYTESIZES |
| 146 | |
| 147 | A list of valid byte sizes for the device (Read Only). |
| 148 | |
| 149 | .. attribute:: PARITIES |
| 150 | |
| 151 | A list of valid parities for the device (Read Only). |
| 152 | |
| 153 | .. attribute:: STOPBITS |
| 154 | |
| 155 | A list of valid stop bit widths for the device (Read Only). |
| 156 | |
| 157 | |
| 158 | New values can be assigned to the following attributes, the port will be reconfigured, even if it's opened at that time: |
| 159 | |
| 160 | .. attribute:: port |
| 161 | |
| 162 | Port name/number as set by the user. |
| 163 | |
| 164 | .. attribute:: baudrate |
| 165 | |
| 166 | Current baud rate setting. |
| 167 | |
| 168 | .. attribute:: bytesize |
| 169 | |
| 170 | Byte size in bits. |
| 171 | |
| 172 | .. attribute:: parity |
| 173 | |
| 174 | Parity setting. |
| 175 | |
| 176 | .. attribute:: stopbits |
| 177 | |
| 178 | Stop bit with. |
| 179 | |
| 180 | .. attribute:: timeout |
| 181 | |
| 182 | Timeout setting (seconds). |
| 183 | |
| 184 | .. attribute:: xonxoff |
| 185 | |
| 186 | If Xon/Xoff flow control is enabled. |
| 187 | |
| 188 | .. attribute:: rtscts |
| 189 | |
| 190 | If hardware flow control is enabled. |
| 191 | |
| 192 | Platform specific methods. |
| 193 | |
| 194 | .. warning:: Programs using the following methods are not portable to other platforms! |
| 195 | |
| 196 | .. method:: nonblocking() |
| 197 | |
| 198 | :platform: Unix |
| 199 | Configure the device for nonblocking operations. This can be useful if |
| 200 | the port is used with ``select``. |
| 201 | |
| 202 | .. method:: fileno() |
| 203 | |
| 204 | :platform: Unix |
| 205 | Return file descriptor number. |
| 206 | |
| 207 | |
| 208 | .. method:: setXON(level=True) |
| 209 | |
| 210 | :platform: Windows |
| 211 | Set software flow control state. |
| 212 | |
| 213 | |
| 214 | Exceptions |
| 215 | ========== |
| 216 | |
| 217 | .. exception:: SerialException |
| 218 | |
| 219 | Base class for serial port exceptions. |
| 220 | |
| 221 | .. exception:: SerialTimeoutException |
| 222 | |
| 223 | Exception that is raised on write timeouts. |
| 224 | |
| 225 | |
| 226 | Constants |
| 227 | ========= |
| 228 | |
| 229 | parity |
| 230 | ------ |
| 231 | .. data:: PARITY_NONE |
| 232 | .. data:: PARITY_EVEN |
| 233 | .. data:: PARITY_ODD |
| 234 | .. data:: PARITY_MARK |
| 235 | .. data:: PARITY_SPACE |
| 236 | |
| 237 | stopbits |
| 238 | -------- |
| 239 | .. data:: STOPBITS_ONE |
| 240 | .. data:: STOPBITS_ONE_POINT_FIVE |
| 241 | .. data:: STOPBITS_TWO |
| 242 | |
| 243 | bytesize |
| 244 | -------- |
| 245 | .. data:: FIVEBITS |
| 246 | .. data:: SIXBITS |
| 247 | .. data:: SEVENBITS |
| 248 | .. data:: EIGHTBITS |