blob: 6b913a5065837788abfa17e978e384def43aa9c0 [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:
cliechti4a567a02009-07-27 22:09:31 +000015 Device name or port number number or :const:`None`.
cliechtic1c37602009-07-21 01:34:57 +000016
cliechtic1c37602009-07-21 01:34:57 +000017 :param baudrate:
18 Baud rate such as 9600 or 115200 etc.
19
20 :param bytesize:
cliechti4a567a02009-07-27 22:09:31 +000021 Number of data bits. Possible values:
22 :const:`FIVEBITS`, :const:`SIXBITS`, :const:`SEVENBITS`,
23 :const:`EIGHTBITS`
cliechtic1c37602009-07-21 01:34:57 +000024
25 :param parity:
cliechti4a567a02009-07-27 22:09:31 +000026 Enable parity checking. Possible values:
27 :const:`PARITY_NONE` :const:`PARITY_EVEN` :const:`PARITY_ODD`
28 :const:`PARITY_MARK` :const:`PARITY_SPACE`
cliechtic1c37602009-07-21 01:34:57 +000029
30 :param stopbits:
cliechti4a567a02009-07-27 22:09:31 +000031 Number of stop bits. Possible values:
32 :const:`STOPBITS_ONE` :const:`STOPBITS_ONE_POINT_FIVE`
33 :const:`STOPBITS_TWO`
cliechtic1c37602009-07-21 01:34:57 +000034
35 :param timeout:
36 Set a read timeout value.
37
38 :param xonxoff:
cliechtic1c37602009-07-21 01:34:57 +000039 Enable software flow control.
40
41 :param rtscts:
cliechtic1c37602009-07-21 01:34:57 +000042 Enable hardware (RTS/CTS) flow control.
43
44 :param interCharTimeout:
cliechti4a567a02009-07-27 22:09:31 +000045 Inter-character timeout, :const:`None` to disable (default).
cliechtic1c37602009-07-21 01:34:57 +000046
cliechti6066f842009-07-24 00:05:45 +000047 :exception ValueError:
cliechti4a567a02009-07-27 22:09:31 +000048 Will be raised when parameter are out of range, e.g. baud rate, data bits.
cliechti6066f842009-07-24 00:05:45 +000049
50 :exception SerialException:
51 In case the device can not be found or can not be configured.
52
53
cliechti4a567a02009-07-27 22:09:31 +000054 The port is immediately opened on object creation, when a *port* is
55 given. It is not opened when *port* is :const:`None` and a successive call
56 to :meth:`open` will be needed.
57
58 Possible values for the parameter *port*:
cliechtic1c37602009-07-21 01:34:57 +000059
cliechti5134aab2009-07-21 19:47:59 +000060 - Number: number of device, numbering starts at zero.
61 - Device name: depending on operating system. e.g. ``/dev/ttyUSB0``
62 on GNU/Linux or ``COM3`` on Windows.
cliechtic1c37602009-07-21 01:34:57 +000063
cliechti4a567a02009-07-27 22:09:31 +000064 Possible values for the parameter *timeout*:
cliechti5134aab2009-07-21 19:47:59 +000065
cliechtibb5c3c52009-07-23 02:29:27 +000066 - ``timeout = None``: wait forever
67 - ``timeout = 0``: non-blocking mode (return immediately on read)
cliechtif81362e2009-07-25 03:44:33 +000068 - ``timeout = x``: set timeout to ``x`` seconds (float allowed)
cliechtic1c37602009-07-21 01:34:57 +000069
70
71 .. method:: open()
72
73 Open port.
74
75 .. method:: close()
76
77 Close port immediately.
78
cliechtic1c37602009-07-21 01:34:57 +000079
cliechti4a567a02009-07-27 22:09:31 +000080 The following methods may raise :exc:`ValueError` when applied to a closed
81 port.
cliechtic1c37602009-07-21 01:34:57 +000082
83 .. method:: read(size=1)
84
cliechtibb5c3c52009-07-23 02:29:27 +000085 :param size: Number of bytes to read.
86 :return: Bytes read from the port.
cliechtic1c37602009-07-21 01:34:57 +000087
cliechti4a567a02009-07-27 22:09:31 +000088 Read *size* bytes from the serial port. If a timeout is set it may
cliechtibb5c3c52009-07-23 02:29:27 +000089 return less characters as requested. With no timeout it will block
90 until the requested number of bytes is read.
cliechtic1c37602009-07-21 01:34:57 +000091
cliechti4a567a02009-07-27 22:09:31 +000092 .. versionchanged:: 2.5
93 Returns an instance of :class:`bytes` when available (Python 2.6
94 and newer) and :class:`str` otherwiese.
95
cliechtibb5c3c52009-07-23 02:29:27 +000096 .. method:: write(data)
97
98 :param data: Data to send.
cliechti4a567a02009-07-27 22:09:31 +000099 :return: Number of bytes written.
cliechti6066f842009-07-24 00:05:45 +0000100 :exception SerialTimeoutException:
101 In case a write timeout is configured for the port and the time is
102 exceeded.
103
cliechti4a567a02009-07-27 22:09:31 +0000104 Write the string *data* to the port.
cliechtic1c37602009-07-21 01:34:57 +0000105
cliechti4a567a02009-07-27 22:09:31 +0000106 .. versionchanged:: 2.5
107 Accepts an instance of :class:`bytes` when available (Python 2.6
108 and newer) and :class:`str` otherwiese.
109
110 .. method:: inWaiting()
111
112 Return the number of chars in the receive buffer.
113
114 .. method:: flush()
cliechtic1c37602009-07-21 01:34:57 +0000115
116 Flush of file like objects. In this case, wait until all data is
117 written.
118
119 .. method:: flushInput()
120
121 Flush input buffer, discarding all it's contents.
122
123 .. method:: flushOutput()
124
125 Clear output buffer, aborting the current output and
126 discarding all that is in the buffer.
127
128 .. method:: sendBreak(duration=0.25)
129
cliechtibb5c3c52009-07-23 02:29:27 +0000130 :param duration: Time (float) to activate the BREAK condition.
131
cliechtic1c37602009-07-21 01:34:57 +0000132 Send break condition. Timed, returns to idle state after given
133 duration.
134
135 .. method:: setBreak(level=True)
136
cliechtibb5c3c52009-07-23 02:29:27 +0000137 :param level: when true activate BREAK condition, else disable.
138
cliechtic1c37602009-07-21 01:34:57 +0000139 Set break: Controls TXD. When active, no transmitting is possible.
140
141 .. method:: setRTS(level=True)
142
cliechtibb5c3c52009-07-23 02:29:27 +0000143 :param level: Set control line to logic level.
144
cliechtic1c37602009-07-21 01:34:57 +0000145 Set RTS line to specified logic level.
146
147 .. method:: setDTR(level=True)
148
cliechtibb5c3c52009-07-23 02:29:27 +0000149 :param level: Set control line to logic level.
150
cliechtic1c37602009-07-21 01:34:57 +0000151 Set DTR line to specified logic level.
152
153 .. method:: getCTS()
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 CTS line.
158
159 .. method:: getDSR()
160
cliechtibb5c3c52009-07-23 02:29:27 +0000161 :return: Current state (boolean)
162
cliechtic1c37602009-07-21 01:34:57 +0000163 Return the state of the DSR line.
164
165 .. method:: getRI()
166
cliechtibb5c3c52009-07-23 02:29:27 +0000167 :return: Current state (boolean)
168
cliechtic1c37602009-07-21 01:34:57 +0000169 Return the state of the RI line.
170
171 .. method:: getCD()
172
cliechtibb5c3c52009-07-23 02:29:27 +0000173 :return: Current state (boolean)
174
cliechtic1c37602009-07-21 01:34:57 +0000175 Return the state of the CD line
176
177 Read-only attributes:
178
cliechtif81362e2009-07-25 03:44:33 +0000179 .. attribute:: name
cliechtic1c37602009-07-21 01:34:57 +0000180
cliechti5134aab2009-07-21 19:47:59 +0000181 Device name. This is always the device name even if the
182 port was opened by a number. (Read Only).
cliechtic1c37602009-07-21 01:34:57 +0000183
cliechtif81362e2009-07-25 03:44:33 +0000184 .. versionadded:: 2.5
185
186 .. attribute:: portstr
187
188 :deprecated: use :attr:`name` instead
189
cliechtic1c37602009-07-21 01:34:57 +0000190 .. attribute:: BAUDRATES
191
192 A list of valid baud rates. The list may be incomplete such that higher
193 baud rates may be supported by the device and that values in between the
194 standard baud rates are supported. (Read Only).
195
196 .. attribute:: BYTESIZES
197
198 A list of valid byte sizes for the device (Read Only).
199
200 .. attribute:: PARITIES
201
202 A list of valid parities for the device (Read Only).
203
204 .. attribute:: STOPBITS
205
206 A list of valid stop bit widths for the device (Read Only).
207
208
cliechti4a567a02009-07-27 22:09:31 +0000209 New values can be assigned to the following attributes, the port will be
210 reconfigured, even if it's opened at that time:
cliechtic1c37602009-07-21 01:34:57 +0000211
212 .. attribute:: port
213
214 Port name/number as set by the user.
215
216 .. attribute:: baudrate
217
218 Current baud rate setting.
219
220 .. attribute:: bytesize
221
222 Byte size in bits.
223
224 .. attribute:: parity
225
226 Parity setting.
227
228 .. attribute:: stopbits
229
230 Stop bit with.
231
232 .. attribute:: timeout
233
cliechti5134aab2009-07-21 19:47:59 +0000234 Timeout setting (seconds, float).
cliechtic1c37602009-07-21 01:34:57 +0000235
236 .. attribute:: xonxoff
237
238 If Xon/Xoff flow control is enabled.
239
240 .. attribute:: rtscts
241
242 If hardware flow control is enabled.
243
244 Platform specific methods.
245
246 .. warning:: Programs using the following methods are not portable to other platforms!
247
248 .. method:: nonblocking()
249
250 :platform: Unix
cliechti86e87872009-07-21 13:32:45 +0000251
cliechtic1c37602009-07-21 01:34:57 +0000252 Configure the device for nonblocking operations. This can be useful if
253 the port is used with ``select``.
254
255 .. method:: fileno()
256
257 :platform: Unix
cliechtibb5c3c52009-07-23 02:29:27 +0000258 :return: File descriptor.
cliechti86e87872009-07-21 13:32:45 +0000259
cliechtibb5c3c52009-07-23 02:29:27 +0000260 Return file descriptor number for the port that is opened by this object.
cliechtic1c37602009-07-21 01:34:57 +0000261
262 .. method:: setXON(level=True)
263
264 :platform: Windows
cliechtibb5c3c52009-07-23 02:29:27 +0000265 :param level: Set flow control state.
cliechti86e87872009-07-21 13:32:45 +0000266
cliechtic1c37602009-07-21 01:34:57 +0000267 Set software flow control state.
268
269
cliechtiedcdbe42009-07-22 00:48:34 +0000270.. class:: SerialBase
271
272 The following attributes are implemented as properties. They work with open
273 and closed ports.
274
275 .. attribute:: port
276
277 Read or write port. When the port is already open, it will be closed
278 and reopened with the new setting.
279
280 .. attribute:: baudrate
281
cliechti6066f842009-07-24 00:05:45 +0000282 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000283
284 .. attribute:: bytesize
285
cliechti6066f842009-07-24 00:05:45 +0000286 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000287
288 .. attribute:: parity
289
cliechti6066f842009-07-24 00:05:45 +0000290 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000291
292 .. attribute:: stopbits
293
cliechti6066f842009-07-24 00:05:45 +0000294 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000295
296 .. attribute:: timeout
297
cliechti6066f842009-07-24 00:05:45 +0000298 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000299
300 .. attribute:: writeTimeout
301
cliechti6066f842009-07-24 00:05:45 +0000302 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000303
304 .. attribute:: xonxoff
305
cliechti6066f842009-07-24 00:05:45 +0000306 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000307
308 .. attribute:: rtscts
309
cliechti6066f842009-07-24 00:05:45 +0000310 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000311
312 .. attribute:: dsrdtr
313
cliechti6066f842009-07-24 00:05:45 +0000314 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000315
316 .. attribute:: interCharTimeout
317
cliechti6066f842009-07-24 00:05:45 +0000318 Read or write current inter character timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000319
320 The following constants are also provided:
321
322 .. attribute:: BAUDRATES
323
324 A tuple of standard baud rate values. The actual device may support more
325 or less...
326
327 .. attribute:: BYTESIZES
328
329 A tuple of supported byte size values.
330
331 .. attribute:: PARITIES
332
333 A tuple of supported parity settings.
334
335 .. attribute:: STOPBITS
336
337 A tuple of supported stop bit settings.
338
cliechti4a567a02009-07-27 22:09:31 +0000339 .. method:: readline(size=None, eol='\\n')
340
341 :param size: Max number of bytes to read, ``None`` -> no limit.
342 :param eol: The end of line character.
343
344 Read a line which is terminated with end-of-line (*eol*) character
345 (``\\n`` by default) or until timeout.
346
347 .. method:: readlines(sizehint=None, eol='\\n')
348
349 :param size: Ignored parameter.
350 :param eol: The end of line character.
351
352 Read a list of lines, until timeout. *sizehint* is ignored and only
353 present for API compatibility with built-in File objects.
354
355 .. method:: xreadlines(sizehint=None)
356
357 Just calls :meth:`readlines` - here for compatibility.
358
359 For compatibility with the :mod:`io` library are the following methods.
360
361 .. method:: readable()
362
363 :return: True
364 .. versionadded:: 2.5
365
366 .. method:: writable()
367
368 :return: True
369 .. versionadded:: 2.5
370
371 .. method:: seekable()
372
373 :return: False
374 .. versionadded:: 2.5
375
376 .. method:: readinto(b)
377
378 :param b: bytearray or array instace
379 :return: Number of byte read
380
381 Read up to len(b) bytes into bytearray b and return the number of bytes read.
382
383 .. versionadded:: 2.5
384
385
386.. note::
387
388 For systems that provide the :mod:`io` library (Python 2.6 and newer), the
389 class :class:`Serial` will derrive from :class:`io.RawIOBase`. For all
390 others from :class:`FileLike`.
391
392.. class:: FileLike
393
394 An abstract file like class. It is used as base class for :class:`Serial`.
395
396 This class implements :meth:`readline` and :meth:`readlines` based on read
397 and :meth:`writelines` based on write. This class is used to provide the
398 above functions for to Serial port objects.
399
400 Note that when the serial port was opened with no timeout that
401 :meth:`readline` blocks until it sees a newline (or the specified size is
402 reached) and that :meth:`readlines` would never return and therefore
403 refuses to work (it raises an exception in this case)!
404
405 .. method:: writelines(sequence)
406
407 Write a list of strings to the port.
408
409
410 The following three methods are overridden in :class:`Serial`.
411
412 .. method:: flush()
413
414 Flush of file like objects. It's a no-op in this class, may be overridden.
415
416 .. method:: read()
417
418 Raises NotImplementedError, needs to be overridden by subclass.
419
420 .. method:: write(data)
421
422 Raises NotImplementedError, needs to be overridden by subclass.
423
424 The following functions are implemented for compatibility with other
425 file-like objects, however serial ports are not seekable.
426
427
428 .. method:: seek(pos, whence=0)
429
430 :exception IOError: always, as method is not supported on serial port
431
432 .. versionadded:: 2.5
433
434 .. method:: tell()
435
436 :exception IOError: always, as method is not supported on serial port
437
438 .. versionadded:: 2.5
439
440 .. method:: truncate(self, n=None)
441
442 :exception IOError: always, as method is not supported on serial port
443
444 .. versionadded:: 2.5
445
446 .. method:: isatty()
447
448 :exception IOError: always, as method is not supported on serial port
449
450 .. versionadded:: 2.5
451
452 To be able to use the file like object as iterator for e.g.
453 ``for line in Serial(0): ...`` usage:
454
455 .. method:: next()
456
457 Return the next line by calling :meth:`readline`.
458
459 .. method:: __iter__()
460
461 Returns self.
462
cliechtiedcdbe42009-07-22 00:48:34 +0000463
464
cliechtic1c37602009-07-21 01:34:57 +0000465Exceptions
466==========
467
468.. exception:: SerialException
469
470 Base class for serial port exceptions.
471
cliechti4a567a02009-07-27 22:09:31 +0000472 .. versionchanged:: 2.5
473 Now derrives from :exc:`IOError` instead of :exc:`Exception`
474
cliechtic1c37602009-07-21 01:34:57 +0000475.. exception:: SerialTimeoutException
476
477 Exception that is raised on write timeouts.
478
479
480Constants
481=========
482
cliechti5134aab2009-07-21 19:47:59 +0000483Parity
cliechtic1c37602009-07-21 01:34:57 +0000484------
485.. data:: PARITY_NONE
486.. data:: PARITY_EVEN
487.. data:: PARITY_ODD
488.. data:: PARITY_MARK
489.. data:: PARITY_SPACE
490
cliechti4a567a02009-07-27 22:09:31 +0000491Stop bits
492---------
cliechtic1c37602009-07-21 01:34:57 +0000493.. data:: STOPBITS_ONE
494.. data:: STOPBITS_ONE_POINT_FIVE
495.. data:: STOPBITS_TWO
496
cliechti4a567a02009-07-27 22:09:31 +0000497Byte size
498---------
cliechtic1c37602009-07-21 01:34:57 +0000499.. data:: FIVEBITS
500.. data:: SIXBITS
501.. data:: SEVENBITS
502.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000503
504Others
505-------
cliechti4a567a02009-07-27 22:09:31 +0000506Default control characters for software flow control:
cliechti6066f842009-07-24 00:05:45 +0000507
cliechti5b7d16a2009-07-21 21:53:59 +0000508.. data:: XON
509.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000510
cliechti4a567a02009-07-27 22:09:31 +0000511Module version:
cliechtif81362e2009-07-25 03:44:33 +0000512
513.. data:: VERSION
514
515 A string indicating the pySerial version, such as ``2.5``.
516
517Functions:
518
519.. function:: device(number)
520
521 :param number: Port number.
522 :return: String containing device name.
523 :deprecated: Use device names directly.
524
525 Convert a port number to a platform dependent device name. Unfortunately
526 this does not work well for all platforms; e.g. some may miss USB-Serial
527 converters and enumerate only internal serial ports.
528
529 The conversion may be made off-line, that is, there is no guarantee that
530 the returned device name really exists on the system.