blob: f5d5791f9aa0eaa9fcd8b414b6ba6fb2605561df [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
cliechti6066f842009-07-24 00:05:45 +000041 :exception ValueError:
42 Will be raised when parameter are out of range, e.g. baudrate, data bits.
43
44 :exception SerialException:
45 In case the device can not be found or can not be configured.
46
47
cliechti5134aab2009-07-21 19:47:59 +000048 The port is immediately opened on object creation, when a ``port`` is
49 given. It is not opened when port is None.
cliechtic1c37602009-07-21 01:34:57 +000050
cliechti5134aab2009-07-21 19:47:59 +000051 - Number: number of device, numbering starts at zero.
52 - Device name: depending on operating system. e.g. ``/dev/ttyUSB0``
53 on GNU/Linux or ``COM3`` on Windows.
cliechtic1c37602009-07-21 01:34:57 +000054
cliechtibb5c3c52009-07-23 02:29:27 +000055 Possible values for the parameter ``timeout``:
cliechti5134aab2009-07-21 19:47:59 +000056
cliechtibb5c3c52009-07-23 02:29:27 +000057 - ``timeout = None``: wait forever
58 - ``timeout = 0``: non-blocking mode (return immediately on read)
59 - ``timeout = x``: set timeout to x seconds (float allowed)
cliechtic1c37602009-07-21 01:34:57 +000060
61
62 .. method:: open()
63
64 Open port.
65
66 .. method:: close()
67
68 Close port immediately.
69
70 .. method:: setBaudrate(baudrate)
71
72 Change baud rate on an open port.
73
74 .. method:: inWaiting()
75
76 Return the number of chars in the receive buffer.
77
78 .. method:: read(size=1)
79
cliechtibb5c3c52009-07-23 02:29:27 +000080 :param size: Number of bytes to read.
81 :return: Bytes read from the port.
cliechtic1c37602009-07-21 01:34:57 +000082
cliechtibb5c3c52009-07-23 02:29:27 +000083 Read ``size`` bytes from the serial port. If a timeout is set it may
84 return less characters as requested. With no timeout it will block
85 until the requested number of bytes is read.
cliechtic1c37602009-07-21 01:34:57 +000086
cliechtibb5c3c52009-07-23 02:29:27 +000087 .. method:: write(data)
88
89 :param data: Data to send.
90
cliechti6066f842009-07-24 00:05:45 +000091 :exception SerialTimeoutException:
92 In case a write timeout is configured for the port and the time is
93 exceeded.
94
cliechtibb5c3c52009-07-23 02:29:27 +000095 Write the string ``data`` to the port.
cliechtic1c37602009-07-21 01:34:57 +000096
cliechti5134aab2009-07-21 19:47:59 +000097 .. method:: flush():
cliechtic1c37602009-07-21 01:34:57 +000098
99 Flush of file like objects. In this case, wait until all data is
100 written.
101
102 .. method:: flushInput()
103
104 Flush input buffer, discarding all it's contents.
105
106 .. method:: flushOutput()
107
108 Clear output buffer, aborting the current output and
109 discarding all that is in the buffer.
110
111 .. method:: sendBreak(duration=0.25)
112
cliechtibb5c3c52009-07-23 02:29:27 +0000113 :param duration: Time (float) to activate the BREAK condition.
114
cliechtic1c37602009-07-21 01:34:57 +0000115 Send break condition. Timed, returns to idle state after given
116 duration.
117
118 .. method:: setBreak(level=True)
119
cliechtibb5c3c52009-07-23 02:29:27 +0000120 :param level: when true activate BREAK condition, else disable.
121
cliechtic1c37602009-07-21 01:34:57 +0000122 Set break: Controls TXD. When active, no transmitting is possible.
123
124 .. method:: setRTS(level=True)
125
cliechtibb5c3c52009-07-23 02:29:27 +0000126 :param level: Set control line to logic level.
127
cliechtic1c37602009-07-21 01:34:57 +0000128 Set RTS line to specified logic level.
129
130 .. method:: setDTR(level=True)
131
cliechtibb5c3c52009-07-23 02:29:27 +0000132 :param level: Set control line to logic level.
133
cliechtic1c37602009-07-21 01:34:57 +0000134 Set DTR line to specified logic level.
135
136 .. method:: getCTS()
137
cliechtibb5c3c52009-07-23 02:29:27 +0000138 :return: Current state (boolean)
139
cliechtic1c37602009-07-21 01:34:57 +0000140 Return the state of the CTS line.
141
142 .. method:: getDSR()
143
cliechtibb5c3c52009-07-23 02:29:27 +0000144 :return: Current state (boolean)
145
cliechtic1c37602009-07-21 01:34:57 +0000146 Return the state of the DSR line.
147
148 .. method:: getRI()
149
cliechtibb5c3c52009-07-23 02:29:27 +0000150 :return: Current state (boolean)
151
cliechtic1c37602009-07-21 01:34:57 +0000152 Return the state of the RI line.
153
154 .. method:: getCD()
155
cliechtibb5c3c52009-07-23 02:29:27 +0000156 :return: Current state (boolean)
157
cliechtic1c37602009-07-21 01:34:57 +0000158 Return the state of the CD line
159
160 Read-only attributes:
161
162 .. attribute:: portstr
163
cliechti5134aab2009-07-21 19:47:59 +0000164 Device name. This is always the device name even if the
165 port was opened by a number. (Read Only).
cliechtic1c37602009-07-21 01:34:57 +0000166
167 .. attribute:: BAUDRATES
168
169 A list of valid baud rates. The list may be incomplete such that higher
170 baud rates may be supported by the device and that values in between the
171 standard baud rates are supported. (Read Only).
172
173 .. attribute:: BYTESIZES
174
175 A list of valid byte sizes for the device (Read Only).
176
177 .. attribute:: PARITIES
178
179 A list of valid parities for the device (Read Only).
180
181 .. attribute:: STOPBITS
182
183 A list of valid stop bit widths for the device (Read Only).
184
185
186 New values can be assigned to the following attributes, the port will be reconfigured, even if it's opened at that time:
187
188 .. attribute:: port
189
190 Port name/number as set by the user.
191
192 .. attribute:: baudrate
193
194 Current baud rate setting.
195
196 .. attribute:: bytesize
197
198 Byte size in bits.
199
200 .. attribute:: parity
201
202 Parity setting.
203
204 .. attribute:: stopbits
205
206 Stop bit with.
207
208 .. attribute:: timeout
209
cliechti5134aab2009-07-21 19:47:59 +0000210 Timeout setting (seconds, float).
cliechtic1c37602009-07-21 01:34:57 +0000211
212 .. attribute:: xonxoff
213
214 If Xon/Xoff flow control is enabled.
215
216 .. attribute:: rtscts
217
218 If hardware flow control is enabled.
219
220 Platform specific methods.
221
222 .. warning:: Programs using the following methods are not portable to other platforms!
223
224 .. method:: nonblocking()
225
226 :platform: Unix
cliechti86e87872009-07-21 13:32:45 +0000227
cliechtic1c37602009-07-21 01:34:57 +0000228 Configure the device for nonblocking operations. This can be useful if
229 the port is used with ``select``.
230
231 .. method:: fileno()
232
233 :platform: Unix
cliechtibb5c3c52009-07-23 02:29:27 +0000234 :return: File descriptor.
cliechti86e87872009-07-21 13:32:45 +0000235
cliechtibb5c3c52009-07-23 02:29:27 +0000236 Return file descriptor number for the port that is opened by this object.
cliechtic1c37602009-07-21 01:34:57 +0000237
238 .. method:: setXON(level=True)
239
240 :platform: Windows
cliechtibb5c3c52009-07-23 02:29:27 +0000241 :param level: Set flow control state.
cliechti86e87872009-07-21 13:32:45 +0000242
cliechtic1c37602009-07-21 01:34:57 +0000243 Set software flow control state.
244
245
cliechti5b7d16a2009-07-21 21:53:59 +0000246.. class:: FileLike
247
cliechti5b7d16a2009-07-21 21:53:59 +0000248 An abstract file like class. It is used as base class for :class:`Serial`.
249
250 This class implements readline and readlines based on read and
251 writelines based on write.
252 This class is used to provide the above functions for to Serial
253 port objects.
254
cliechti6066f842009-07-24 00:05:45 +0000255 Note that when the serial port was opened with no timeout that
cliechti5b7d16a2009-07-21 21:53:59 +0000256 readline blocks until it sees a newline (or the specified size is
257 reached) and that readlines would never return and therefore
258 refuses to work (it raises an exception in this case)!
259
260 .. method:: readline(size=None, eol='\n')
261
262 :param size: Max number of bytes to read, ``None`` -> no limit.
263 :param eol: The end of line character.
264
265 Read a line which is terminated with end-of-line (eol) character
266 ('\n' by default) or until timeout.
267
268 .. method:: readlines(sizehint=None, eol='\n')
269
270 :param size: Ignored parameter.
271 :param eol: The end of line character.
272
273 Read a list of lines, until timeout. ``sizehint`` is ignored and only
274 present for API compatibility with built-in File objects.
275
276 .. method:: xreadlines(sizehint=None)
277
278 Just calls ``readlines`` - here for compatibility.
279
280 .. method:: writelines(sequence)
281
282 Write a list of strings to the port.
283
cliechti6066f842009-07-24 00:05:45 +0000284
285 The following three methods are overridden in :class:`Serial`.
286
cliechti5b7d16a2009-07-21 21:53:59 +0000287 .. method:: flush()
288
289 Flush of file like objects. It's a no-op in this class, may be overridden.
290
291 .. method:: read()
292
293 Raises NotImplementedError, needs to be overridden by subclass.
294
cliechtibb5c3c52009-07-23 02:29:27 +0000295 .. method:: write(data)
cliechti5b7d16a2009-07-21 21:53:59 +0000296
297 Raises NotImplementedError, needs to be overridden by subclass.
298
299
300 To be able to use the file like object as iterator for e.g.
301 ``for line in Serial(0): ...`` usage:
302
303 .. method:: next()
304
305 Return the next line by calling :meth:`readline`.
306
307 .. method:: __iter__()
308
309 Returns self.
310
311
cliechtiedcdbe42009-07-22 00:48:34 +0000312.. class:: SerialBase
313
314 The following attributes are implemented as properties. They work with open
315 and closed ports.
316
317 .. attribute:: port
318
319 Read or write port. When the port is already open, it will be closed
320 and reopened with the new setting.
321
322 .. attribute:: baudrate
323
cliechti6066f842009-07-24 00:05:45 +0000324 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000325
326 .. attribute:: bytesize
327
cliechti6066f842009-07-24 00:05:45 +0000328 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000329
330 .. attribute:: parity
331
cliechti6066f842009-07-24 00:05:45 +0000332 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000333
334 .. attribute:: stopbits
335
cliechti6066f842009-07-24 00:05:45 +0000336 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000337
338 .. attribute:: timeout
339
cliechti6066f842009-07-24 00:05:45 +0000340 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000341
342 .. attribute:: writeTimeout
343
cliechti6066f842009-07-24 00:05:45 +0000344 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000345
346 .. attribute:: xonxoff
347
cliechti6066f842009-07-24 00:05:45 +0000348 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000349
350 .. attribute:: rtscts
351
cliechti6066f842009-07-24 00:05:45 +0000352 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000353
354 .. attribute:: dsrdtr
355
cliechti6066f842009-07-24 00:05:45 +0000356 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000357
358 .. attribute:: interCharTimeout
359
cliechti6066f842009-07-24 00:05:45 +0000360 Read or write current inter character timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000361
362 The following constants are also provided:
363
364 .. attribute:: BAUDRATES
365
366 A tuple of standard baud rate values. The actual device may support more
367 or less...
368
369 .. attribute:: BYTESIZES
370
371 A tuple of supported byte size values.
372
373 .. attribute:: PARITIES
374
375 A tuple of supported parity settings.
376
377 .. attribute:: STOPBITS
378
379 A tuple of supported stop bit settings.
380
381
382
cliechtic1c37602009-07-21 01:34:57 +0000383Exceptions
384==========
385
386.. exception:: SerialException
387
388 Base class for serial port exceptions.
389
390.. exception:: SerialTimeoutException
391
392 Exception that is raised on write timeouts.
393
394
395Constants
396=========
397
cliechti5134aab2009-07-21 19:47:59 +0000398Parity
cliechtic1c37602009-07-21 01:34:57 +0000399------
400.. data:: PARITY_NONE
401.. data:: PARITY_EVEN
402.. data:: PARITY_ODD
403.. data:: PARITY_MARK
404.. data:: PARITY_SPACE
405
cliechti5134aab2009-07-21 19:47:59 +0000406Stopbits
cliechtic1c37602009-07-21 01:34:57 +0000407--------
408.. data:: STOPBITS_ONE
409.. data:: STOPBITS_ONE_POINT_FIVE
410.. data:: STOPBITS_TWO
411
cliechti5134aab2009-07-21 19:47:59 +0000412Bytesize
cliechtic1c37602009-07-21 01:34:57 +0000413--------
414.. data:: FIVEBITS
415.. data:: SIXBITS
416.. data:: SEVENBITS
417.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000418
419Others
420-------
cliechti6066f842009-07-24 00:05:45 +0000421Default control characters for software flow control.
422
cliechti5b7d16a2009-07-21 21:53:59 +0000423.. data:: XON
424.. data:: XOFF