blob: f6117d1826338c30c3a3b976eeb47f0cbcde54dd [file] [log] [blame]
cliechtic1c37602009-07-21 01:34:57 +00001==============
2 pySerial API
3==============
4
5.. module:: serial
6
7Classes
8=======
9
cliechti7aed8332009-08-05 14:19:31 +000010Native ports
11------------
12
cliechtic1c37602009-07-21 01:34:57 +000013.. class:: Serial
14
cliechti5c72e4d2010-07-21 14:04:54 +000015 .. method:: __init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, writeTimeout=None, dsrdtr=False, interCharTimeout=None)
cliechtic1c37602009-07-21 01:34:57 +000016
17 :param port:
cliechti4a567a02009-07-27 22:09:31 +000018 Device name or port number number or :const:`None`.
cliechtic1c37602009-07-21 01:34:57 +000019
cliechtic1c37602009-07-21 01:34:57 +000020 :param baudrate:
21 Baud rate such as 9600 or 115200 etc.
22
23 :param bytesize:
cliechti4a567a02009-07-27 22:09:31 +000024 Number of data bits. Possible values:
25 :const:`FIVEBITS`, :const:`SIXBITS`, :const:`SEVENBITS`,
26 :const:`EIGHTBITS`
cliechtic1c37602009-07-21 01:34:57 +000027
28 :param parity:
cliechti4a567a02009-07-27 22:09:31 +000029 Enable parity checking. Possible values:
cliechti87686242009-08-18 00:58:31 +000030 :const:`PARITY_NONE`, :const:`PARITY_EVEN`, :const:`PARITY_ODD`
31 :const:`PARITY_MARK`, :const:`PARITY_SPACE`
cliechtic1c37602009-07-21 01:34:57 +000032
33 :param stopbits:
cliechti4a567a02009-07-27 22:09:31 +000034 Number of stop bits. Possible values:
cliechti87686242009-08-18 00:58:31 +000035 :const:`STOPBITS_ONE`, :const:`STOPBITS_ONE_POINT_FIVE`,
cliechti4a567a02009-07-27 22:09:31 +000036 :const:`STOPBITS_TWO`
cliechtic1c37602009-07-21 01:34:57 +000037
38 :param timeout:
39 Set a read timeout value.
40
41 :param xonxoff:
cliechtic1c37602009-07-21 01:34:57 +000042 Enable software flow control.
43
44 :param rtscts:
cliechtic1c37602009-07-21 01:34:57 +000045 Enable hardware (RTS/CTS) flow control.
46
cliechti07709e42010-05-21 00:30:09 +000047 :param dsrdtr:
48 Enable hardware (DSR/DTR) flow control.
49
50 :param writeTimeout:
51 Set a write timeout value.
52
cliechtic1c37602009-07-21 01:34:57 +000053 :param interCharTimeout:
cliechti4a567a02009-07-27 22:09:31 +000054 Inter-character timeout, :const:`None` to disable (default).
cliechtic1c37602009-07-21 01:34:57 +000055
cliechti6066f842009-07-24 00:05:45 +000056 :exception ValueError:
cliechti4a567a02009-07-27 22:09:31 +000057 Will be raised when parameter are out of range, e.g. baud rate, data bits.
cliechti6066f842009-07-24 00:05:45 +000058
59 :exception SerialException:
60 In case the device can not be found or can not be configured.
61
62
cliechti4a567a02009-07-27 22:09:31 +000063 The port is immediately opened on object creation, when a *port* is
64 given. It is not opened when *port* is :const:`None` and a successive call
65 to :meth:`open` will be needed.
66
67 Possible values for the parameter *port*:
cliechtic1c37602009-07-21 01:34:57 +000068
cliechti5134aab2009-07-21 19:47:59 +000069 - Number: number of device, numbering starts at zero.
70 - Device name: depending on operating system. e.g. ``/dev/ttyUSB0``
71 on GNU/Linux or ``COM3`` on Windows.
cliechtic1c37602009-07-21 01:34:57 +000072
cliechti25375b52010-07-21 23:27:13 +000073 The parameter *baudrate* can be one of the standard values:
74 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
75 9600, 19200, 38400, 57600, 115200.
cliechti28b8fd02011-12-28 21:39:42 +000076 These are well supported on all platforms.
77
78 Standard values above 115200, such as: 230400, 460800, 500000, 576000,
79 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000,
80 4000000 also work on many platforms and devices.
cliechti25375b52010-07-21 23:27:13 +000081
cliechti06281be2011-08-25 23:08:29 +000082 Non-standard values are also supported on some platforms (GNU/Linux, MAC
cliechti25375b52010-07-21 23:27:13 +000083 OSX >= Tiger, Windows). Though, even on these platforms some serial
84 ports may reject non-standard values.
85
cliechti4a567a02009-07-27 22:09:31 +000086 Possible values for the parameter *timeout*:
cliechti5134aab2009-07-21 19:47:59 +000087
cliechtibb5c3c52009-07-23 02:29:27 +000088 - ``timeout = None``: wait forever
89 - ``timeout = 0``: non-blocking mode (return immediately on read)
cliechtif81362e2009-07-25 03:44:33 +000090 - ``timeout = x``: set timeout to ``x`` seconds (float allowed)
cliechtic1c37602009-07-21 01:34:57 +000091
cliechti25375b52010-07-21 23:27:13 +000092 Writes are blocking by default, unless *writeTimeout* is set. For
93 possible values refer to the list for *timeout* above.
cliechti07709e42010-05-21 00:30:09 +000094
cliechti8611bf42009-08-03 00:34:03 +000095 Note that enabling both flow control methods (*xonxoff* and *rtscts*)
96 together may not be supported. It is common to use one of the methods
97 at once, not both.
cliechtic1c37602009-07-21 01:34:57 +000098
cliechti07709e42010-05-21 00:30:09 +000099 *dsrdtr* is not supported by all platforms (silently ignored). Setting
100 it to ``None`` has the effect that its state follows *rtscts*.
101
cliechti25375b52010-07-21 23:27:13 +0000102 Also consider using the function :func:`serial_for_url` instead of
103 creating Serial instances directly.
104
cliechti5c72e4d2010-07-21 14:04:54 +0000105 .. versionchanged:: 2.5
cliechti06281be2011-08-25 23:08:29 +0000106 *dsrdtr* now defaults to ``False`` (instead of *None*)
cliechti5c72e4d2010-07-21 14:04:54 +0000107
cliechtic1c37602009-07-21 01:34:57 +0000108 .. method:: open()
109
110 Open port.
111
112 .. method:: close()
113
114 Close port immediately.
115
cliechti25375b52010-07-21 23:27:13 +0000116 .. method:: __del__()
117
118 Destructor, close port when serial port instance is freed.
119
cliechtic1c37602009-07-21 01:34:57 +0000120
cliechti4a567a02009-07-27 22:09:31 +0000121 The following methods may raise :exc:`ValueError` when applied to a closed
122 port.
cliechtic1c37602009-07-21 01:34:57 +0000123
124 .. method:: read(size=1)
125
cliechtibb5c3c52009-07-23 02:29:27 +0000126 :param size: Number of bytes to read.
127 :return: Bytes read from the port.
cliechtic1c37602009-07-21 01:34:57 +0000128
cliechti4a567a02009-07-27 22:09:31 +0000129 Read *size* bytes from the serial port. If a timeout is set it may
cliechtibb5c3c52009-07-23 02:29:27 +0000130 return less characters as requested. With no timeout it will block
131 until the requested number of bytes is read.
cliechtic1c37602009-07-21 01:34:57 +0000132
cliechti4a567a02009-07-27 22:09:31 +0000133 .. versionchanged:: 2.5
134 Returns an instance of :class:`bytes` when available (Python 2.6
cliechti8611bf42009-08-03 00:34:03 +0000135 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000136
cliechtibb5c3c52009-07-23 02:29:27 +0000137 .. method:: write(data)
138
139 :param data: Data to send.
cliechti4a567a02009-07-27 22:09:31 +0000140 :return: Number of bytes written.
cliechti6066f842009-07-24 00:05:45 +0000141 :exception SerialTimeoutException:
142 In case a write timeout is configured for the port and the time is
143 exceeded.
144
cliechti4a567a02009-07-27 22:09:31 +0000145 Write the string *data* to the port.
cliechtic1c37602009-07-21 01:34:57 +0000146
cliechti4a567a02009-07-27 22:09:31 +0000147 .. versionchanged:: 2.5
cliechtiddd78132009-07-28 01:13:28 +0000148 Accepts instances of :class:`bytes` and :class:`bytearray` when
cliechti8611bf42009-08-03 00:34:03 +0000149 available (Python 2.6 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000150
cliechti9a1c6952009-10-20 22:18:28 +0000151 .. versionchanged:: 2.5
152 Write returned ``None`` in previous versions.
153
cliechti4a567a02009-07-27 22:09:31 +0000154 .. method:: inWaiting()
155
156 Return the number of chars in the receive buffer.
157
158 .. method:: flush()
cliechtic1c37602009-07-21 01:34:57 +0000159
160 Flush of file like objects. In this case, wait until all data is
161 written.
162
163 .. method:: flushInput()
164
165 Flush input buffer, discarding all it's contents.
166
167 .. method:: flushOutput()
168
169 Clear output buffer, aborting the current output and
170 discarding all that is in the buffer.
171
172 .. method:: sendBreak(duration=0.25)
173
cliechtibb5c3c52009-07-23 02:29:27 +0000174 :param duration: Time (float) to activate the BREAK condition.
175
cliechtic1c37602009-07-21 01:34:57 +0000176 Send break condition. Timed, returns to idle state after given
177 duration.
178
179 .. method:: setBreak(level=True)
180
cliechtibb5c3c52009-07-23 02:29:27 +0000181 :param level: when true activate BREAK condition, else disable.
182
cliechtic1c37602009-07-21 01:34:57 +0000183 Set break: Controls TXD. When active, no transmitting is possible.
184
185 .. method:: setRTS(level=True)
186
cliechtibb5c3c52009-07-23 02:29:27 +0000187 :param level: Set control line to logic level.
188
cliechtic1c37602009-07-21 01:34:57 +0000189 Set RTS line to specified logic level.
190
191 .. method:: setDTR(level=True)
192
cliechtibb5c3c52009-07-23 02:29:27 +0000193 :param level: Set control line to logic level.
194
cliechtic1c37602009-07-21 01:34:57 +0000195 Set DTR line to specified logic level.
196
197 .. method:: getCTS()
198
cliechtibb5c3c52009-07-23 02:29:27 +0000199 :return: Current state (boolean)
200
cliechtic1c37602009-07-21 01:34:57 +0000201 Return the state of the CTS line.
202
203 .. method:: getDSR()
204
cliechtibb5c3c52009-07-23 02:29:27 +0000205 :return: Current state (boolean)
206
cliechtic1c37602009-07-21 01:34:57 +0000207 Return the state of the DSR line.
208
209 .. method:: getRI()
210
cliechtibb5c3c52009-07-23 02:29:27 +0000211 :return: Current state (boolean)
212
cliechtic1c37602009-07-21 01:34:57 +0000213 Return the state of the RI line.
214
215 .. method:: getCD()
216
cliechtibb5c3c52009-07-23 02:29:27 +0000217 :return: Current state (boolean)
218
cliechtic1c37602009-07-21 01:34:57 +0000219 Return the state of the CD line
220
221 Read-only attributes:
222
cliechtif81362e2009-07-25 03:44:33 +0000223 .. attribute:: name
cliechtic1c37602009-07-21 01:34:57 +0000224
cliechti5134aab2009-07-21 19:47:59 +0000225 Device name. This is always the device name even if the
226 port was opened by a number. (Read Only).
cliechtic1c37602009-07-21 01:34:57 +0000227
cliechtif81362e2009-07-25 03:44:33 +0000228 .. versionadded:: 2.5
229
230 .. attribute:: portstr
231
232 :deprecated: use :attr:`name` instead
233
cliechti25375b52010-07-21 23:27:13 +0000234 New values can be assigned to the following attributes (properties), the
235 port will be reconfigured, even if it's opened at that time:
cliechtic1c37602009-07-21 01:34:57 +0000236
cliechtiedcdbe42009-07-22 00:48:34 +0000237
238 .. attribute:: port
239
240 Read or write port. When the port is already open, it will be closed
241 and reopened with the new setting.
242
243 .. attribute:: baudrate
244
cliechti6066f842009-07-24 00:05:45 +0000245 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000246
247 .. attribute:: bytesize
248
cliechti6066f842009-07-24 00:05:45 +0000249 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000250
251 .. attribute:: parity
252
cliechti6066f842009-07-24 00:05:45 +0000253 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000254
255 .. attribute:: stopbits
256
cliechti6066f842009-07-24 00:05:45 +0000257 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000258
259 .. attribute:: timeout
260
cliechti6066f842009-07-24 00:05:45 +0000261 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000262
263 .. attribute:: writeTimeout
264
cliechti6066f842009-07-24 00:05:45 +0000265 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000266
267 .. attribute:: xonxoff
268
cliechti6066f842009-07-24 00:05:45 +0000269 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000270
271 .. attribute:: rtscts
272
cliechti6066f842009-07-24 00:05:45 +0000273 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000274
275 .. attribute:: dsrdtr
276
cliechti6066f842009-07-24 00:05:45 +0000277 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000278
279 .. attribute:: interCharTimeout
280
cliechti6066f842009-07-24 00:05:45 +0000281 Read or write current inter character timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000282
283 The following constants are also provided:
284
285 .. attribute:: BAUDRATES
286
cliechti25375b52010-07-21 23:27:13 +0000287 A list of valid baud rates. The list may be incomplete such that higher
288 baud rates may be supported by the device and that values in between the
289 standard baud rates are supported. (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000290
291 .. attribute:: BYTESIZES
292
cliechti25375b52010-07-21 23:27:13 +0000293 A list of valid byte sizes for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000294
295 .. attribute:: PARITIES
296
cliechti25375b52010-07-21 23:27:13 +0000297 A list of valid parities for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000298
299 .. attribute:: STOPBITS
300
cliechti25375b52010-07-21 23:27:13 +0000301 A list of valid stop bit widths for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000302
cliechti4a567a02009-07-27 22:09:31 +0000303
cliechti25375b52010-07-21 23:27:13 +0000304 The following methods are for compatibility with the :mod:`io` library.
cliechti4a567a02009-07-27 22:09:31 +0000305
306 .. method:: readable()
307
308 :return: True
cliechtif4566342009-08-04 00:07:19 +0000309
cliechti4a567a02009-07-27 22:09:31 +0000310 .. versionadded:: 2.5
311
312 .. method:: writable()
313
314 :return: True
cliechtif4566342009-08-04 00:07:19 +0000315
cliechti4a567a02009-07-27 22:09:31 +0000316 .. versionadded:: 2.5
317
318 .. method:: seekable()
319
320 :return: False
cliechtif4566342009-08-04 00:07:19 +0000321
cliechti4a567a02009-07-27 22:09:31 +0000322 .. versionadded:: 2.5
323
324 .. method:: readinto(b)
325
cliechti8611bf42009-08-03 00:34:03 +0000326 :param b: bytearray or array instance
cliechti4a567a02009-07-27 22:09:31 +0000327 :return: Number of byte read
328
cliechtid7e7ce22009-08-03 02:01:07 +0000329 Read up to len(b) bytes into :class:`bytearray` *b* and return the
330 number of bytes read.
cliechti4a567a02009-07-27 22:09:31 +0000331
332 .. versionadded:: 2.5
333
cliechti25375b52010-07-21 23:27:13 +0000334 The port settings can be read and written as dictionary.
335
cliechti7053b232009-08-10 01:37:34 +0000336 .. method:: getSettingsDict()
cliechti4065dce2009-08-10 00:55:46 +0000337
338 :return: a dictionary with current port settings.
339
340 Get a dictionary with port settings. This is useful to backup the
341 current settings so that a later point in time they can be restored
342 using :meth:`applySettingsDict`.
343
344 Note that control lines (RTS/DTR) are part of the settings.
345
346 .. versionadded:: 2.5
347
cliechti7053b232009-08-10 01:37:34 +0000348 .. method:: applySettingsDict(d)
cliechti4065dce2009-08-10 00:55:46 +0000349
350 :param d: a dictionary with port settings.
351
352 Applies a dictionary that was created by :meth:`getSettingsDict`. Only
353 changes are applied and when a key is missing it means that the setting
354 stays unchanged.
355
356 Note that control lines (RTS/DTR) are not changed.
357
358 .. versionadded:: 2.5
359
cliechti25375b52010-07-21 23:27:13 +0000360 Platform specific methods.
361
cliechtic648da92011-12-29 01:17:51 +0000362 .. warning:: Programs using the following methods and attributes are not
363 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000364
cliechti28b8fd02011-12-28 21:39:42 +0000365 .. method:: outWaiting()
366
367 :platform: Unix
368 :platform: Windows
369
370 Return the number of bytes in the output buffer.
371
372 .. versionchanged:: 2.7 (Posix support added)
373
cliechti25375b52010-07-21 23:27:13 +0000374 .. method:: nonblocking()
375
376 :platform: Unix
377
378 Configure the device for nonblocking operation. This can be useful if
379 the port is used with :mod:`select`.
380
381 .. method:: fileno()
382
383 :platform: Unix
384 :return: File descriptor.
385
386 Return file descriptor number for the port that is opened by this object.
387 It is useful when serial ports are used with :mod:`select`.
388
389 .. method:: setXON(level=True)
390
391 :platform: Windows
cliechti2f0f8a32011-12-28 22:10:00 +0000392 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000393 :param level: Set flow control state.
394
cliechti2f0f8a32011-12-28 22:10:00 +0000395 Manually control flow - when software flow control is enabled.
396
397 This will send XON (true) and XOFF (false) to the other device.
398
399 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
400
401 .. method:: flowControlOut(enable)
402
403 :platform: Posix
404 :param enable: Set flow control state.
405
406 Manually control flow of outgoing data - when hardware or software flow
407 control is enabled.
408
409 Sending will be suspended when called with ``False`` and enabled when
410 called with ``True``.
411
412 .. versionadded:: 2.7 (Posix support added)
cliechtif4566342009-08-04 00:07:19 +0000413
cliechtic648da92011-12-29 01:17:51 +0000414 .. attribute:: rtsToggle
415
416 :platform: Windows
417
418 Attribute to configure RTS toggle control setting. When enabled and
419 supported by OS, RTS will be active when data is available and inactive
420 if no data is available.
421
422 .. versionadded:: 2.6
423
424
cliechti4a567a02009-07-27 22:09:31 +0000425.. note::
426
cliechtic56e41d2011-08-25 22:06:38 +0000427 For systems that provide the :py:mod:`io` library (Python 2.6 and newer), the
428 class :class:`Serial` will derive from :py:class:`io.RawIOBase`. For all
cliechti4a567a02009-07-27 22:09:31 +0000429 others from :class:`FileLike`.
430
cliechtic56e41d2011-08-25 22:06:38 +0000431Implementation detail: some attributes and functions are provided by the
432class :class:`SerialBase` and some by the platform specific class and
433others by the base class mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000434
cliechti4a567a02009-07-27 22:09:31 +0000435.. class:: FileLike
436
cliechti25375b52010-07-21 23:27:13 +0000437 An abstract file like class. It is used as base class for :class:`Serial`
cliechtic56e41d2011-08-25 22:06:38 +0000438 when no :py:mod:`io` module is available.
cliechti4a567a02009-07-27 22:09:31 +0000439
cliechti25375b52010-07-21 23:27:13 +0000440 This class implements :meth:`readline` and :meth:`readlines` based on
441 :meth:`read` and :meth:`writelines` based on :meth:`write`.
cliechti4a567a02009-07-27 22:09:31 +0000442
cliechti25375b52010-07-21 23:27:13 +0000443 Note that when the serial port was opened with no timeout, that
cliechti4a567a02009-07-27 22:09:31 +0000444 :meth:`readline` blocks until it sees a newline (or the specified size is
445 reached) and that :meth:`readlines` would never return and therefore
446 refuses to work (it raises an exception in this case)!
447
448 .. method:: writelines(sequence)
449
450 Write a list of strings to the port.
451
452
453 The following three methods are overridden in :class:`Serial`.
454
455 .. method:: flush()
456
457 Flush of file like objects. It's a no-op in this class, may be overridden.
458
459 .. method:: read()
460
461 Raises NotImplementedError, needs to be overridden by subclass.
462
463 .. method:: write(data)
464
465 Raises NotImplementedError, needs to be overridden by subclass.
466
467 The following functions are implemented for compatibility with other
468 file-like objects, however serial ports are not seekable.
469
470
471 .. method:: seek(pos, whence=0)
472
473 :exception IOError: always, as method is not supported on serial port
474
475 .. versionadded:: 2.5
476
477 .. method:: tell()
478
479 :exception IOError: always, as method is not supported on serial port
480
481 .. versionadded:: 2.5
482
483 .. method:: truncate(self, n=None)
484
485 :exception IOError: always, as method is not supported on serial port
486
487 .. versionadded:: 2.5
488
489 .. method:: isatty()
490
491 :exception IOError: always, as method is not supported on serial port
492
493 .. versionadded:: 2.5
494
cliechti25375b52010-07-21 23:27:13 +0000495 To be able to use the file like object as iterator for e.g.
cliechti4a567a02009-07-27 22:09:31 +0000496 ``for line in Serial(0): ...`` usage:
497
498 .. method:: next()
499
500 Return the next line by calling :meth:`readline`.
501
502 .. method:: __iter__()
503
504 Returns self.
505
cliechti25375b52010-07-21 23:27:13 +0000506 Other high level access functions.
507
cliechtie214ff82010-07-21 15:48:47 +0000508 .. method:: readline(size=None, eol='\\n')
509
510 :param size: Max number of bytes to read, ``None`` -> no limit.
511 :param eol: The end of line character.
512
513 Read a line which is terminated with end-of-line (*eol*) character
cliechti25375b52010-07-21 23:27:13 +0000514 (``\n`` by default) or until timeout.
cliechtie214ff82010-07-21 15:48:47 +0000515
516 .. method:: readlines(sizehint=None, eol='\\n')
517
cliechtic56e41d2011-08-25 22:06:38 +0000518 :param sizehint: Ignored parameter.
cliechtie214ff82010-07-21 15:48:47 +0000519 :param eol: The end of line character.
520
521 Read a list of lines, until timeout. *sizehint* is ignored and only
522 present for API compatibility with built-in File objects.
523
524 Note that this function only returns on a timeout.
525
526 .. method:: xreadlines(sizehint=None)
527
528 Read lines, implemented as generator. Unlike *readlines* (that only
cliechti25375b52010-07-21 23:27:13 +0000529 returns on a timeout) is this function yielding lines as they are
530 received.
cliechtie214ff82010-07-21 15:48:47 +0000531
532 .. deprecated:: 2.5
cliechti25375b52010-07-21 23:27:13 +0000533 Use ``for line in Serial(...): ...`` instead. This method is not
534 available in Python 2.6 and newer where the :mod:`io` library is
535 available and pySerial bases on it.
cliechtie214ff82010-07-21 15:48:47 +0000536
537 .. versionchanged:: 2.5
538 Implement as generator.
539
cliechti25375b52010-07-21 23:27:13 +0000540
cliechti7aed8332009-08-05 14:19:31 +0000541:rfc:`2217` Network ports
542-------------------------
543
544.. warning:: This implementation is currently in an experimental state. Use
545 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000546
cliechtiec4ac1b2009-08-02 00:47:21 +0000547.. class:: rfc2217.Serial
548
cliechtif4566342009-08-04 00:07:19 +0000549 This implements a :rfc:`2217` compatible client. Port names are URLs_ in the
550 form: ``rfc2217://<host>:<port>[/<option>[/<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000551
cliechtiec4ac1b2009-08-02 00:47:21 +0000552 This class API is compatible to :class:`Serial` with a few exceptions:
553
554 - numbers as port name are not allowed, only URLs in the form described
555 above.
556 - writeTimeout is not implemented
557 - The current implementation starts a thread that keeps reading from the
558 (internal) socket. The thread is managed automatically by the
559 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
560 However it may be a problem for user applications that like to use select
561 instead of threads.
562
563 Due to the nature of the network and protocol involved there are a few
564 extra points to keep in mind:
565
566 - All operations have an additional latency time.
567 - Setting control lines (RTS/CTS) needs more time.
568 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
569 cache is updated depends entirely on the server. The server itself may
570 implement a polling at a certain rate and quick changes may be invisible.
571 - The network layer also has buffers. This means that :meth:`flush`,
572 :meth:`flushInput` and :meth:`flushOutput` may work with additional delay.
573 Likewise :meth:`inWaiting` returns the size of the data arrived at the
574 object internal buffer and excludes any bytes in the network buffers or
575 any server side buffer.
576 - Closing and immediately reopening the same port may fail due to time
577 needed by the server to get ready again.
578
579 Not implemented yet / Possible problems with the implementation:
580
cliechti8611bf42009-08-03 00:34:03 +0000581 - :rfc:`2217` flow control between client and server (objects internal
582 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000583 - No authentication support (servers may not prompt for a password etc.)
584 - No encryption.
585
586 Due to lack of authentication and encryption it is not suitable to use this
587 client for connections across the internet and should only be used in
588 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000589
cliechti7c640ed2009-08-02 00:54:51 +0000590 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000591
cliechti7aed8332009-08-05 14:19:31 +0000592
593.. class:: rfc2217.PortManager
594
595 This class provides helper functions for implementing :rfc:`2217`
596 compatible servers.
597
598 Basically, it implements every thing needed for the :rfc:`2217` protocol.
599 It just does not open sockets and read/write to serial ports (though it
600 changes other port settings). The user of this class must take care of the
601 data transmission itself. The reason for that is, that this way, this class
602 supports all programming models such as threads and select.
603
604 Usage examples can be found in the examples where two TCP/IP - serial
605 converters are shown, one using threads (the single port server) and an
606 other using select (the multi port server).
607
608 .. note:: Each new client connection must create a new instance as this
609 object (and the :rfc:`2217` protocol) has internal state.
610
611 .. method:: __init__(serial_port, connection, debug_output=False)
612
613 :param serial_port: a :class:`Serial` instance that is managed.
614 :param connection: an object implementing :meth:`write`, used to write
615 to the network.
cliechti86844e82009-08-12 00:05:33 +0000616 :param debug_output: enables debug messages: a :class:`logging.Logger`
617 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000618
619 Initializes the Manager and starts negotiating with client in Telnet
620 and :rfc:`2217` protocol. The negotiation starts immediately so that
621 the class should be instantiated in the moment the client connects.
622
623 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000624 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000625 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
626 the :meth:`filter` method.
627
628 The *connection* object must implement a :meth:`write(data)` function.
629 This function must ensure that *data* is written at once (no user data
630 mixed in, i.e. it must be thread-safe). All data must be sent in its
631 raw form (:meth:`escape` must not be used) as it is used to send Telnet
632 and :rfc:`2217` control commands.
633
cliechti86844e82009-08-12 00:05:33 +0000634 For diagnostics of the connection or the implementation, *debug_output*
635 can be set to an instance of a :class:`logging.Logger` (e.g.
636 ``logging.getLogger('rfc2217.server')``). The caller should configure
637 the logger using ``setLevel`` for the desired detail level of the logs.
638
cliechti7aed8332009-08-05 14:19:31 +0000639 .. method:: escape(data)
640
641 :param data: data to be sent over the network.
642 :return: data, escaped for Telnet/:rfc:`2217`
643
644 A generator that escapes all data to be compatible with :rfc:`2217`.
645 Implementors of servers should use this function to process all data
646 sent over the network.
647
648 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000649 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000650
651 .. method:: filter(data)
652
653 :param data: data read from the network, including Telnet and
654 :rfc:`2217` controls.
655 :return: data, free from Telnet and :rfc:`2217` controls.
656
657 A generator that filters and processes all data related to :rfc:`2217`.
658 Implementors of servers should use this function to process all data
659 received from the network.
660
661 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000662 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000663
cliechti7cb78e82009-08-05 15:47:57 +0000664 .. method:: check_modem_lines(force_notification=False)
665
666 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000667
668 This function needs to be called periodically (e.g. every second) when
669 the server wants to send NOTIFY_MODEMSTATE messages. This is required
670 to support the client for reading CTS/DSR/RI/CD status lines.
671
672 The function reads the status line and issues the notifications
673 automatically.
674
675 .. versionadded:: 2.5
676
cliechtiec4ac1b2009-08-02 00:47:21 +0000677.. seealso::
678
679 :rfc:`2217` - Telnet Com Port Control Option
680
681
cliechtic1c37602009-07-21 01:34:57 +0000682Exceptions
683==========
684
685.. exception:: SerialException
686
687 Base class for serial port exceptions.
688
cliechti4a567a02009-07-27 22:09:31 +0000689 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000690 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000691
cliechtic1c37602009-07-21 01:34:57 +0000692.. exception:: SerialTimeoutException
693
694 Exception that is raised on write timeouts.
695
696
697Constants
698=========
699
cliechti87686242009-08-18 00:58:31 +0000700*Parity*
701
cliechtic1c37602009-07-21 01:34:57 +0000702.. data:: PARITY_NONE
703.. data:: PARITY_EVEN
704.. data:: PARITY_ODD
705.. data:: PARITY_MARK
706.. data:: PARITY_SPACE
707
cliechti87686242009-08-18 00:58:31 +0000708*Stop bits*
709
cliechtic1c37602009-07-21 01:34:57 +0000710.. data:: STOPBITS_ONE
711.. data:: STOPBITS_ONE_POINT_FIVE
712.. data:: STOPBITS_TWO
713
cliechti06281be2011-08-25 23:08:29 +0000714Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +0000715bits.
716
cliechti87686242009-08-18 00:58:31 +0000717*Byte size*
718
cliechtic1c37602009-07-21 01:34:57 +0000719.. data:: FIVEBITS
720.. data:: SIXBITS
721.. data:: SEVENBITS
722.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000723
cliechti87686242009-08-18 00:58:31 +0000724
725*Others*
726
cliechti8611bf42009-08-03 00:34:03 +0000727Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000728software flow control:
cliechti6066f842009-07-24 00:05:45 +0000729
cliechti5b7d16a2009-07-21 21:53:59 +0000730.. data:: XON
731.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000732
cliechti4a567a02009-07-27 22:09:31 +0000733Module version:
cliechtif81362e2009-07-25 03:44:33 +0000734
735.. data:: VERSION
736
737 A string indicating the pySerial version, such as ``2.5``.
738
cliechti87686242009-08-18 00:58:31 +0000739 .. versionadded:: 2.3
740
cliechti44eb98f2011-03-21 21:41:10 +0000741
cliechtie542b362011-03-18 00:49:16 +0000742Module functions and attributes
743===============================
cliechtif81362e2009-07-25 03:44:33 +0000744
745.. function:: device(number)
746
747 :param number: Port number.
748 :return: String containing device name.
749 :deprecated: Use device names directly.
750
751 Convert a port number to a platform dependent device name. Unfortunately
752 this does not work well for all platforms; e.g. some may miss USB-Serial
753 converters and enumerate only internal serial ports.
754
755 The conversion may be made off-line, that is, there is no guarantee that
756 the returned device name really exists on the system.
cliechti7c640ed2009-08-02 00:54:51 +0000757
758
cliechtie3ab3532009-08-05 12:40:38 +0000759.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +0000760
cliechti86844e82009-08-12 00:05:33 +0000761 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +0000762 :param do_not_open: When set to true, the serial port is not opened.
763 :return: an instance of :class:`Serial` or a compatible object.
764
765 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +0000766 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +0000767 to support both, local ports and remote ports. There is also support
768 for other types, see :ref:`URL <URLs>` section below.
cliechti7c640ed2009-08-02 00:54:51 +0000769
cliechtid7e7ce22009-08-03 02:01:07 +0000770 The port is not opened when a keyword parameter called *do_not_open* is
771 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +0000772
773 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +0000774
cliechti87686242009-08-18 00:58:31 +0000775
cliechtie542b362011-03-18 00:49:16 +0000776.. attribute:: protocol_handler_packages
777
778 This attribute is a list of package names (strings) that is searched for
779 protocol handlers.
780
781 e.g. we want to support a URL ``foobar://``. A module
782 ``my_handlers.protocol_foobar`` is provided by the user::
783
784 serial.protocol_handler_packages.append("my_handlers")
785 s = serial.serial_for_url("foobar://")
786
787 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
788 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
789 ``PACKAGE`` from this list.
790
791 .. versionadded:: 2.6
792
793
cliechti25375b52010-07-21 23:27:13 +0000794.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +0000795
796 :param sequence: String or list of integers
cliechti25375b52010-07-21 23:27:13 +0000797 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +0000798
cliechtie542b362011-03-18 00:49:16 +0000799 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +0000800 compatible to Python 2.x and 3.x.
801
cliechtie542b362011-03-18 00:49:16 +0000802 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +0000803 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +0000804 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +0000805
806 This function is used internally and in the unit tests.
807
cliechtie542b362011-03-18 00:49:16 +0000808 .. versionadded:: 2.5
809
cliechtie214ff82010-07-21 15:48:47 +0000810
cliechti86844e82009-08-12 00:05:33 +0000811.. _URLs:
cliechtif4566342009-08-04 00:07:19 +0000812
813URLs
814----
cliechtie214ff82010-07-21 15:48:47 +0000815The function :func:`serial_for_url` accepts the following types of URLs:
cliechtif4566342009-08-04 00:07:19 +0000816
cliechtiab90e072009-08-06 01:44:34 +0000817- ``rfc2217://<host>:<port>[/<option>[/<option>]]``
818- ``socket://<host>:<port>[/<option>[/<option>]]``
cliechti41973a92009-08-06 02:18:21 +0000819- ``loop://[<option>[/<option>]]``
cliechtif4566342009-08-04 00:07:19 +0000820
cliechtie214ff82010-07-21 15:48:47 +0000821Device names are also supported, e.g.:
cliechti87686242009-08-18 00:58:31 +0000822
823- ``/dev/ttyUSB0`` (Linux)
824- ``COM3`` (Windows)
825
cliechtie542b362011-03-18 00:49:16 +0000826Future releases of pySerial might add more types. Since pySerial 2.6 it is also
827possible for the user to add protocol handlers using
828:attr:`protocol_handler_packages`.
cliechtif4566342009-08-04 00:07:19 +0000829
cliechtiab90e072009-08-06 01:44:34 +0000830``rfc2217://``
831 Used to connect to :rfc:`2217` compatible servers. All serial port
cliechti25375b52010-07-21 23:27:13 +0000832 functions are supported. Implemented by :class:`rfc2217.Serial`.
cliechtiab90e072009-08-06 01:44:34 +0000833
834 Supported options in the URL are:
835
836 - ``ign_set_control`` does not wait for acknowledges to SET_CONTROL. This
837 option can be used for non compliant servers (i.e. when getting an
838 ``remote rejected value for option 'control'`` error when connecting).
839
840 - ``poll_modem``: The client issues NOTIFY_MODEMSTATE requests when status
841 lines are read (CTS/DTR/RI/CD). Without this option it relies on the server
842 sending the notifications automatically (that's what the RFC suggests and
843 most servers do). Enable this option when :meth:`getCTS` does not work as
844 expected, i.e. for servers that do not send notifications.
845
cliechtidfe2d272009-08-10 22:19:41 +0000846 - ``timeout=<value>``: Change network timeout (default 3 seconds). This is
847 useful when the server takes a little more time to send its answers. The
848 timeout applies to the initial Telnet / :rfc:`2271` negotiation as well
849 as changing port settings or control line change commands.
850
cliechti86844e82009-08-12 00:05:33 +0000851 - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not
cliechtic64ba692009-08-12 00:32:47 +0000852 useful for end users). It uses the logging module and a logger called
853 ``pySerial.rfc2217`` so that the application can setup up logging
854 handlers etc. It will call :meth:`logging.basicConfig` which initializes
855 for output on ``sys.stderr`` (if no logging was set up already).
cliechtiab90e072009-08-06 01:44:34 +0000856
857``socket://``
858 The purpose of this connection type is that applications using pySerial can
859 connect to TCP/IP to serial port converters that do not support :rfc:`2217`.
860
861 Uses a TCP/IP socket. All serial port settings, control and status lines
862 are ignored. Only data is transmitted and received.
863
864 Supported options in the URL are:
865
cliechtic64ba692009-08-12 00:32:47 +0000866 - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not
867 useful for end users). It uses the logging module and a logger called
868 ``pySerial.socket`` so that the application can setup up logging handlers
869 etc. It will call :meth:`logging.basicConfig` which initializes for
870 output on ``sys.stderr`` (if no logging was set up already).
cliechtiab90e072009-08-06 01:44:34 +0000871
cliechti41973a92009-08-06 02:18:21 +0000872``loop://``
cliechtie214ff82010-07-21 15:48:47 +0000873 The least useful type. It simulates a loop back connection
874 (``RX<->TX`` ``RTS<->CTS`` ``DTR<->DSR``). It could be used to test
875 applications or run the unit tests.
cliechti41973a92009-08-06 02:18:21 +0000876
877 Supported options in the URL are:
878
cliechtic64ba692009-08-12 00:32:47 +0000879 - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not
880 useful for end users). It uses the logging module and a logger called
881 ``pySerial.loop`` so that the application can setup up logging handlers
882 etc. It will call :meth:`logging.basicConfig` which initializes for
883 output on ``sys.stderr`` (if no logging was set up already).
cliechti41973a92009-08-06 02:18:21 +0000884
cliechticdc660c2011-11-03 23:24:31 +0000885``hwgrep://``
886 This type uses :mod:`serial.tools.list_ports` to obtain a list of ports and
887 searches the list for matches by a regexp (see :py:mod:`re`) that follows
888 the slashes.
889
890 Depending on the capabilities of the list_ports module on the system, it is
891 possible to search for the description or hardware ID of a device, e.g. USB
892 VID:PID or texts.
893
894 Unfortunately, on some systems list_ports only lists a subset of the port
895 names with no additional information. Currently, on Windows and Linux it
896 should find additional information.
897
898
cliechti41973a92009-08-06 02:18:21 +0000899
cliechtidfe2d272009-08-10 22:19:41 +0000900Examples:
cliechtif4566342009-08-04 00:07:19 +0000901
cliechtidfe2d272009-08-10 22:19:41 +0000902- ``rfc2217://localhost:7000``
903- ``rfc2217://localhost:7000/poll_modem``
904- ``rfc2217://localhost:7000/ign_set_control/timeout=5.5``
905- ``socket://localhost:7777``
cliechtic64ba692009-08-12 00:32:47 +0000906- ``loop://logging=debug``
cliechticdc660c2011-11-03 23:24:31 +0000907- ``hwgrep://0451:f432`` (USB VID:PID)
cliechtic56e41d2011-08-25 22:06:38 +0000908
909Tools
910=====
911
912
913serial.tools.list_ports
914-----------------------
915.. module:: serial.tools.list_ports
916.. versionadded:: 2.6
917
cliechti06281be2011-08-25 23:08:29 +0000918This module can be executed to get a list of ports (``python -m
919serial.tools.list_ports``). It also contains the following functions.
cliechtic56e41d2011-08-25 22:06:38 +0000920
921
922.. function:: comports()
923
924 :return: an iterable.
925
926 The function returns an iterable that yields tuples of three strings:
927
cliechti06281be2011-08-25 23:08:29 +0000928 - port name as it can be passed to :class:`serial.Serial` or
cliechtic56e41d2011-08-25 22:06:38 +0000929 :func:`serial.serial_for_url`
cliechti06281be2011-08-25 23:08:29 +0000930 - description in human readable form
931 - sort of hardware ID. E.g. may contain VID:PID of USB-serial adapters.
cliechtic56e41d2011-08-25 22:06:38 +0000932
cliechti06281be2011-08-25 23:08:29 +0000933 Items are returned in no particular order. It may make sense to sort the
934 items. Also note that the reported strings are different across platforms
935 and operating systems, even for the same device.
cliechtic56e41d2011-08-25 22:06:38 +0000936
937 .. note:: Support is limited to a number of operating systems. On some
cliechti06281be2011-08-25 23:08:29 +0000938 systems description and hardware ID will not be available
cliechtic56e41d2011-08-25 22:06:38 +0000939 (``None``).
940
941 :platform: Posix (/dev files)
942 :platform: Linux (/dev files, sysfs and lsusb)
cliechti4cb94662013-10-17 03:17:50 +0000943 :platform: OSX (iokit)
cliechtic56e41d2011-08-25 22:06:38 +0000944 :platform: Windows (setupapi, registry)
945
946
947.. function:: grep(regexp)
948
949 :param regexp: regular expression (see stdlib :mod:`re`)
cliechti06281be2011-08-25 23:08:29 +0000950 :return: filtered sequence, see :func:`comports`.
cliechtic56e41d2011-08-25 22:06:38 +0000951
952 Search for ports using a regular expression. Port name, description and
cliechti06281be2011-08-25 23:08:29 +0000953 hardware ID are searched (case insensitive). The function returns an
954 iterable that contains the same tuples that :func:`comport` generates but
955 only those that match the regexp.
cliechtic56e41d2011-08-25 22:06:38 +0000956
957
958serial.tools.miniterm
959-----------------------
960.. module:: serial.tools.miniterm
961.. versionadded:: 2.6
962
963Miniterm is now available as module instead of example.
964see :ref:`miniterm` for details.
965