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 | |
| 213 | Exceptions |
| 214 | ========== |
| 215 | |
| 216 | .. exception:: SerialException |
| 217 | |
| 218 | Base class for serial port exceptions. |
| 219 | |
| 220 | .. exception:: SerialTimeoutException |
| 221 | |
| 222 | Exception that is raised on write timeouts. |
| 223 | |
| 224 | |
| 225 | Constants |
| 226 | ========= |
| 227 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 228 | Parity |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 229 | ------ |
| 230 | .. data:: PARITY_NONE |
| 231 | .. data:: PARITY_EVEN |
| 232 | .. data:: PARITY_ODD |
| 233 | .. data:: PARITY_MARK |
| 234 | .. data:: PARITY_SPACE |
| 235 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 236 | Stopbits |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 237 | -------- |
| 238 | .. data:: STOPBITS_ONE |
| 239 | .. data:: STOPBITS_ONE_POINT_FIVE |
| 240 | .. data:: STOPBITS_TWO |
| 241 | |
cliechti | 5134aab | 2009-07-21 19:47:59 +0000 | [diff] [blame] | 242 | Bytesize |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 243 | -------- |
| 244 | .. data:: FIVEBITS |
| 245 | .. data:: SIXBITS |
| 246 | .. data:: SEVENBITS |
| 247 | .. data:: EIGHTBITS |