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: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 38 | Enable software flow control. |
| 39 | |
| 40 | :param rtscts: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 41 | Enable hardware (RTS/CTS) flow control. |
| 42 | |
| 43 | :param interCharTimeout: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 44 | Inter-character timeout, None to disable. |
| 45 | |
| 46 | The port is immediately opened on object creation, when a port is given. It |
| 47 | is not opened when port is None. |
| 48 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame^] | 49 | Possible values for the parameter ``timeout``:: |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 50 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame^] | 51 | timeout = None # wait forever |
| 52 | timeout = 0 # non-blocking mode (return immediately on read) |
| 53 | timeout = x # set timeout to x seconds (float allowed) |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | .. method:: open() |
| 57 | |
| 58 | Open port. |
| 59 | |
| 60 | .. method:: close() |
| 61 | |
| 62 | Close port immediately. |
| 63 | |
| 64 | .. method:: setBaudrate(baudrate) |
| 65 | |
| 66 | Change baud rate on an open port. |
| 67 | |
| 68 | .. method:: inWaiting() |
| 69 | |
| 70 | Return the number of chars in the receive buffer. |
| 71 | |
| 72 | .. method:: read(size=1) |
| 73 | |
| 74 | Read size bytes from the serial port. If a timeout is set it may return |
| 75 | less characters as requested. With no timeout it will block until the |
| 76 | requested number of bytes is read. |
| 77 | |
| 78 | .. method:: write(s) |
| 79 | |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame^] | 80 | Write the string `s` to the port. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 81 | |
| 82 | .. method:: flush(self): |
| 83 | |
| 84 | Flush of file like objects. In this case, wait until all data is |
| 85 | written. |
| 86 | |
| 87 | .. method:: flushInput() |
| 88 | |
| 89 | Flush input buffer, discarding all it's contents. |
| 90 | |
| 91 | .. method:: flushOutput() |
| 92 | |
| 93 | Clear output buffer, aborting the current output and |
| 94 | discarding all that is in the buffer. |
| 95 | |
| 96 | .. method:: sendBreak(duration=0.25) |
| 97 | |
| 98 | Send break condition. Timed, returns to idle state after given |
| 99 | duration. |
| 100 | |
| 101 | .. method:: setBreak(level=True) |
| 102 | |
| 103 | Set break: Controls TXD. When active, no transmitting is possible. |
| 104 | |
| 105 | .. method:: setRTS(level=True) |
| 106 | |
| 107 | Set RTS line to specified logic level. |
| 108 | |
| 109 | .. method:: setDTR(level=True) |
| 110 | |
| 111 | Set DTR line to specified logic level. |
| 112 | |
| 113 | .. method:: getCTS() |
| 114 | |
| 115 | Return the state of the CTS line. |
| 116 | |
| 117 | .. method:: getDSR() |
| 118 | |
| 119 | Return the state of the DSR line. |
| 120 | |
| 121 | .. method:: getRI() |
| 122 | |
| 123 | Return the state of the RI line. |
| 124 | |
| 125 | .. method:: getCD() |
| 126 | |
| 127 | Return the state of the CD line |
| 128 | |
| 129 | Read-only attributes: |
| 130 | |
| 131 | .. attribute:: portstr |
| 132 | |
| 133 | Device name (Read Only). This is always the device name even if the |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame^] | 134 | port was opened by a number. |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 135 | |
| 136 | .. attribute:: BAUDRATES |
| 137 | |
| 138 | A list of valid baud rates. The list may be incomplete such that higher |
| 139 | baud rates may be supported by the device and that values in between the |
| 140 | standard baud rates are supported. (Read Only). |
| 141 | |
| 142 | .. attribute:: BYTESIZES |
| 143 | |
| 144 | A list of valid byte sizes for the device (Read Only). |
| 145 | |
| 146 | .. attribute:: PARITIES |
| 147 | |
| 148 | A list of valid parities for the device (Read Only). |
| 149 | |
| 150 | .. attribute:: STOPBITS |
| 151 | |
| 152 | A list of valid stop bit widths for the device (Read Only). |
| 153 | |
| 154 | |
| 155 | New values can be assigned to the following attributes, the port will be reconfigured, even if it's opened at that time: |
| 156 | |
| 157 | .. attribute:: port |
| 158 | |
| 159 | Port name/number as set by the user. |
| 160 | |
| 161 | .. attribute:: baudrate |
| 162 | |
| 163 | Current baud rate setting. |
| 164 | |
| 165 | .. attribute:: bytesize |
| 166 | |
| 167 | Byte size in bits. |
| 168 | |
| 169 | .. attribute:: parity |
| 170 | |
| 171 | Parity setting. |
| 172 | |
| 173 | .. attribute:: stopbits |
| 174 | |
| 175 | Stop bit with. |
| 176 | |
| 177 | .. attribute:: timeout |
| 178 | |
| 179 | Timeout setting (seconds). |
| 180 | |
| 181 | .. attribute:: xonxoff |
| 182 | |
| 183 | If Xon/Xoff flow control is enabled. |
| 184 | |
| 185 | .. attribute:: rtscts |
| 186 | |
| 187 | If hardware flow control is enabled. |
| 188 | |
| 189 | Platform specific methods. |
| 190 | |
| 191 | .. warning:: Programs using the following methods are not portable to other platforms! |
| 192 | |
| 193 | .. method:: nonblocking() |
| 194 | |
| 195 | :platform: Unix |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame^] | 196 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 197 | Configure the device for nonblocking operations. This can be useful if |
| 198 | the port is used with ``select``. |
| 199 | |
| 200 | .. method:: fileno() |
| 201 | |
| 202 | :platform: Unix |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame^] | 203 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 204 | Return file descriptor number. |
| 205 | |
| 206 | |
| 207 | .. method:: setXON(level=True) |
| 208 | |
| 209 | :platform: Windows |
cliechti | 86e8787 | 2009-07-21 13:32:45 +0000 | [diff] [blame^] | 210 | |
cliechti | c1c3760 | 2009-07-21 01:34:57 +0000 | [diff] [blame] | 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 |