blob: f5fa01a8a31ed65e4d544e9d267c5a35a24d39f3 [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)
cliechtif81362e2009-07-25 03:44:33 +000059 - ``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
cliechtic1c37602009-07-21 01:34:57 +000070
cliechtif81362e2009-07-25 03:44:33 +000071 The following methods may rise :exc:`ValueError` when applied to a closed port.
cliechtic1c37602009-07-21 01:34:57 +000072
73 .. method:: inWaiting()
74
75 Return the number of chars in the receive buffer.
76
77 .. method:: read(size=1)
78
cliechtibb5c3c52009-07-23 02:29:27 +000079 :param size: Number of bytes to read.
80 :return: Bytes read from the port.
cliechtic1c37602009-07-21 01:34:57 +000081
cliechtibb5c3c52009-07-23 02:29:27 +000082 Read ``size`` bytes from the serial port. If a timeout is set it may
83 return less characters as requested. With no timeout it will block
84 until the requested number of bytes is read.
cliechtic1c37602009-07-21 01:34:57 +000085
cliechtibb5c3c52009-07-23 02:29:27 +000086 .. method:: write(data)
87
88 :param data: Data to send.
89
cliechti6066f842009-07-24 00:05:45 +000090 :exception SerialTimeoutException:
91 In case a write timeout is configured for the port and the time is
92 exceeded.
93
cliechtibb5c3c52009-07-23 02:29:27 +000094 Write the string ``data`` to the port.
cliechtic1c37602009-07-21 01:34:57 +000095
cliechti5134aab2009-07-21 19:47:59 +000096 .. method:: flush():
cliechtic1c37602009-07-21 01:34:57 +000097
98 Flush of file like objects. In this case, wait until all data is
99 written.
100
101 .. method:: flushInput()
102
103 Flush input buffer, discarding all it's contents.
104
105 .. method:: flushOutput()
106
107 Clear output buffer, aborting the current output and
108 discarding all that is in the buffer.
109
110 .. method:: sendBreak(duration=0.25)
111
cliechtibb5c3c52009-07-23 02:29:27 +0000112 :param duration: Time (float) to activate the BREAK condition.
113
cliechtic1c37602009-07-21 01:34:57 +0000114 Send break condition. Timed, returns to idle state after given
115 duration.
116
117 .. method:: setBreak(level=True)
118
cliechtibb5c3c52009-07-23 02:29:27 +0000119 :param level: when true activate BREAK condition, else disable.
120
cliechtic1c37602009-07-21 01:34:57 +0000121 Set break: Controls TXD. When active, no transmitting is possible.
122
123 .. method:: setRTS(level=True)
124
cliechtibb5c3c52009-07-23 02:29:27 +0000125 :param level: Set control line to logic level.
126
cliechtic1c37602009-07-21 01:34:57 +0000127 Set RTS line to specified logic level.
128
129 .. method:: setDTR(level=True)
130
cliechtibb5c3c52009-07-23 02:29:27 +0000131 :param level: Set control line to logic level.
132
cliechtic1c37602009-07-21 01:34:57 +0000133 Set DTR line to specified logic level.
134
135 .. method:: getCTS()
136
cliechtibb5c3c52009-07-23 02:29:27 +0000137 :return: Current state (boolean)
138
cliechtic1c37602009-07-21 01:34:57 +0000139 Return the state of the CTS line.
140
141 .. method:: getDSR()
142
cliechtibb5c3c52009-07-23 02:29:27 +0000143 :return: Current state (boolean)
144
cliechtic1c37602009-07-21 01:34:57 +0000145 Return the state of the DSR line.
146
147 .. method:: getRI()
148
cliechtibb5c3c52009-07-23 02:29:27 +0000149 :return: Current state (boolean)
150
cliechtic1c37602009-07-21 01:34:57 +0000151 Return the state of the RI line.
152
153 .. method:: getCD()
154
cliechtibb5c3c52009-07-23 02:29:27 +0000155 :return: Current state (boolean)
156
cliechtic1c37602009-07-21 01:34:57 +0000157 Return the state of the CD line
158
159 Read-only attributes:
160
cliechtif81362e2009-07-25 03:44:33 +0000161 .. attribute:: name
cliechtic1c37602009-07-21 01:34:57 +0000162
cliechti5134aab2009-07-21 19:47:59 +0000163 Device name. This is always the device name even if the
164 port was opened by a number. (Read Only).
cliechtic1c37602009-07-21 01:34:57 +0000165
cliechtif81362e2009-07-25 03:44:33 +0000166 .. versionadded:: 2.5
167
168 .. attribute:: portstr
169
170 :deprecated: use :attr:`name` instead
171
cliechtic1c37602009-07-21 01:34:57 +0000172 .. attribute:: BAUDRATES
173
174 A list of valid baud rates. The list may be incomplete such that higher
175 baud rates may be supported by the device and that values in between the
176 standard baud rates are supported. (Read Only).
177
178 .. attribute:: BYTESIZES
179
180 A list of valid byte sizes for the device (Read Only).
181
182 .. attribute:: PARITIES
183
184 A list of valid parities for the device (Read Only).
185
186 .. attribute:: STOPBITS
187
188 A list of valid stop bit widths for the device (Read Only).
189
190
191 New values can be assigned to the following attributes, the port will be reconfigured, even if it's opened at that time:
192
193 .. attribute:: port
194
195 Port name/number as set by the user.
196
197 .. attribute:: baudrate
198
199 Current baud rate setting.
200
201 .. attribute:: bytesize
202
203 Byte size in bits.
204
205 .. attribute:: parity
206
207 Parity setting.
208
209 .. attribute:: stopbits
210
211 Stop bit with.
212
213 .. attribute:: timeout
214
cliechti5134aab2009-07-21 19:47:59 +0000215 Timeout setting (seconds, float).
cliechtic1c37602009-07-21 01:34:57 +0000216
217 .. attribute:: xonxoff
218
219 If Xon/Xoff flow control is enabled.
220
221 .. attribute:: rtscts
222
223 If hardware flow control is enabled.
224
225 Platform specific methods.
226
227 .. warning:: Programs using the following methods are not portable to other platforms!
228
229 .. method:: nonblocking()
230
231 :platform: Unix
cliechti86e87872009-07-21 13:32:45 +0000232
cliechtic1c37602009-07-21 01:34:57 +0000233 Configure the device for nonblocking operations. This can be useful if
234 the port is used with ``select``.
235
236 .. method:: fileno()
237
238 :platform: Unix
cliechtibb5c3c52009-07-23 02:29:27 +0000239 :return: File descriptor.
cliechti86e87872009-07-21 13:32:45 +0000240
cliechtibb5c3c52009-07-23 02:29:27 +0000241 Return file descriptor number for the port that is opened by this object.
cliechtic1c37602009-07-21 01:34:57 +0000242
243 .. method:: setXON(level=True)
244
245 :platform: Windows
cliechtibb5c3c52009-07-23 02:29:27 +0000246 :param level: Set flow control state.
cliechti86e87872009-07-21 13:32:45 +0000247
cliechtic1c37602009-07-21 01:34:57 +0000248 Set software flow control state.
249
250
cliechtif81362e2009-07-25 03:44:33 +0000251.. class:: RawSerial
252
253 This class is only present when run with Python 2.6 and newer that prides
254 the module :mod:`io`. It shares the same interface with :class:`Serial`
255 with the difference that :meth:`read` and :meth:`write` work with
256 :class:`bytes`and :class:`bytearrays`.
257
258 This also means that readline is borrowed from the :mod:`io` module and
259 lacks the ``eol`` parameter.
260
261 .. versionadded:: 2.5
262
263
cliechti5b7d16a2009-07-21 21:53:59 +0000264.. class:: FileLike
265
cliechti5b7d16a2009-07-21 21:53:59 +0000266 An abstract file like class. It is used as base class for :class:`Serial`.
267
268 This class implements readline and readlines based on read and
269 writelines based on write.
270 This class is used to provide the above functions for to Serial
271 port objects.
272
cliechti6066f842009-07-24 00:05:45 +0000273 Note that when the serial port was opened with no timeout that
cliechti5b7d16a2009-07-21 21:53:59 +0000274 readline blocks until it sees a newline (or the specified size is
275 reached) and that readlines would never return and therefore
276 refuses to work (it raises an exception in this case)!
277
278 .. method:: readline(size=None, eol='\n')
279
280 :param size: Max number of bytes to read, ``None`` -> no limit.
281 :param eol: The end of line character.
282
283 Read a line which is terminated with end-of-line (eol) character
284 ('\n' by default) or until timeout.
285
286 .. method:: readlines(sizehint=None, eol='\n')
287
288 :param size: Ignored parameter.
289 :param eol: The end of line character.
290
291 Read a list of lines, until timeout. ``sizehint`` is ignored and only
292 present for API compatibility with built-in File objects.
293
294 .. method:: xreadlines(sizehint=None)
295
296 Just calls ``readlines`` - here for compatibility.
297
298 .. method:: writelines(sequence)
299
300 Write a list of strings to the port.
301
cliechti6066f842009-07-24 00:05:45 +0000302
303 The following three methods are overridden in :class:`Serial`.
304
cliechti5b7d16a2009-07-21 21:53:59 +0000305 .. method:: flush()
306
307 Flush of file like objects. It's a no-op in this class, may be overridden.
308
309 .. method:: read()
310
311 Raises NotImplementedError, needs to be overridden by subclass.
312
cliechtibb5c3c52009-07-23 02:29:27 +0000313 .. method:: write(data)
cliechti5b7d16a2009-07-21 21:53:59 +0000314
315 Raises NotImplementedError, needs to be overridden by subclass.
316
cliechtif81362e2009-07-25 03:44:33 +0000317 The following functions are implemented for compatibility with other
318 file-like objects, however serial ports are not seekable.
319
320
321 .. method:: seek(pos, whence=0)
322
323 :exception IOError: always, as method is not supported on serial port
324
325 .. versionadded:: 2.5
326
327 .. method:: tell()
328
329 :exception IOError: always, as method is not supported on serial port
330
331 .. versionadded:: 2.5
332
333 .. method:: truncate(self, n=None):
334
335 :exception IOError: always, as method is not supported on serial port
336
337 .. versionadded:: 2.5
338
339 .. method:: isatty()
340
341 :exception IOError: always, as method is not supported on serial port
342
343 .. versionadded:: 2.5
cliechti5b7d16a2009-07-21 21:53:59 +0000344
345 To be able to use the file like object as iterator for e.g.
346 ``for line in Serial(0): ...`` usage:
347
348 .. method:: next()
349
350 Return the next line by calling :meth:`readline`.
351
352 .. method:: __iter__()
353
354 Returns self.
355
356
cliechtiedcdbe42009-07-22 00:48:34 +0000357.. class:: SerialBase
358
359 The following attributes are implemented as properties. They work with open
360 and closed ports.
361
362 .. attribute:: port
363
364 Read or write port. When the port is already open, it will be closed
365 and reopened with the new setting.
366
367 .. attribute:: baudrate
368
cliechti6066f842009-07-24 00:05:45 +0000369 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000370
371 .. attribute:: bytesize
372
cliechti6066f842009-07-24 00:05:45 +0000373 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000374
375 .. attribute:: parity
376
cliechti6066f842009-07-24 00:05:45 +0000377 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000378
379 .. attribute:: stopbits
380
cliechti6066f842009-07-24 00:05:45 +0000381 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000382
383 .. attribute:: timeout
384
cliechti6066f842009-07-24 00:05:45 +0000385 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000386
387 .. attribute:: writeTimeout
388
cliechti6066f842009-07-24 00:05:45 +0000389 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000390
391 .. attribute:: xonxoff
392
cliechti6066f842009-07-24 00:05:45 +0000393 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000394
395 .. attribute:: rtscts
396
cliechti6066f842009-07-24 00:05:45 +0000397 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000398
399 .. attribute:: dsrdtr
400
cliechti6066f842009-07-24 00:05:45 +0000401 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000402
403 .. attribute:: interCharTimeout
404
cliechti6066f842009-07-24 00:05:45 +0000405 Read or write current inter character timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000406
407 The following constants are also provided:
408
409 .. attribute:: BAUDRATES
410
411 A tuple of standard baud rate values. The actual device may support more
412 or less...
413
414 .. attribute:: BYTESIZES
415
416 A tuple of supported byte size values.
417
418 .. attribute:: PARITIES
419
420 A tuple of supported parity settings.
421
422 .. attribute:: STOPBITS
423
424 A tuple of supported stop bit settings.
425
426
427
cliechtic1c37602009-07-21 01:34:57 +0000428Exceptions
429==========
430
431.. exception:: SerialException
432
433 Base class for serial port exceptions.
434
435.. exception:: SerialTimeoutException
436
437 Exception that is raised on write timeouts.
438
439
440Constants
441=========
442
cliechti5134aab2009-07-21 19:47:59 +0000443Parity
cliechtic1c37602009-07-21 01:34:57 +0000444------
445.. data:: PARITY_NONE
446.. data:: PARITY_EVEN
447.. data:: PARITY_ODD
448.. data:: PARITY_MARK
449.. data:: PARITY_SPACE
450
cliechti5134aab2009-07-21 19:47:59 +0000451Stopbits
cliechtic1c37602009-07-21 01:34:57 +0000452--------
453.. data:: STOPBITS_ONE
454.. data:: STOPBITS_ONE_POINT_FIVE
455.. data:: STOPBITS_TWO
456
cliechti5134aab2009-07-21 19:47:59 +0000457Bytesize
cliechtic1c37602009-07-21 01:34:57 +0000458--------
459.. data:: FIVEBITS
460.. data:: SIXBITS
461.. data:: SEVENBITS
462.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000463
464Others
465-------
cliechti6066f842009-07-24 00:05:45 +0000466Default control characters for software flow control.
467
cliechti5b7d16a2009-07-21 21:53:59 +0000468.. data:: XON
469.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000470
471Version
472
473.. data:: VERSION
474
475 A string indicating the pySerial version, such as ``2.5``.
476
477Functions:
478
479.. function:: device(number)
480
481 :param number: Port number.
482 :return: String containing device name.
483 :deprecated: Use device names directly.
484
485 Convert a port number to a platform dependent device name. Unfortunately
486 this does not work well for all platforms; e.g. some may miss USB-Serial
487 converters and enumerate only internal serial ports.
488
489 The conversion may be made off-line, that is, there is no guarantee that
490 the returned device name really exists on the system.