blob: 692c65ee2343ff2f166eb850b72c23f6646ceaea [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
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200803.. function:: iterbytes(b)
804
805 :param b: bytes, bytearray or memoryview
806 :returns: a generator that yields bytes
807
808 Some versions of Python (3.x) would return integers instead of bytes when
809 looping over an instance of ``bytes``. This helper function ensures that
810 bytes are returned.
811
812 .. versionadded:: 3.0
813
cliechtie214ff82010-07-21 15:48:47 +0000814
cliechti86844e82009-08-12 00:05:33 +0000815.. _URLs:
cliechtif4566342009-08-04 00:07:19 +0000816
817URLs
818----
cliechtie214ff82010-07-21 15:48:47 +0000819The function :func:`serial_for_url` accepts the following types of URLs:
cliechtif4566342009-08-04 00:07:19 +0000820
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200821- ``rfc2217://<host>:<port>[?<option>[&<option>...]]``
822- ``socket://<host>:<port>[?logging={debug|info|warning|error}]``
823- ``loop://[?logging={debug|info|warning|error}]``
Chris Liechtie9e27ff2015-08-20 23:46:51 +0200824- ``spy://port[?option[=value][&option[=value]]]``
cliechtif4566342009-08-04 00:07:19 +0000825
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200826.. versionchanged:: 3.0 Options are specified with ``?`` and ``&`` instead of ``/``
827
cliechtie214ff82010-07-21 15:48:47 +0000828Device names are also supported, e.g.:
cliechti87686242009-08-18 00:58:31 +0000829
830- ``/dev/ttyUSB0`` (Linux)
831- ``COM3`` (Windows)
832
cliechtie542b362011-03-18 00:49:16 +0000833Future releases of pySerial might add more types. Since pySerial 2.6 it is also
834possible for the user to add protocol handlers using
835:attr:`protocol_handler_packages`.
cliechtif4566342009-08-04 00:07:19 +0000836
cliechtiab90e072009-08-06 01:44:34 +0000837``rfc2217://``
838 Used to connect to :rfc:`2217` compatible servers. All serial port
cliechti25375b52010-07-21 23:27:13 +0000839 functions are supported. Implemented by :class:`rfc2217.Serial`.
cliechtiab90e072009-08-06 01:44:34 +0000840
841 Supported options in the URL are:
842
843 - ``ign_set_control`` does not wait for acknowledges to SET_CONTROL. This
844 option can be used for non compliant servers (i.e. when getting an
845 ``remote rejected value for option 'control'`` error when connecting).
846
847 - ``poll_modem``: The client issues NOTIFY_MODEMSTATE requests when status
848 lines are read (CTS/DTR/RI/CD). Without this option it relies on the server
849 sending the notifications automatically (that's what the RFC suggests and
850 most servers do). Enable this option when :meth:`getCTS` does not work as
851 expected, i.e. for servers that do not send notifications.
852
cliechtidfe2d272009-08-10 22:19:41 +0000853 - ``timeout=<value>``: Change network timeout (default 3 seconds). This is
854 useful when the server takes a little more time to send its answers. The
855 timeout applies to the initial Telnet / :rfc:`2271` negotiation as well
856 as changing port settings or control line change commands.
857
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200858 - ``logging={debug|info|warning|error}``: Prints diagnostic messages (not
cliechtic64ba692009-08-12 00:32:47 +0000859 useful for end users). It uses the logging module and a logger called
860 ``pySerial.rfc2217`` so that the application can setup up logging
861 handlers etc. It will call :meth:`logging.basicConfig` which initializes
862 for output on ``sys.stderr`` (if no logging was set up already).
cliechtiab90e072009-08-06 01:44:34 +0000863
864``socket://``
865 The purpose of this connection type is that applications using pySerial can
866 connect to TCP/IP to serial port converters that do not support :rfc:`2217`.
867
868 Uses a TCP/IP socket. All serial port settings, control and status lines
869 are ignored. Only data is transmitted and received.
870
871 Supported options in the URL are:
872
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200873 - ``logging={debug|info|warning|error}``: Prints diagnostic messages (not
cliechtic64ba692009-08-12 00:32:47 +0000874 useful for end users). It uses the logging module and a logger called
875 ``pySerial.socket`` so that the application can setup up logging handlers
876 etc. It will call :meth:`logging.basicConfig` which initializes for
877 output on ``sys.stderr`` (if no logging was set up already).
cliechtiab90e072009-08-06 01:44:34 +0000878
cliechti41973a92009-08-06 02:18:21 +0000879``loop://``
cliechtie214ff82010-07-21 15:48:47 +0000880 The least useful type. It simulates a loop back connection
881 (``RX<->TX`` ``RTS<->CTS`` ``DTR<->DSR``). It could be used to test
882 applications or run the unit tests.
cliechti41973a92009-08-06 02:18:21 +0000883
884 Supported options in the URL are:
885
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200886 - ``logging={debug|info|warning|error}``: Prints diagnostic messages (not
cliechtic64ba692009-08-12 00:32:47 +0000887 useful for end users). It uses the logging module and a logger called
888 ``pySerial.loop`` so that the application can setup up logging handlers
889 etc. It will call :meth:`logging.basicConfig` which initializes for
890 output on ``sys.stderr`` (if no logging was set up already).
cliechti41973a92009-08-06 02:18:21 +0000891
cliechticdc660c2011-11-03 23:24:31 +0000892``hwgrep://``
893 This type uses :mod:`serial.tools.list_ports` to obtain a list of ports and
894 searches the list for matches by a regexp (see :py:mod:`re`) that follows
895 the slashes.
896
897 Depending on the capabilities of the list_ports module on the system, it is
898 possible to search for the description or hardware ID of a device, e.g. USB
899 VID:PID or texts.
900
901 Unfortunately, on some systems list_ports only lists a subset of the port
Chris Liechtice621882015-08-06 19:29:31 +0200902 names with no additional information. Currently, on Windows and Linux and
903 OSX it should find additional information.
cliechticdc660c2011-11-03 23:24:31 +0000904
Chris Liechtie9e27ff2015-08-20 23:46:51 +0200905``spy://``
906 Wrapping the native serial port, this protocol makes it possible to
907 intercept the data received and transmitted as well as the access to the
908 control lines, break and flush commands.
909
910 Supported options in the URL are:
911
Chris Liechti876801a2015-08-21 23:15:45 +0200912 - ``file=FILENAME`` output to given file or device instead of stderr
Chris Liechtie9e27ff2015-08-20 23:46:51 +0200913 - ``color`` enable ANSI escape sequences to colorize output
914 - ``raw`` output the read and written data directly (default is to create a
915 hex dump). In this mode, no control line and other commands are logged.
916
917 Example::
918
919 import serial
920
Chris Liechti876801a2015-08-21 23:15:45 +0200921 with serial.serial_for_url('spy:///dev/ttyUSB0?file=test.txt', timeout=1) as s:
Chris Liechtie9e27ff2015-08-20 23:46:51 +0200922 s.setDTR(False)
923 s.write('hello world')
924 s.read(20)
925 s.setDTR(True)
926 s.write(serial.to_bytes(range(256)))
927 s.read(400)
928 s.sendBreak()
929
930 with open('test.txt') as f:
931 print(f.read())
932
933 Outputs::
934
935 000000.002 FLSH flushInput
936 000000.002 DTR inactive
937 000000.002 TX 0000 68 65 6C 6C 6F 20 77 6F 72 6C 64 hello wo rld
938 000001.015 RX 0000 68 65 6C 6C 6F 20 77 6F 72 6C 64 hello wo rld
939 000001.015 DTR active
940 000001.015 TX 0000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ........ ........
941 000001.015 TX 0010 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ........ ........
942 000001.015 TX 0020 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&' ()*+,-./
943 000001.015 TX 0030 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 01234567 89:;<=>?
944 000001.015 TX 0040 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F @ABCDEFG HIJKLMNO
945 000001.016 TX 0050 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F PQRSTUVW XYZ[\]^_
946 000001.016 TX 0060 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F `abcdefg hijklmno
947 000001.016 TX 0070 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F pqrstuvw xyz{|}~.
948 000001.016 TX 0080 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F ........ ........
949 000001.016 TX 0090 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F ........ ........
950 000001.016 TX 00A0 A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF ........ ........
951 000001.016 TX 00B0 B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF ........ ........
952 000001.016 TX 00C0 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF ........ ........
953 000001.016 TX 00D0 D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF ........ ........
954 000001.016 TX 00E0 E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF ........ ........
955 000001.016 TX 00F0 F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF ........ ........
956 000002.284 RX 0000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ........ ........
957 000002.284 RX 0010 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ........ ........
958 000002.284 RX 0020 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&' ()*+,-./
959 000002.284 RX 0030 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 01234567 89:;<=>?
960 000002.284 RX 0040 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F @ABCDEFG HIJKLMNO
961 000002.284 RX 0050 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F PQRSTUVW XYZ[\]^_
962 000002.284 RX 0060 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F `abcdefg hijklmno
963 000002.284 RX 0070 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F pqrstuvw xyz{|}~.
964 000002.284 RX 0080 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F ........ ........
965 000002.284 RX 0090 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F ........ ........
966 000002.284 RX 00A0 A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF ........ ........
967 000002.284 RX 00B0 B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF ........ ........
968 000002.284 RX 00C0 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF ........ ........
969 000002.284 RX 00D0 D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF ........ ........
970 000002.284 RX 00E0 E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF ........ ........
971 000002.284 RX 00F0 F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF ........ ........
972 000002.284 BRK sendBreak 0.25
973
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200974 .. versionadded:: 3.0
975
cliechticdc660c2011-11-03 23:24:31 +0000976
cliechti41973a92009-08-06 02:18:21 +0000977
cliechtidfe2d272009-08-10 22:19:41 +0000978Examples:
cliechtif4566342009-08-04 00:07:19 +0000979
cliechtidfe2d272009-08-10 22:19:41 +0000980- ``rfc2217://localhost:7000``
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200981- ``rfc2217://localhost:7000?poll_modem``
982- ``rfc2217://localhost:7000?ign_set_control&timeout=5.5``
cliechtidfe2d272009-08-10 22:19:41 +0000983- ``socket://localhost:7777``
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200984- ``loop://?logging=debug``
cliechticdc660c2011-11-03 23:24:31 +0000985- ``hwgrep://0451:f432`` (USB VID:PID)
Chris Liechti876801a2015-08-21 23:15:45 +0200986- ``spy://COM54?file=log.txt``
cliechtic56e41d2011-08-25 22:06:38 +0000987
988Tools
989=====
990
991
992serial.tools.list_ports
993-----------------------
994.. module:: serial.tools.list_ports
995.. versionadded:: 2.6
996
cliechti06281be2011-08-25 23:08:29 +0000997This module can be executed to get a list of ports (``python -m
998serial.tools.list_ports``). It also contains the following functions.
cliechtic56e41d2011-08-25 22:06:38 +0000999
1000
1001.. function:: comports()
1002
1003 :return: an iterable.
1004
1005 The function returns an iterable that yields tuples of three strings:
1006
cliechti06281be2011-08-25 23:08:29 +00001007 - port name as it can be passed to :class:`serial.Serial` or
cliechtic56e41d2011-08-25 22:06:38 +00001008 :func:`serial.serial_for_url`
cliechti06281be2011-08-25 23:08:29 +00001009 - description in human readable form
1010 - sort of hardware ID. E.g. may contain VID:PID of USB-serial adapters.
cliechtic56e41d2011-08-25 22:06:38 +00001011
cliechti06281be2011-08-25 23:08:29 +00001012 Items are returned in no particular order. It may make sense to sort the
1013 items. Also note that the reported strings are different across platforms
1014 and operating systems, even for the same device.
cliechtic56e41d2011-08-25 22:06:38 +00001015
1016 .. note:: Support is limited to a number of operating systems. On some
cliechti06281be2011-08-25 23:08:29 +00001017 systems description and hardware ID will not be available
cliechtic56e41d2011-08-25 22:06:38 +00001018 (``None``).
1019
1020 :platform: Posix (/dev files)
1021 :platform: Linux (/dev files, sysfs and lsusb)
cliechti4cb94662013-10-17 03:17:50 +00001022 :platform: OSX (iokit)
cliechtic56e41d2011-08-25 22:06:38 +00001023 :platform: Windows (setupapi, registry)
1024
1025
1026.. function:: grep(regexp)
1027
1028 :param regexp: regular expression (see stdlib :mod:`re`)
cliechti06281be2011-08-25 23:08:29 +00001029 :return: filtered sequence, see :func:`comports`.
cliechtic56e41d2011-08-25 22:06:38 +00001030
1031 Search for ports using a regular expression. Port name, description and
cliechti06281be2011-08-25 23:08:29 +00001032 hardware ID are searched (case insensitive). The function returns an
1033 iterable that contains the same tuples that :func:`comport` generates but
1034 only those that match the regexp.
cliechtic56e41d2011-08-25 22:06:38 +00001035
1036
1037serial.tools.miniterm
1038-----------------------
1039.. module:: serial.tools.miniterm
1040.. versionadded:: 2.6
1041
1042Miniterm is now available as module instead of example.
1043see :ref:`miniterm` for details.
1044