blob: 202bcbc2edf9d460f170622fcdd8b62a5e1966a4 [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
Chris Liechtice621882015-08-06 19:29:31 +020020 :param int baudrate:
cliechtic1c37602009-07-21 01:34:57 +000021 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
Chris Liechtice621882015-08-06 19:29:31 +020038 :param float timeout:
cliechtic1c37602009-07-21 01:34:57 +000039 Set a read timeout value.
40
Chris Liechtice621882015-08-06 19:29:31 +020041 :param bool xonxoff:
cliechtic1c37602009-07-21 01:34:57 +000042 Enable software flow control.
43
Chris Liechtice621882015-08-06 19:29:31 +020044 :param bool rtscts:
cliechtic1c37602009-07-21 01:34:57 +000045 Enable hardware (RTS/CTS) flow control.
46
Chris Liechtice621882015-08-06 19:29:31 +020047 :param bool dsrdtr:
cliechti07709e42010-05-21 00:30:09 +000048 Enable hardware (DSR/DTR) flow control.
49
Chris Liechtice621882015-08-06 19:29:31 +020050 :param float writeTimeout:
cliechti07709e42010-05-21 00:30:09 +000051 Set a write timeout value.
52
Chris Liechtice621882015-08-06 19:29:31 +020053 :param float 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
Chris Liechtice621882015-08-06 19:29:31 +0200283 .. attribute:: rs485_mode
284
285 :platform: Posix
286 :platform: Windows
287
288 Attribute to configure RS485 support. When set to an instance of
289 :class:`rs485.RS485Settings` and supported by OS, RTS will be active
290 when data is sent and inactive otherwise (for reception). The
291 :class:`rs485.RS485Settings` class provides additional settings
292 supported on some platforms.
293
294 .. versionadded:: 3.0
295
296
cliechtiedcdbe42009-07-22 00:48:34 +0000297 The following constants are also provided:
298
299 .. attribute:: BAUDRATES
300
cliechti25375b52010-07-21 23:27:13 +0000301 A list of valid baud rates. The list may be incomplete such that higher
302 baud rates may be supported by the device and that values in between the
303 standard baud rates are supported. (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000304
305 .. attribute:: BYTESIZES
306
cliechti25375b52010-07-21 23:27:13 +0000307 A list of valid byte sizes for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000308
309 .. attribute:: PARITIES
310
cliechti25375b52010-07-21 23:27:13 +0000311 A list of valid parities for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000312
313 .. attribute:: STOPBITS
314
cliechti25375b52010-07-21 23:27:13 +0000315 A list of valid stop bit widths for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000316
cliechti4a567a02009-07-27 22:09:31 +0000317
cliechti25375b52010-07-21 23:27:13 +0000318 The following methods are for compatibility with the :mod:`io` library.
cliechti4a567a02009-07-27 22:09:31 +0000319
320 .. method:: readable()
321
322 :return: True
cliechtif4566342009-08-04 00:07:19 +0000323
cliechti4a567a02009-07-27 22:09:31 +0000324 .. versionadded:: 2.5
325
326 .. method:: writable()
327
328 :return: True
cliechtif4566342009-08-04 00:07:19 +0000329
cliechti4a567a02009-07-27 22:09:31 +0000330 .. versionadded:: 2.5
331
332 .. method:: seekable()
333
334 :return: False
cliechtif4566342009-08-04 00:07:19 +0000335
cliechti4a567a02009-07-27 22:09:31 +0000336 .. versionadded:: 2.5
337
338 .. method:: readinto(b)
339
cliechti8611bf42009-08-03 00:34:03 +0000340 :param b: bytearray or array instance
cliechti4a567a02009-07-27 22:09:31 +0000341 :return: Number of byte read
342
cliechtid7e7ce22009-08-03 02:01:07 +0000343 Read up to len(b) bytes into :class:`bytearray` *b* and return the
344 number of bytes read.
cliechti4a567a02009-07-27 22:09:31 +0000345
346 .. versionadded:: 2.5
347
cliechti25375b52010-07-21 23:27:13 +0000348 The port settings can be read and written as dictionary.
349
cliechti7053b232009-08-10 01:37:34 +0000350 .. method:: getSettingsDict()
cliechti4065dce2009-08-10 00:55:46 +0000351
352 :return: a dictionary with current port settings.
353
354 Get a dictionary with port settings. This is useful to backup the
355 current settings so that a later point in time they can be restored
356 using :meth:`applySettingsDict`.
357
358 Note that control lines (RTS/DTR) are part of the settings.
359
360 .. versionadded:: 2.5
361
cliechti7053b232009-08-10 01:37:34 +0000362 .. method:: applySettingsDict(d)
cliechti4065dce2009-08-10 00:55:46 +0000363
364 :param d: a dictionary with port settings.
365
366 Applies a dictionary that was created by :meth:`getSettingsDict`. Only
367 changes are applied and when a key is missing it means that the setting
368 stays unchanged.
369
370 Note that control lines (RTS/DTR) are not changed.
371
372 .. versionadded:: 2.5
373
cliechti25375b52010-07-21 23:27:13 +0000374 Platform specific methods.
375
cliechtic648da92011-12-29 01:17:51 +0000376 .. warning:: Programs using the following methods and attributes are not
377 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000378
cliechti28b8fd02011-12-28 21:39:42 +0000379 .. method:: outWaiting()
380
Chris Liechtice621882015-08-06 19:29:31 +0200381 :platform: Posix
cliechti28b8fd02011-12-28 21:39:42 +0000382 :platform: Windows
383
384 Return the number of bytes in the output buffer.
385
386 .. versionchanged:: 2.7 (Posix support added)
387
cliechti25375b52010-07-21 23:27:13 +0000388 .. method:: nonblocking()
389
Chris Liechtice621882015-08-06 19:29:31 +0200390 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000391
392 Configure the device for nonblocking operation. This can be useful if
393 the port is used with :mod:`select`.
394
395 .. method:: fileno()
396
Chris Liechtice621882015-08-06 19:29:31 +0200397 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000398 :return: File descriptor.
399
400 Return file descriptor number for the port that is opened by this object.
401 It is useful when serial ports are used with :mod:`select`.
402
403 .. method:: setXON(level=True)
404
405 :platform: Windows
cliechti2f0f8a32011-12-28 22:10:00 +0000406 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000407 :param level: Set flow control state.
408
cliechti2f0f8a32011-12-28 22:10:00 +0000409 Manually control flow - when software flow control is enabled.
410
411 This will send XON (true) and XOFF (false) to the other device.
412
413 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
414
415 .. method:: flowControlOut(enable)
416
417 :platform: Posix
418 :param enable: Set flow control state.
419
420 Manually control flow of outgoing data - when hardware or software flow
421 control is enabled.
422
423 Sending will be suspended when called with ``False`` and enabled when
424 called with ``True``.
425
426 .. versionadded:: 2.7 (Posix support added)
cliechtif4566342009-08-04 00:07:19 +0000427
cliechtic648da92011-12-29 01:17:51 +0000428 .. attribute:: rtsToggle
429
430 :platform: Windows
431
432 Attribute to configure RTS toggle control setting. When enabled and
433 supported by OS, RTS will be active when data is available and inactive
434 if no data is available.
435
436 .. versionadded:: 2.6
Chris Liechtice621882015-08-06 19:29:31 +0200437 .. versionchanged:: 3.0 (removed, see :meth:`rs485_mode` instead)
cliechtic648da92011-12-29 01:17:51 +0000438
439
cliechtic56e41d2011-08-25 22:06:38 +0000440Implementation detail: some attributes and functions are provided by the
441class :class:`SerialBase` and some by the platform specific class and
442others by the base class mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000443
Chris Liechtice621882015-08-06 19:29:31 +0200444RS485 support
445-------------
446The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to
447enable RS485 specific support on some platforms. Currently Windows and Linux
448(only a small number of devices) are supported.
cliechti4a567a02009-07-27 22:09:31 +0000449
Chris Liechtice621882015-08-06 19:29:31 +0200450:attr:`Serial.rs485_mode` needs to be set to an instance of
451:class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature.
cliechti4a567a02009-07-27 22:09:31 +0000452
Chris Liechtice621882015-08-06 19:29:31 +0200453Usage::
cliechti4a567a02009-07-27 22:09:31 +0000454
Chris Liechtice621882015-08-06 19:29:31 +0200455 ser = serial.Serial(...)
456 ser.rs485_mode = serial.rs485.RS485Settings(...)
457 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000458
Chris Liechtice621882015-08-06 19:29:31 +0200459There is a subclass :class:`rs485.RS485` available to emulate the RS485 support
460on regular serial ports.
cliechti4a567a02009-07-27 22:09:31 +0000461
Chris Liechtice621882015-08-06 19:29:31 +0200462Usage::
463
464 ser = serial.rs485.RS485(...)
465 ser.rs485_mode = serial.rs485.RS485Settings(...)
466 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000467
468
Chris Liechtice621882015-08-06 19:29:31 +0200469.. class:: rs485.RS485Settings
cliechti4a567a02009-07-27 22:09:31 +0000470
Chris Liechtice621882015-08-06 19:29:31 +0200471 A class that holds RS485 specific settings which are supported on
472 some platforms.
cliechti4a567a02009-07-27 22:09:31 +0000473
Chris Liechtice621882015-08-06 19:29:31 +0200474 .. versionadded:: 3.0
cliechti4a567a02009-07-27 22:09:31 +0000475
Chris Liechtice621882015-08-06 19:29:31 +0200476 .. method:: __init__(rts_level_for_tx=True, rts_level_for_rx=False, loopback=False, delay_before_tx=None, delay_before_rx=None):
cliechti4a567a02009-07-27 22:09:31 +0000477
Chris Liechtice621882015-08-06 19:29:31 +0200478 :param bool rts_level_for_tx:
479 RTS level for transmission
cliechti4a567a02009-07-27 22:09:31 +0000480
Chris Liechtice621882015-08-06 19:29:31 +0200481 :param bool rts_level_for_rx:
482 RTS level for reception
cliechti4a567a02009-07-27 22:09:31 +0000483
Chris Liechtice621882015-08-06 19:29:31 +0200484 :param bool loopback:
485 When set to ``True`` transmitted data is also received.
cliechti4a567a02009-07-27 22:09:31 +0000486
Chris Liechtice621882015-08-06 19:29:31 +0200487 :param float delay_before_tx:
488 Delay after setting RTS but before transmission starts
489
490 :param float delay_before_rx:
491 Delay after transmission ends and resetting RTS
492
493 .. attribute:: rts_level_for_tx
494
495 RTS level for transmission.
496
497 .. attribute:: rts_level_for_rx
498
499 RTS level for reception.
500
501 .. attribute:: loopback
502
503 When set to ``True`` transmitted data is also received.
504
505 .. attribute:: delay_before_tx
506
507 Delay after setting RTS but before transmission starts (seconds as float).
508
509 .. attribute:: delay_before_rx
510
511 Delay after transmission ends and resetting RTS (seconds as float).
cliechti4a567a02009-07-27 22:09:31 +0000512
513
Chris Liechtice621882015-08-06 19:29:31 +0200514.. class:: rs485.RS485
cliechti4a567a02009-07-27 22:09:31 +0000515
Chris Liechtice621882015-08-06 19:29:31 +0200516 A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS
517 according to the RS485 settings.
cliechti4a567a02009-07-27 22:09:31 +0000518
Chris Liechtice621882015-08-06 19:29:31 +0200519 .. warning:: This may work unreliably on some serial ports (control signals not
520 synchronized or delayed compared to data). Using delays may be unreliable
521 (varying times, larger than expected) as the OS may not support very fine
522 grained delays (no smaller than in the order of tens of milliseconds).
cliechti4a567a02009-07-27 22:09:31 +0000523
Chris Liechtice621882015-08-06 19:29:31 +0200524 .. note:: Some implementations support this natively in the class
525 :class:`Serial`. Better performance can be expected when the native version
526 is used.
cliechti4a567a02009-07-27 22:09:31 +0000527
Chris Liechtice621882015-08-06 19:29:31 +0200528 .. note:: The loopback property is ignored by this implementation. The actual
529 behavior depends on the used hardware.
cliechti4a567a02009-07-27 22:09:31 +0000530
cliechti4a567a02009-07-27 22:09:31 +0000531
cliechtie214ff82010-07-21 15:48:47 +0000532
cliechti25375b52010-07-21 23:27:13 +0000533
cliechti7aed8332009-08-05 14:19:31 +0000534:rfc:`2217` Network ports
535-------------------------
536
537.. warning:: This implementation is currently in an experimental state. Use
538 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000539
cliechtiec4ac1b2009-08-02 00:47:21 +0000540.. class:: rfc2217.Serial
541
cliechtif4566342009-08-04 00:07:19 +0000542 This implements a :rfc:`2217` compatible client. Port names are URLs_ in the
543 form: ``rfc2217://<host>:<port>[/<option>[/<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000544
cliechtiec4ac1b2009-08-02 00:47:21 +0000545 This class API is compatible to :class:`Serial` with a few exceptions:
546
547 - numbers as port name are not allowed, only URLs in the form described
548 above.
549 - writeTimeout is not implemented
550 - The current implementation starts a thread that keeps reading from the
551 (internal) socket. The thread is managed automatically by the
552 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
553 However it may be a problem for user applications that like to use select
554 instead of threads.
555
556 Due to the nature of the network and protocol involved there are a few
557 extra points to keep in mind:
558
559 - All operations have an additional latency time.
560 - Setting control lines (RTS/CTS) needs more time.
561 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
562 cache is updated depends entirely on the server. The server itself may
563 implement a polling at a certain rate and quick changes may be invisible.
564 - The network layer also has buffers. This means that :meth:`flush`,
565 :meth:`flushInput` and :meth:`flushOutput` may work with additional delay.
566 Likewise :meth:`inWaiting` returns the size of the data arrived at the
567 object internal buffer and excludes any bytes in the network buffers or
568 any server side buffer.
569 - Closing and immediately reopening the same port may fail due to time
570 needed by the server to get ready again.
571
572 Not implemented yet / Possible problems with the implementation:
573
cliechti8611bf42009-08-03 00:34:03 +0000574 - :rfc:`2217` flow control between client and server (objects internal
575 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000576 - No authentication support (servers may not prompt for a password etc.)
577 - No encryption.
578
579 Due to lack of authentication and encryption it is not suitable to use this
580 client for connections across the internet and should only be used in
581 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000582
cliechti7c640ed2009-08-02 00:54:51 +0000583 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000584
cliechti7aed8332009-08-05 14:19:31 +0000585
586.. class:: rfc2217.PortManager
587
588 This class provides helper functions for implementing :rfc:`2217`
589 compatible servers.
590
591 Basically, it implements every thing needed for the :rfc:`2217` protocol.
592 It just does not open sockets and read/write to serial ports (though it
593 changes other port settings). The user of this class must take care of the
594 data transmission itself. The reason for that is, that this way, this class
595 supports all programming models such as threads and select.
596
597 Usage examples can be found in the examples where two TCP/IP - serial
598 converters are shown, one using threads (the single port server) and an
599 other using select (the multi port server).
600
601 .. note:: Each new client connection must create a new instance as this
602 object (and the :rfc:`2217` protocol) has internal state.
603
604 .. method:: __init__(serial_port, connection, debug_output=False)
605
606 :param serial_port: a :class:`Serial` instance that is managed.
607 :param connection: an object implementing :meth:`write`, used to write
608 to the network.
cliechti86844e82009-08-12 00:05:33 +0000609 :param debug_output: enables debug messages: a :class:`logging.Logger`
610 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000611
612 Initializes the Manager and starts negotiating with client in Telnet
613 and :rfc:`2217` protocol. The negotiation starts immediately so that
614 the class should be instantiated in the moment the client connects.
615
616 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000617 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000618 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
619 the :meth:`filter` method.
620
621 The *connection* object must implement a :meth:`write(data)` function.
622 This function must ensure that *data* is written at once (no user data
623 mixed in, i.e. it must be thread-safe). All data must be sent in its
624 raw form (:meth:`escape` must not be used) as it is used to send Telnet
625 and :rfc:`2217` control commands.
626
cliechti86844e82009-08-12 00:05:33 +0000627 For diagnostics of the connection or the implementation, *debug_output*
628 can be set to an instance of a :class:`logging.Logger` (e.g.
629 ``logging.getLogger('rfc2217.server')``). The caller should configure
630 the logger using ``setLevel`` for the desired detail level of the logs.
631
cliechti7aed8332009-08-05 14:19:31 +0000632 .. method:: escape(data)
633
634 :param data: data to be sent over the network.
635 :return: data, escaped for Telnet/:rfc:`2217`
636
637 A generator that escapes all data to be compatible with :rfc:`2217`.
638 Implementors of servers should use this function to process all data
639 sent over the network.
640
641 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000642 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000643
644 .. method:: filter(data)
645
646 :param data: data read from the network, including Telnet and
647 :rfc:`2217` controls.
648 :return: data, free from Telnet and :rfc:`2217` controls.
649
650 A generator that filters and processes all data related to :rfc:`2217`.
651 Implementors of servers should use this function to process all data
652 received from the network.
653
654 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000655 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000656
cliechti7cb78e82009-08-05 15:47:57 +0000657 .. method:: check_modem_lines(force_notification=False)
658
659 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000660
661 This function needs to be called periodically (e.g. every second) when
662 the server wants to send NOTIFY_MODEMSTATE messages. This is required
663 to support the client for reading CTS/DSR/RI/CD status lines.
664
665 The function reads the status line and issues the notifications
666 automatically.
667
668 .. versionadded:: 2.5
669
cliechtiec4ac1b2009-08-02 00:47:21 +0000670.. seealso::
671
672 :rfc:`2217` - Telnet Com Port Control Option
673
674
cliechtic1c37602009-07-21 01:34:57 +0000675Exceptions
676==========
677
678.. exception:: SerialException
679
680 Base class for serial port exceptions.
681
cliechti4a567a02009-07-27 22:09:31 +0000682 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000683 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000684
cliechtic1c37602009-07-21 01:34:57 +0000685.. exception:: SerialTimeoutException
686
687 Exception that is raised on write timeouts.
688
689
690Constants
691=========
692
cliechti87686242009-08-18 00:58:31 +0000693*Parity*
694
cliechtic1c37602009-07-21 01:34:57 +0000695.. data:: PARITY_NONE
696.. data:: PARITY_EVEN
697.. data:: PARITY_ODD
698.. data:: PARITY_MARK
699.. data:: PARITY_SPACE
700
cliechti87686242009-08-18 00:58:31 +0000701*Stop bits*
702
cliechtic1c37602009-07-21 01:34:57 +0000703.. data:: STOPBITS_ONE
704.. data:: STOPBITS_ONE_POINT_FIVE
705.. data:: STOPBITS_TWO
706
cliechti06281be2011-08-25 23:08:29 +0000707Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +0000708bits.
709
cliechti87686242009-08-18 00:58:31 +0000710*Byte size*
711
cliechtic1c37602009-07-21 01:34:57 +0000712.. data:: FIVEBITS
713.. data:: SIXBITS
714.. data:: SEVENBITS
715.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000716
cliechti87686242009-08-18 00:58:31 +0000717
718*Others*
719
cliechti8611bf42009-08-03 00:34:03 +0000720Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000721software flow control:
cliechti6066f842009-07-24 00:05:45 +0000722
cliechti5b7d16a2009-07-21 21:53:59 +0000723.. data:: XON
724.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000725
cliechti4a567a02009-07-27 22:09:31 +0000726Module version:
cliechtif81362e2009-07-25 03:44:33 +0000727
728.. data:: VERSION
729
730 A string indicating the pySerial version, such as ``2.5``.
731
cliechti87686242009-08-18 00:58:31 +0000732 .. versionadded:: 2.3
733
cliechti44eb98f2011-03-21 21:41:10 +0000734
cliechtie542b362011-03-18 00:49:16 +0000735Module functions and attributes
736===============================
cliechtif81362e2009-07-25 03:44:33 +0000737
738.. function:: device(number)
739
740 :param number: Port number.
741 :return: String containing device name.
742 :deprecated: Use device names directly.
743
744 Convert a port number to a platform dependent device name. Unfortunately
745 this does not work well for all platforms; e.g. some may miss USB-Serial
746 converters and enumerate only internal serial ports.
747
748 The conversion may be made off-line, that is, there is no guarantee that
749 the returned device name really exists on the system.
cliechti7c640ed2009-08-02 00:54:51 +0000750
751
cliechtie3ab3532009-08-05 12:40:38 +0000752.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +0000753
cliechti86844e82009-08-12 00:05:33 +0000754 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +0000755 :param do_not_open: When set to true, the serial port is not opened.
756 :return: an instance of :class:`Serial` or a compatible object.
757
758 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +0000759 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +0000760 to support both, local ports and remote ports. There is also support
761 for other types, see :ref:`URL <URLs>` section below.
cliechti7c640ed2009-08-02 00:54:51 +0000762
cliechtid7e7ce22009-08-03 02:01:07 +0000763 The port is not opened when a keyword parameter called *do_not_open* is
764 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +0000765
766 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +0000767
cliechti87686242009-08-18 00:58:31 +0000768
cliechtie542b362011-03-18 00:49:16 +0000769.. attribute:: protocol_handler_packages
770
771 This attribute is a list of package names (strings) that is searched for
772 protocol handlers.
773
774 e.g. we want to support a URL ``foobar://``. A module
775 ``my_handlers.protocol_foobar`` is provided by the user::
776
777 serial.protocol_handler_packages.append("my_handlers")
778 s = serial.serial_for_url("foobar://")
779
780 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
781 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
782 ``PACKAGE`` from this list.
783
784 .. versionadded:: 2.6
785
786
cliechti25375b52010-07-21 23:27:13 +0000787.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +0000788
789 :param sequence: String or list of integers
cliechti25375b52010-07-21 23:27:13 +0000790 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +0000791
cliechtie542b362011-03-18 00:49:16 +0000792 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +0000793 compatible to Python 2.x and 3.x.
794
cliechtie542b362011-03-18 00:49:16 +0000795 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +0000796 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +0000797 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +0000798
799 This function is used internally and in the unit tests.
800
cliechtie542b362011-03-18 00:49:16 +0000801 .. versionadded:: 2.5
802
cliechtie214ff82010-07-21 15:48:47 +0000803
cliechti86844e82009-08-12 00:05:33 +0000804.. _URLs:
cliechtif4566342009-08-04 00:07:19 +0000805
806URLs
807----
cliechtie214ff82010-07-21 15:48:47 +0000808The function :func:`serial_for_url` accepts the following types of URLs:
cliechtif4566342009-08-04 00:07:19 +0000809
cliechtiab90e072009-08-06 01:44:34 +0000810- ``rfc2217://<host>:<port>[/<option>[/<option>]]``
811- ``socket://<host>:<port>[/<option>[/<option>]]``
cliechti41973a92009-08-06 02:18:21 +0000812- ``loop://[<option>[/<option>]]``
cliechtif4566342009-08-04 00:07:19 +0000813
cliechtie214ff82010-07-21 15:48:47 +0000814Device names are also supported, e.g.:
cliechti87686242009-08-18 00:58:31 +0000815
816- ``/dev/ttyUSB0`` (Linux)
817- ``COM3`` (Windows)
818
cliechtie542b362011-03-18 00:49:16 +0000819Future releases of pySerial might add more types. Since pySerial 2.6 it is also
820possible for the user to add protocol handlers using
821:attr:`protocol_handler_packages`.
cliechtif4566342009-08-04 00:07:19 +0000822
cliechtiab90e072009-08-06 01:44:34 +0000823``rfc2217://``
824 Used to connect to :rfc:`2217` compatible servers. All serial port
cliechti25375b52010-07-21 23:27:13 +0000825 functions are supported. Implemented by :class:`rfc2217.Serial`.
cliechtiab90e072009-08-06 01:44:34 +0000826
827 Supported options in the URL are:
828
829 - ``ign_set_control`` does not wait for acknowledges to SET_CONTROL. This
830 option can be used for non compliant servers (i.e. when getting an
831 ``remote rejected value for option 'control'`` error when connecting).
832
833 - ``poll_modem``: The client issues NOTIFY_MODEMSTATE requests when status
834 lines are read (CTS/DTR/RI/CD). Without this option it relies on the server
835 sending the notifications automatically (that's what the RFC suggests and
836 most servers do). Enable this option when :meth:`getCTS` does not work as
837 expected, i.e. for servers that do not send notifications.
838
cliechtidfe2d272009-08-10 22:19:41 +0000839 - ``timeout=<value>``: Change network timeout (default 3 seconds). This is
840 useful when the server takes a little more time to send its answers. The
841 timeout applies to the initial Telnet / :rfc:`2271` negotiation as well
842 as changing port settings or control line change commands.
843
cliechti86844e82009-08-12 00:05:33 +0000844 - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not
cliechtic64ba692009-08-12 00:32:47 +0000845 useful for end users). It uses the logging module and a logger called
846 ``pySerial.rfc2217`` so that the application can setup up logging
847 handlers etc. It will call :meth:`logging.basicConfig` which initializes
848 for output on ``sys.stderr`` (if no logging was set up already).
cliechtiab90e072009-08-06 01:44:34 +0000849
850``socket://``
851 The purpose of this connection type is that applications using pySerial can
852 connect to TCP/IP to serial port converters that do not support :rfc:`2217`.
853
854 Uses a TCP/IP socket. All serial port settings, control and status lines
855 are ignored. Only data is transmitted and received.
856
857 Supported options in the URL are:
858
cliechtic64ba692009-08-12 00:32:47 +0000859 - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not
860 useful for end users). It uses the logging module and a logger called
861 ``pySerial.socket`` so that the application can setup up logging handlers
862 etc. It will call :meth:`logging.basicConfig` which initializes for
863 output on ``sys.stderr`` (if no logging was set up already).
cliechtiab90e072009-08-06 01:44:34 +0000864
cliechti41973a92009-08-06 02:18:21 +0000865``loop://``
cliechtie214ff82010-07-21 15:48:47 +0000866 The least useful type. It simulates a loop back connection
867 (``RX<->TX`` ``RTS<->CTS`` ``DTR<->DSR``). It could be used to test
868 applications or run the unit tests.
cliechti41973a92009-08-06 02:18:21 +0000869
870 Supported options in the URL are:
871
cliechtic64ba692009-08-12 00:32:47 +0000872 - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not
873 useful for end users). It uses the logging module and a logger called
874 ``pySerial.loop`` so that the application can setup up logging handlers
875 etc. It will call :meth:`logging.basicConfig` which initializes for
876 output on ``sys.stderr`` (if no logging was set up already).
cliechti41973a92009-08-06 02:18:21 +0000877
cliechticdc660c2011-11-03 23:24:31 +0000878``hwgrep://``
879 This type uses :mod:`serial.tools.list_ports` to obtain a list of ports and
880 searches the list for matches by a regexp (see :py:mod:`re`) that follows
881 the slashes.
882
883 Depending on the capabilities of the list_ports module on the system, it is
884 possible to search for the description or hardware ID of a device, e.g. USB
885 VID:PID or texts.
886
887 Unfortunately, on some systems list_ports only lists a subset of the port
Chris Liechtice621882015-08-06 19:29:31 +0200888 names with no additional information. Currently, on Windows and Linux and
889 OSX it should find additional information.
cliechticdc660c2011-11-03 23:24:31 +0000890
891
cliechti41973a92009-08-06 02:18:21 +0000892
cliechtidfe2d272009-08-10 22:19:41 +0000893Examples:
cliechtif4566342009-08-04 00:07:19 +0000894
cliechtidfe2d272009-08-10 22:19:41 +0000895- ``rfc2217://localhost:7000``
896- ``rfc2217://localhost:7000/poll_modem``
897- ``rfc2217://localhost:7000/ign_set_control/timeout=5.5``
898- ``socket://localhost:7777``
cliechtic64ba692009-08-12 00:32:47 +0000899- ``loop://logging=debug``
cliechticdc660c2011-11-03 23:24:31 +0000900- ``hwgrep://0451:f432`` (USB VID:PID)
cliechtic56e41d2011-08-25 22:06:38 +0000901
902Tools
903=====
904
905
906serial.tools.list_ports
907-----------------------
908.. module:: serial.tools.list_ports
909.. versionadded:: 2.6
910
cliechti06281be2011-08-25 23:08:29 +0000911This module can be executed to get a list of ports (``python -m
912serial.tools.list_ports``). It also contains the following functions.
cliechtic56e41d2011-08-25 22:06:38 +0000913
914
915.. function:: comports()
916
917 :return: an iterable.
918
919 The function returns an iterable that yields tuples of three strings:
920
cliechti06281be2011-08-25 23:08:29 +0000921 - port name as it can be passed to :class:`serial.Serial` or
cliechtic56e41d2011-08-25 22:06:38 +0000922 :func:`serial.serial_for_url`
cliechti06281be2011-08-25 23:08:29 +0000923 - description in human readable form
924 - sort of hardware ID. E.g. may contain VID:PID of USB-serial adapters.
cliechtic56e41d2011-08-25 22:06:38 +0000925
cliechti06281be2011-08-25 23:08:29 +0000926 Items are returned in no particular order. It may make sense to sort the
927 items. Also note that the reported strings are different across platforms
928 and operating systems, even for the same device.
cliechtic56e41d2011-08-25 22:06:38 +0000929
930 .. note:: Support is limited to a number of operating systems. On some
cliechti06281be2011-08-25 23:08:29 +0000931 systems description and hardware ID will not be available
cliechtic56e41d2011-08-25 22:06:38 +0000932 (``None``).
933
934 :platform: Posix (/dev files)
935 :platform: Linux (/dev files, sysfs and lsusb)
cliechti4cb94662013-10-17 03:17:50 +0000936 :platform: OSX (iokit)
cliechtic56e41d2011-08-25 22:06:38 +0000937 :platform: Windows (setupapi, registry)
938
939
940.. function:: grep(regexp)
941
942 :param regexp: regular expression (see stdlib :mod:`re`)
cliechti06281be2011-08-25 23:08:29 +0000943 :return: filtered sequence, see :func:`comports`.
cliechtic56e41d2011-08-25 22:06:38 +0000944
945 Search for ports using a regular expression. Port name, description and
cliechti06281be2011-08-25 23:08:29 +0000946 hardware ID are searched (case insensitive). The function returns an
947 iterable that contains the same tuples that :func:`comport` generates but
948 only those that match the regexp.
cliechtic56e41d2011-08-25 22:06:38 +0000949
950
951serial.tools.miniterm
952-----------------------
953.. module:: serial.tools.miniterm
954.. versionadded:: 2.6
955
956Miniterm is now available as module instead of example.
957see :ref:`miniterm` for details.
958