blob: 9df35c62745fa0a6a6132b6755f3c715f67d3c19 [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
cliechtibb5c3c52009-07-23 02:29:27 +000048 Possible values for the parameter ``timeout``:
cliechti5134aab2009-07-21 19:47:59 +000049
cliechtibb5c3c52009-07-23 02:29:27 +000050 - ``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
cliechtibb5c3c52009-07-23 02:29:27 +000073 :param size: Number of bytes to read.
74 :return: Bytes read from the port.
cliechtic1c37602009-07-21 01:34:57 +000075
cliechtibb5c3c52009-07-23 02:29:27 +000076 Read ``size`` bytes from the serial port. If a timeout is set it may
77 return less characters as requested. With no timeout it will block
78 until the requested number of bytes is read.
cliechtic1c37602009-07-21 01:34:57 +000079
cliechtibb5c3c52009-07-23 02:29:27 +000080 .. method:: write(data)
81
82 :param data: Data to send.
83
84 Write the string ``data`` to the port.
cliechtic1c37602009-07-21 01:34:57 +000085
cliechti5134aab2009-07-21 19:47:59 +000086 .. method:: flush():
cliechtic1c37602009-07-21 01:34:57 +000087
88 Flush of file like objects. In this case, wait until all data is
89 written.
90
91 .. method:: flushInput()
92
93 Flush input buffer, discarding all it's contents.
94
95 .. method:: flushOutput()
96
97 Clear output buffer, aborting the current output and
98 discarding all that is in the buffer.
99
100 .. method:: sendBreak(duration=0.25)
101
cliechtibb5c3c52009-07-23 02:29:27 +0000102 :param duration: Time (float) to activate the BREAK condition.
103
cliechtic1c37602009-07-21 01:34:57 +0000104 Send break condition. Timed, returns to idle state after given
105 duration.
106
107 .. method:: setBreak(level=True)
108
cliechtibb5c3c52009-07-23 02:29:27 +0000109 :param level: when true activate BREAK condition, else disable.
110
cliechtic1c37602009-07-21 01:34:57 +0000111 Set break: Controls TXD. When active, no transmitting is possible.
112
113 .. method:: setRTS(level=True)
114
cliechtibb5c3c52009-07-23 02:29:27 +0000115 :param level: Set control line to logic level.
116
cliechtic1c37602009-07-21 01:34:57 +0000117 Set RTS line to specified logic level.
118
119 .. method:: setDTR(level=True)
120
cliechtibb5c3c52009-07-23 02:29:27 +0000121 :param level: Set control line to logic level.
122
cliechtic1c37602009-07-21 01:34:57 +0000123 Set DTR line to specified logic level.
124
125 .. method:: getCTS()
126
cliechtibb5c3c52009-07-23 02:29:27 +0000127 :return: Current state (boolean)
128
cliechtic1c37602009-07-21 01:34:57 +0000129 Return the state of the CTS line.
130
131 .. method:: getDSR()
132
cliechtibb5c3c52009-07-23 02:29:27 +0000133 :return: Current state (boolean)
134
cliechtic1c37602009-07-21 01:34:57 +0000135 Return the state of the DSR line.
136
137 .. method:: getRI()
138
cliechtibb5c3c52009-07-23 02:29:27 +0000139 :return: Current state (boolean)
140
cliechtic1c37602009-07-21 01:34:57 +0000141 Return the state of the RI line.
142
143 .. method:: getCD()
144
cliechtibb5c3c52009-07-23 02:29:27 +0000145 :return: Current state (boolean)
146
cliechtic1c37602009-07-21 01:34:57 +0000147 Return the state of the CD line
148
149 Read-only attributes:
150
151 .. attribute:: portstr
152
cliechti5134aab2009-07-21 19:47:59 +0000153 Device name. This is always the device name even if the
154 port was opened by a number. (Read Only).
cliechtic1c37602009-07-21 01:34:57 +0000155
156 .. attribute:: BAUDRATES
157
158 A list of valid baud rates. The list may be incomplete such that higher
159 baud rates may be supported by the device and that values in between the
160 standard baud rates are supported. (Read Only).
161
162 .. attribute:: BYTESIZES
163
164 A list of valid byte sizes for the device (Read Only).
165
166 .. attribute:: PARITIES
167
168 A list of valid parities for the device (Read Only).
169
170 .. attribute:: STOPBITS
171
172 A list of valid stop bit widths for the device (Read Only).
173
174
175 New values can be assigned to the following attributes, the port will be reconfigured, even if it's opened at that time:
176
177 .. attribute:: port
178
179 Port name/number as set by the user.
180
181 .. attribute:: baudrate
182
183 Current baud rate setting.
184
185 .. attribute:: bytesize
186
187 Byte size in bits.
188
189 .. attribute:: parity
190
191 Parity setting.
192
193 .. attribute:: stopbits
194
195 Stop bit with.
196
197 .. attribute:: timeout
198
cliechti5134aab2009-07-21 19:47:59 +0000199 Timeout setting (seconds, float).
cliechtic1c37602009-07-21 01:34:57 +0000200
201 .. attribute:: xonxoff
202
203 If Xon/Xoff flow control is enabled.
204
205 .. attribute:: rtscts
206
207 If hardware flow control is enabled.
208
209 Platform specific methods.
210
211 .. warning:: Programs using the following methods are not portable to other platforms!
212
213 .. method:: nonblocking()
214
215 :platform: Unix
cliechti86e87872009-07-21 13:32:45 +0000216
cliechtic1c37602009-07-21 01:34:57 +0000217 Configure the device for nonblocking operations. This can be useful if
218 the port is used with ``select``.
219
220 .. method:: fileno()
221
222 :platform: Unix
cliechtibb5c3c52009-07-23 02:29:27 +0000223 :return: File descriptor.
cliechti86e87872009-07-21 13:32:45 +0000224
cliechtibb5c3c52009-07-23 02:29:27 +0000225 Return file descriptor number for the port that is opened by this object.
cliechtic1c37602009-07-21 01:34:57 +0000226
227 .. method:: setXON(level=True)
228
229 :platform: Windows
cliechtibb5c3c52009-07-23 02:29:27 +0000230 :param level: Set flow control state.
cliechti86e87872009-07-21 13:32:45 +0000231
cliechtic1c37602009-07-21 01:34:57 +0000232 Set software flow control state.
233
234
cliechti5b7d16a2009-07-21 21:53:59 +0000235.. class:: FileLike
236
cliechti5b7d16a2009-07-21 21:53:59 +0000237 An abstract file like class. It is used as base class for :class:`Serial`.
238
239 This class implements readline and readlines based on read and
240 writelines based on write.
241 This class is used to provide the above functions for to Serial
242 port objects.
243
244 Note that when the serial port was opened with _NO_ timeout that
245 readline blocks until it sees a newline (or the specified size is
246 reached) and that readlines would never return and therefore
247 refuses to work (it raises an exception in this case)!
248
249 .. method:: readline(size=None, eol='\n')
250
251 :param size: Max number of bytes to read, ``None`` -> no limit.
252 :param eol: The end of line character.
253
254 Read a line which is terminated with end-of-line (eol) character
255 ('\n' by default) or until timeout.
256
257 .. method:: readlines(sizehint=None, eol='\n')
258
259 :param size: Ignored parameter.
260 :param eol: The end of line character.
261
262 Read a list of lines, until timeout. ``sizehint`` is ignored and only
263 present for API compatibility with built-in File objects.
264
265 .. method:: xreadlines(sizehint=None)
266
267 Just calls ``readlines`` - here for compatibility.
268
269 .. method:: writelines(sequence)
270
271 Write a list of strings to the port.
272
273 .. method:: flush()
274
275 Flush of file like objects. It's a no-op in this class, may be overridden.
276
277 .. method:: read()
278
279 Raises NotImplementedError, needs to be overridden by subclass.
280
cliechtibb5c3c52009-07-23 02:29:27 +0000281 .. method:: write(data)
cliechti5b7d16a2009-07-21 21:53:59 +0000282
283 Raises NotImplementedError, needs to be overridden by subclass.
284
285
286 To be able to use the file like object as iterator for e.g.
287 ``for line in Serial(0): ...`` usage:
288
289 .. method:: next()
290
291 Return the next line by calling :meth:`readline`.
292
293 .. method:: __iter__()
294
295 Returns self.
296
297
cliechtiedcdbe42009-07-22 00:48:34 +0000298.. class:: SerialBase
299
300 The following attributes are implemented as properties. They work with open
301 and closed ports.
302
303 .. attribute:: port
304
305 Read or write port. When the port is already open, it will be closed
306 and reopened with the new setting.
307
308 .. attribute:: baudrate
309
310 Read or write current baud rate setting. It is possible to change this
311 on an opened port.
312
313 .. attribute:: bytesize
314
315 Read or write current data byte size setting. It is possible to change
316 this on an opened port.
317
318 .. attribute:: parity
319
320 Read or write current parity setting. It is possible to change this on
321 an opened port.
322
323 .. attribute:: stopbits
324
325 Read or write current stop bit width setting. It is possible to change
326 this on an opened port.
327
328 .. attribute:: timeout
329
330 Read or write current read timeout setting. It is possible to change
331 this on an opened port.
332
333 .. attribute:: writeTimeout
334
335 Read or write current write timeout setting. It is possible to change
336 this on an opened port.
337
338 .. attribute:: xonxoff
339
340 Read or write current software flow control rate setting. It is
341 possible to change this on an opened port.
342
343 .. attribute:: rtscts
344
345 Read or write current hardware flow control setting. It is possible to
346 change this on an opened port.
347
348 .. attribute:: dsrdtr
349
350 Read or write current hardware flow control setting. It is possible to
351 change this on an opened port.
352
353 .. attribute:: interCharTimeout
354
355 Read or write current inter character timeout setting. It is possible
356 to change this on an opened port.
357
358 The following constants are also provided:
359
360 .. attribute:: BAUDRATES
361
362 A tuple of standard baud rate values. The actual device may support more
363 or less...
364
365 .. attribute:: BYTESIZES
366
367 A tuple of supported byte size values.
368
369 .. attribute:: PARITIES
370
371 A tuple of supported parity settings.
372
373 .. attribute:: STOPBITS
374
375 A tuple of supported stop bit settings.
376
377
378
cliechtic1c37602009-07-21 01:34:57 +0000379Exceptions
380==========
381
382.. exception:: SerialException
383
384 Base class for serial port exceptions.
385
386.. exception:: SerialTimeoutException
387
388 Exception that is raised on write timeouts.
389
390
391Constants
392=========
393
cliechti5134aab2009-07-21 19:47:59 +0000394Parity
cliechtic1c37602009-07-21 01:34:57 +0000395------
396.. data:: PARITY_NONE
397.. data:: PARITY_EVEN
398.. data:: PARITY_ODD
399.. data:: PARITY_MARK
400.. data:: PARITY_SPACE
401
cliechti5134aab2009-07-21 19:47:59 +0000402Stopbits
cliechtic1c37602009-07-21 01:34:57 +0000403--------
404.. data:: STOPBITS_ONE
405.. data:: STOPBITS_ONE_POINT_FIVE
406.. data:: STOPBITS_TWO
407
cliechti5134aab2009-07-21 19:47:59 +0000408Bytesize
cliechtic1c37602009-07-21 01:34:57 +0000409--------
410.. data:: FIVEBITS
411.. data:: SIXBITS
412.. data:: SEVENBITS
413.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000414
415Others
416-------
417.. data:: XON
418.. data:: XOFF