blob: 622e214d6dd03576ee722241fb92f7093de69866 [file] [log] [blame]
cliechtic1c37602009-07-21 01:34:57 +00001==============
2 pySerial API
3==============
4
5.. module:: serial
6
7Classes
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:
cliechtic1c37602009-07-21 01:34:57 +000015 Device name or port number number or None.
16
cliechtic1c37602009-07-21 01:34:57 +000017 :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:
cliechtic1c37602009-07-21 01:34:57 +000033 Enable software flow control.
34
35 :param rtscts:
cliechtic1c37602009-07-21 01:34:57 +000036 Enable hardware (RTS/CTS) flow control.
37
38 :param interCharTimeout:
cliechtic1c37602009-07-21 01:34:57 +000039 Inter-character timeout, None to disable.
40
cliechti5134aab2009-07-21 19:47:59 +000041 The port is immediately opened on object creation, when a ``port`` is
42 given. It is not opened when port is None.
cliechtic1c37602009-07-21 01:34:57 +000043
cliechti5134aab2009-07-21 19:47:59 +000044 - 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.
cliechtic1c37602009-07-21 01:34:57 +000047
cliechti5134aab2009-07-21 19:47:59 +000048 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)
cliechtic1c37602009-07-21 01:34:57 +000053
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
cliechti86e87872009-07-21 13:32:45 +000079 Write the string `s` to the port.
cliechtic1c37602009-07-21 01:34:57 +000080
cliechti5134aab2009-07-21 19:47:59 +000081 .. method:: flush():
cliechtic1c37602009-07-21 01:34:57 +000082
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
cliechti5134aab2009-07-21 19:47:59 +0000132 Device name. This is always the device name even if the
133 port was opened by a number. (Read Only).
cliechtic1c37602009-07-21 01:34:57 +0000134
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
cliechti5134aab2009-07-21 19:47:59 +0000178 Timeout setting (seconds, float).
cliechtic1c37602009-07-21 01:34:57 +0000179
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
cliechti86e87872009-07-21 13:32:45 +0000195
cliechtic1c37602009-07-21 01:34:57 +0000196 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
cliechti86e87872009-07-21 13:32:45 +0000202
cliechtic1c37602009-07-21 01:34:57 +0000203 Return file descriptor number.
204
205
206 .. method:: setXON(level=True)
207
208 :platform: Windows
cliechti86e87872009-07-21 13:32:45 +0000209
cliechtic1c37602009-07-21 01:34:57 +0000210 Set software flow control state.
211
212
cliechti5b7d16a2009-07-21 21:53:59 +0000213.. 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
cliechtiedcdbe42009-07-22 00:48:34 +0000278.. class:: SerialBase
279
280 The following attributes are implemented as properties. They work with open
281 and closed ports.
282
283 .. attribute:: port
284
285 Read or write port. When the port is already open, it will be closed
286 and reopened with the new setting.
287
288 .. attribute:: baudrate
289
290 Read or write current baud rate setting. It is possible to change this
291 on an opened port.
292
293 .. attribute:: bytesize
294
295 Read or write current data byte size setting. It is possible to change
296 this on an opened port.
297
298 .. attribute:: parity
299
300 Read or write current parity setting. It is possible to change this on
301 an opened port.
302
303 .. attribute:: stopbits
304
305 Read or write current stop bit width setting. It is possible to change
306 this on an opened port.
307
308 .. attribute:: timeout
309
310 Read or write current read timeout setting. It is possible to change
311 this on an opened port.
312
313 .. attribute:: writeTimeout
314
315 Read or write current write timeout setting. It is possible to change
316 this on an opened port.
317
318 .. attribute:: xonxoff
319
320 Read or write current software flow control rate setting. It is
321 possible to change this on an opened port.
322
323 .. attribute:: rtscts
324
325 Read or write current hardware flow control setting. It is possible to
326 change this on an opened port.
327
328 .. attribute:: dsrdtr
329
330 Read or write current hardware flow control setting. It is possible to
331 change this on an opened port.
332
333 .. attribute:: interCharTimeout
334
335 Read or write current inter character timeout setting. It is possible
336 to change this on an opened port.
337
338 The following constants are also provided:
339
340 .. attribute:: BAUDRATES
341
342 A tuple of standard baud rate values. The actual device may support more
343 or less...
344
345 .. attribute:: BYTESIZES
346
347 A tuple of supported byte size values.
348
349 .. attribute:: PARITIES
350
351 A tuple of supported parity settings.
352
353 .. attribute:: STOPBITS
354
355 A tuple of supported stop bit settings.
356
357
358
cliechtic1c37602009-07-21 01:34:57 +0000359Exceptions
360==========
361
362.. exception:: SerialException
363
364 Base class for serial port exceptions.
365
366.. exception:: SerialTimeoutException
367
368 Exception that is raised on write timeouts.
369
370
371Constants
372=========
373
cliechti5134aab2009-07-21 19:47:59 +0000374Parity
cliechtic1c37602009-07-21 01:34:57 +0000375------
376.. data:: PARITY_NONE
377.. data:: PARITY_EVEN
378.. data:: PARITY_ODD
379.. data:: PARITY_MARK
380.. data:: PARITY_SPACE
381
cliechti5134aab2009-07-21 19:47:59 +0000382Stopbits
cliechtic1c37602009-07-21 01:34:57 +0000383--------
384.. data:: STOPBITS_ONE
385.. data:: STOPBITS_ONE_POINT_FIVE
386.. data:: STOPBITS_TWO
387
cliechti5134aab2009-07-21 19:47:59 +0000388Bytesize
cliechtic1c37602009-07-21 01:34:57 +0000389--------
390.. data:: FIVEBITS
391.. data:: SIXBITS
392.. data:: SEVENBITS
393.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000394
395Others
396-------
397.. data:: XON
398.. data:: XOFF