blob: ef5eb76dac0991bd58640b6ae833a9384e63ce8d [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
Chris Liechti256ea212015-08-29 23:57:00 +0200121 The following methods may raise :exc:`SerialException` when applied to a closed
cliechti4a567a02009-07-27 22:09:31 +0000122 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
Chris Liechti256ea212015-08-29 23:57:00 +0200154 .. attribute:: in_waiting
cliechti4a567a02009-07-27 22:09:31 +0000155
Chris Liechti256ea212015-08-29 23:57:00 +0200156 :getter: Get the number of bytes in the input buffer
157 :type: int
158
159 Return the number of bytes in the receive buffer.
cliechti4a567a02009-07-27 22:09:31 +0000160
161 .. method:: flush()
cliechtic1c37602009-07-21 01:34:57 +0000162
163 Flush of file like objects. In this case, wait until all data is
164 written.
165
Chris Liechti256ea212015-08-29 23:57:00 +0200166 .. method:: reset_input_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000167
168 Flush input buffer, discarding all it's contents.
169
Chris Liechti256ea212015-08-29 23:57:00 +0200170 .. method:: reset_output_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000171
172 Clear output buffer, aborting the current output and
173 discarding all that is in the buffer.
174
Chris Liechti256ea212015-08-29 23:57:00 +0200175 .. method:: send_break(duration=0.25)
cliechtic1c37602009-07-21 01:34:57 +0000176
cliechtibb5c3c52009-07-23 02:29:27 +0000177 :param duration: Time (float) to activate the BREAK condition.
178
cliechtic1c37602009-07-21 01:34:57 +0000179 Send break condition. Timed, returns to idle state after given
180 duration.
181
cliechtic1c37602009-07-21 01:34:57 +0000182
Chris Liechti256ea212015-08-29 23:57:00 +0200183 .. attribute:: break_condition
cliechtibb5c3c52009-07-23 02:29:27 +0000184
Chris Liechti256ea212015-08-29 23:57:00 +0200185 :getter: Get the current BREAK state
186 :setter: Control the BREAK state
187 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000188
Chris Liechti256ea212015-08-29 23:57:00 +0200189 When set to ``True`` activate BREAK condition, else disable.
190 Controls TXD. When active, no transmitting is possible.
cliechtic1c37602009-07-21 01:34:57 +0000191
Chris Liechti256ea212015-08-29 23:57:00 +0200192 .. attribute:: rts
cliechtibb5c3c52009-07-23 02:29:27 +0000193
Chris Liechti256ea212015-08-29 23:57:00 +0200194 :setter: Set the state of the RTS line
195 :getter: Return the state of the RTS line
196 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000197
Chris Liechti256ea212015-08-29 23:57:00 +0200198 Set RTS line to specified logic level. It is possible to assign this
199 value before opening the serial port, then the value is applied uppon
200 :meth:`open`.
cliechtic1c37602009-07-21 01:34:57 +0000201
Chris Liechti256ea212015-08-29 23:57:00 +0200202 .. attribute:: dtr
cliechtibb5c3c52009-07-23 02:29:27 +0000203
Chris Liechti256ea212015-08-29 23:57:00 +0200204 :setter: Set the state of the DTR line
205 :getter: Return the state of the DTR line
206 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000207
Chris Liechti256ea212015-08-29 23:57:00 +0200208 Set DTR line to specified logic level. It is possible to assign this
209 value before opening the serial port, then the value is applied uppon
210 :meth:`open`.
cliechtic1c37602009-07-21 01:34:57 +0000211
Chris Liechti256ea212015-08-29 23:57:00 +0200212 .. attribute:: cts
213
214 :getter: Get the state of the CTS line
215 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000216
cliechtic1c37602009-07-21 01:34:57 +0000217 Return the state of the CTS line.
218
Chris Liechti256ea212015-08-29 23:57:00 +0200219 .. attribute:: dsr
cliechtic1c37602009-07-21 01:34:57 +0000220
Chris Liechti256ea212015-08-29 23:57:00 +0200221 :getter: Get the state of the DSR line
222 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000223
cliechtic1c37602009-07-21 01:34:57 +0000224 Return the state of the DSR line.
225
Chris Liechti256ea212015-08-29 23:57:00 +0200226 .. attribute:: ri
cliechtic1c37602009-07-21 01:34:57 +0000227
Chris Liechti256ea212015-08-29 23:57:00 +0200228 :getter: Get the state of the RI line
229 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000230
cliechtic1c37602009-07-21 01:34:57 +0000231 Return the state of the RI line.
232
Chris Liechti256ea212015-08-29 23:57:00 +0200233 .. attribute:: cd
cliechtic1c37602009-07-21 01:34:57 +0000234
Chris Liechti256ea212015-08-29 23:57:00 +0200235 :getter: Get the state of the CD line
236 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000237
cliechtic1c37602009-07-21 01:34:57 +0000238 Return the state of the CD line
239
240 Read-only attributes:
241
cliechtif81362e2009-07-25 03:44:33 +0000242 .. attribute:: name
cliechtic1c37602009-07-21 01:34:57 +0000243
cliechti5134aab2009-07-21 19:47:59 +0000244 Device name. This is always the device name even if the
245 port was opened by a number. (Read Only).
cliechtic1c37602009-07-21 01:34:57 +0000246
cliechtif81362e2009-07-25 03:44:33 +0000247 .. versionadded:: 2.5
248
cliechti25375b52010-07-21 23:27:13 +0000249 New values can be assigned to the following attributes (properties), the
250 port will be reconfigured, even if it's opened at that time:
cliechtic1c37602009-07-21 01:34:57 +0000251
cliechtiedcdbe42009-07-22 00:48:34 +0000252
253 .. attribute:: port
254
255 Read or write port. When the port is already open, it will be closed
256 and reopened with the new setting.
257
258 .. attribute:: baudrate
259
cliechti6066f842009-07-24 00:05:45 +0000260 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000261
262 .. attribute:: bytesize
263
cliechti6066f842009-07-24 00:05:45 +0000264 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000265
266 .. attribute:: parity
267
cliechti6066f842009-07-24 00:05:45 +0000268 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000269
270 .. attribute:: stopbits
271
cliechti6066f842009-07-24 00:05:45 +0000272 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000273
274 .. attribute:: timeout
275
cliechti6066f842009-07-24 00:05:45 +0000276 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000277
278 .. attribute:: writeTimeout
279
cliechti6066f842009-07-24 00:05:45 +0000280 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000281
282 .. attribute:: xonxoff
283
cliechti6066f842009-07-24 00:05:45 +0000284 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000285
286 .. attribute:: rtscts
287
cliechti6066f842009-07-24 00:05:45 +0000288 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000289
290 .. attribute:: dsrdtr
291
cliechti6066f842009-07-24 00:05:45 +0000292 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000293
Chris Liechti256ea212015-08-29 23:57:00 +0200294 .. attribute:: inter_character_timeout
cliechtiedcdbe42009-07-22 00:48:34 +0000295
cliechti6066f842009-07-24 00:05:45 +0000296 Read or write current inter character timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000297
Chris Liechtice621882015-08-06 19:29:31 +0200298 .. attribute:: rs485_mode
299
300 :platform: Posix
301 :platform: Windows
302
303 Attribute to configure RS485 support. When set to an instance of
304 :class:`rs485.RS485Settings` and supported by OS, RTS will be active
305 when data is sent and inactive otherwise (for reception). The
306 :class:`rs485.RS485Settings` class provides additional settings
307 supported on some platforms.
308
309 .. versionadded:: 3.0
310
311
cliechtiedcdbe42009-07-22 00:48:34 +0000312 The following constants are also provided:
313
314 .. attribute:: BAUDRATES
315
cliechti25375b52010-07-21 23:27:13 +0000316 A list of valid baud rates. The list may be incomplete such that higher
317 baud rates may be supported by the device and that values in between the
318 standard baud rates are supported. (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000319
320 .. attribute:: BYTESIZES
321
cliechti25375b52010-07-21 23:27:13 +0000322 A list of valid byte sizes for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000323
324 .. attribute:: PARITIES
325
cliechti25375b52010-07-21 23:27:13 +0000326 A list of valid parities for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000327
328 .. attribute:: STOPBITS
329
cliechti25375b52010-07-21 23:27:13 +0000330 A list of valid stop bit widths for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000331
cliechti4a567a02009-07-27 22:09:31 +0000332
cliechti25375b52010-07-21 23:27:13 +0000333 The following methods are for compatibility with the :mod:`io` library.
cliechti4a567a02009-07-27 22:09:31 +0000334
335 .. method:: readable()
336
337 :return: True
cliechtif4566342009-08-04 00:07:19 +0000338
cliechti4a567a02009-07-27 22:09:31 +0000339 .. versionadded:: 2.5
340
341 .. method:: writable()
342
343 :return: True
cliechtif4566342009-08-04 00:07:19 +0000344
cliechti4a567a02009-07-27 22:09:31 +0000345 .. versionadded:: 2.5
346
347 .. method:: seekable()
348
349 :return: False
cliechtif4566342009-08-04 00:07:19 +0000350
cliechti4a567a02009-07-27 22:09:31 +0000351 .. versionadded:: 2.5
352
353 .. method:: readinto(b)
354
cliechti8611bf42009-08-03 00:34:03 +0000355 :param b: bytearray or array instance
cliechti4a567a02009-07-27 22:09:31 +0000356 :return: Number of byte read
357
cliechtid7e7ce22009-08-03 02:01:07 +0000358 Read up to len(b) bytes into :class:`bytearray` *b* and return the
359 number of bytes read.
cliechti4a567a02009-07-27 22:09:31 +0000360
361 .. versionadded:: 2.5
362
cliechti25375b52010-07-21 23:27:13 +0000363 The port settings can be read and written as dictionary.
364
Chris Liechti256ea212015-08-29 23:57:00 +0200365 .. method:: get_settings()
cliechti4065dce2009-08-10 00:55:46 +0000366
367 :return: a dictionary with current port settings.
368
369 Get a dictionary with port settings. This is useful to backup the
370 current settings so that a later point in time they can be restored
371 using :meth:`applySettingsDict`.
372
373 Note that control lines (RTS/DTR) are part of the settings.
374
375 .. versionadded:: 2.5
376
Chris Liechti256ea212015-08-29 23:57:00 +0200377 .. method:: apply_settings(d)
cliechti4065dce2009-08-10 00:55:46 +0000378
379 :param d: a dictionary with port settings.
380
381 Applies a dictionary that was created by :meth:`getSettingsDict`. Only
382 changes are applied and when a key is missing it means that the setting
383 stays unchanged.
384
385 Note that control lines (RTS/DTR) are not changed.
386
387 .. versionadded:: 2.5
388
cliechti25375b52010-07-21 23:27:13 +0000389 Platform specific methods.
390
cliechtic648da92011-12-29 01:17:51 +0000391 .. warning:: Programs using the following methods and attributes are not
392 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000393
Chris Liechti256ea212015-08-29 23:57:00 +0200394 .. attribute:: out_waiting
cliechti28b8fd02011-12-28 21:39:42 +0000395
Chris Liechtice621882015-08-06 19:29:31 +0200396 :platform: Posix
cliechti28b8fd02011-12-28 21:39:42 +0000397 :platform: Windows
398
399 Return the number of bytes in the output buffer.
400
401 .. versionchanged:: 2.7 (Posix support added)
Chris Liechti256ea212015-08-29 23:57:00 +0200402 .. versionchanged:: 3.0 changed to property from ``outWaiting()``
cliechti28b8fd02011-12-28 21:39:42 +0000403
cliechti25375b52010-07-21 23:27:13 +0000404 .. method:: nonblocking()
405
Chris Liechtice621882015-08-06 19:29:31 +0200406 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000407
408 Configure the device for nonblocking operation. This can be useful if
409 the port is used with :mod:`select`.
410
411 .. method:: fileno()
412
Chris Liechtice621882015-08-06 19:29:31 +0200413 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000414 :return: File descriptor.
415
416 Return file descriptor number for the port that is opened by this object.
417 It is useful when serial ports are used with :mod:`select`.
418
Chris Liechti256ea212015-08-29 23:57:00 +0200419 .. method:: set_output_flow_control(level=True)
cliechti25375b52010-07-21 23:27:13 +0000420
421 :platform: Windows
cliechti2f0f8a32011-12-28 22:10:00 +0000422 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000423 :param level: Set flow control state.
424
cliechti2f0f8a32011-12-28 22:10:00 +0000425 Manually control flow - when software flow control is enabled.
426
427 This will send XON (true) and XOFF (false) to the other device.
428
429 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
Chris Liechti256ea212015-08-29 23:57:00 +0200430 .. versionchanged:: 3.0 renamed from ``setXON``
cliechti2f0f8a32011-12-28 22:10:00 +0000431
Chris Liechti256ea212015-08-29 23:57:00 +0200432 .. method:: set_input_flow_control(enable)
cliechti2f0f8a32011-12-28 22:10:00 +0000433
434 :platform: Posix
435 :param enable: Set flow control state.
436
437 Manually control flow of outgoing data - when hardware or software flow
438 control is enabled.
439
440 Sending will be suspended when called with ``False`` and enabled when
441 called with ``True``.
442
443 .. versionadded:: 2.7 (Posix support added)
Chris Liechti256ea212015-08-29 23:57:00 +0200444 .. versionchanged:: 3.0 renamed from ``flowControlOut``
445
446
447 .. note:: Deprecated API
448
449 .. attribute:: portstr
450
451 .. deprecated:: use :attr:`name` instead
452
453 .. method:: inWaiting()
454
455 .. deprecated:: 3.0, see :attr:`in_waiting`
456
457 .. attribute:: interCharTimeout
458
459 .. deprecated:: 3.0, see :attr:`inter_character_timeout`
460
461 .. method:: sendBreak(duration=0.25)
462
463 .. deprecated:: 3.0, see :meth:`send_break`
464
465 .. method:: flushInput()
466
467 .. deprecated:: 3.0, see :meth:`reset_input_buffer`
468
469 .. method:: flushOutput()
470
471 .. deprecated:: 3.0, see :meth:`reset_output_buffer`
472
473 .. method:: setBreak(level=True)
474
475 .. deprecated:: 3.0, see :attr:`break_condition`
476
477 .. method:: setRTS(level=True)
478
479 .. deprecated:: 3.0, see :attr:`rts`
480
481 .. method:: setDTR(level=True)
482
483 .. deprecated:: 3.0, see :attr:`dtr`
484
485 .. method:: getCTS()
486
487 .. deprecated:: 3.0, see :attr:`cts`
488
489 .. method:: getDSR()
490
491 .. deprecated:: 3.0, see :attr:`dsr`
492
493 .. method:: getRI()
494
495 .. deprecated:: 3.0, see :attr:`ri`
496
497 .. method:: getCD()
498
499 .. deprecated:: 3.0, see :attr:`cd`
500
501 .. method:: getSettingsDict()
502
503 .. deprecated:: 3.0, see :meth:`get_settings`
504
505 .. method:: applySettingsDict(d)
506
507 .. deprecated:: 3.0, see :meth:`apply_settings`
508
509 .. method:: outWaiting()
510
511 .. deprecated:: 3.0, see :attr:`out_waiting`
512
513 .. method:: setXON(level=True)
514
515 .. deprecated:: 3.0, see :meth:`set_output_flow_control`
516
517 .. method:: flowControlOut(enable)
518
519 .. deprecated:: 3.0, see :meth:`set_input_flow_control`
cliechtif4566342009-08-04 00:07:19 +0000520
cliechtic648da92011-12-29 01:17:51 +0000521 .. attribute:: rtsToggle
522
523 :platform: Windows
524
525 Attribute to configure RTS toggle control setting. When enabled and
526 supported by OS, RTS will be active when data is available and inactive
527 if no data is available.
528
529 .. versionadded:: 2.6
Chris Liechtice621882015-08-06 19:29:31 +0200530 .. versionchanged:: 3.0 (removed, see :meth:`rs485_mode` instead)
cliechtic648da92011-12-29 01:17:51 +0000531
532
cliechtic56e41d2011-08-25 22:06:38 +0000533Implementation detail: some attributes and functions are provided by the
534class :class:`SerialBase` and some by the platform specific class and
535others by the base class mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000536
Chris Liechtice621882015-08-06 19:29:31 +0200537RS485 support
538-------------
539The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to
540enable RS485 specific support on some platforms. Currently Windows and Linux
541(only a small number of devices) are supported.
cliechti4a567a02009-07-27 22:09:31 +0000542
Chris Liechtice621882015-08-06 19:29:31 +0200543:attr:`Serial.rs485_mode` needs to be set to an instance of
544:class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature.
cliechti4a567a02009-07-27 22:09:31 +0000545
Chris Liechtice621882015-08-06 19:29:31 +0200546Usage::
cliechti4a567a02009-07-27 22:09:31 +0000547
Chris Liechtice621882015-08-06 19:29:31 +0200548 ser = serial.Serial(...)
549 ser.rs485_mode = serial.rs485.RS485Settings(...)
550 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000551
Chris Liechtice621882015-08-06 19:29:31 +0200552There is a subclass :class:`rs485.RS485` available to emulate the RS485 support
553on regular serial ports.
cliechti4a567a02009-07-27 22:09:31 +0000554
Chris Liechtice621882015-08-06 19:29:31 +0200555Usage::
556
557 ser = serial.rs485.RS485(...)
558 ser.rs485_mode = serial.rs485.RS485Settings(...)
559 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000560
561
Chris Liechtice621882015-08-06 19:29:31 +0200562.. class:: rs485.RS485Settings
cliechti4a567a02009-07-27 22:09:31 +0000563
Chris Liechtice621882015-08-06 19:29:31 +0200564 A class that holds RS485 specific settings which are supported on
565 some platforms.
cliechti4a567a02009-07-27 22:09:31 +0000566
Chris Liechtice621882015-08-06 19:29:31 +0200567 .. versionadded:: 3.0
cliechti4a567a02009-07-27 22:09:31 +0000568
Chris Liechtice621882015-08-06 19:29:31 +0200569 .. 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 +0000570
Chris Liechtice621882015-08-06 19:29:31 +0200571 :param bool rts_level_for_tx:
572 RTS level for transmission
cliechti4a567a02009-07-27 22:09:31 +0000573
Chris Liechtice621882015-08-06 19:29:31 +0200574 :param bool rts_level_for_rx:
575 RTS level for reception
cliechti4a567a02009-07-27 22:09:31 +0000576
Chris Liechtice621882015-08-06 19:29:31 +0200577 :param bool loopback:
578 When set to ``True`` transmitted data is also received.
cliechti4a567a02009-07-27 22:09:31 +0000579
Chris Liechtice621882015-08-06 19:29:31 +0200580 :param float delay_before_tx:
581 Delay after setting RTS but before transmission starts
582
583 :param float delay_before_rx:
584 Delay after transmission ends and resetting RTS
585
586 .. attribute:: rts_level_for_tx
587
588 RTS level for transmission.
589
590 .. attribute:: rts_level_for_rx
591
592 RTS level for reception.
593
594 .. attribute:: loopback
595
596 When set to ``True`` transmitted data is also received.
597
598 .. attribute:: delay_before_tx
599
600 Delay after setting RTS but before transmission starts (seconds as float).
601
602 .. attribute:: delay_before_rx
603
604 Delay after transmission ends and resetting RTS (seconds as float).
cliechti4a567a02009-07-27 22:09:31 +0000605
606
Chris Liechtice621882015-08-06 19:29:31 +0200607.. class:: rs485.RS485
cliechti4a567a02009-07-27 22:09:31 +0000608
Chris Liechtice621882015-08-06 19:29:31 +0200609 A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS
610 according to the RS485 settings.
cliechti4a567a02009-07-27 22:09:31 +0000611
Chris Liechtice621882015-08-06 19:29:31 +0200612 .. warning:: This may work unreliably on some serial ports (control signals not
613 synchronized or delayed compared to data). Using delays may be unreliable
614 (varying times, larger than expected) as the OS may not support very fine
615 grained delays (no smaller than in the order of tens of milliseconds).
cliechti4a567a02009-07-27 22:09:31 +0000616
Chris Liechtice621882015-08-06 19:29:31 +0200617 .. note:: Some implementations support this natively in the class
618 :class:`Serial`. Better performance can be expected when the native version
619 is used.
cliechti4a567a02009-07-27 22:09:31 +0000620
Chris Liechtice621882015-08-06 19:29:31 +0200621 .. note:: The loopback property is ignored by this implementation. The actual
622 behavior depends on the used hardware.
cliechti4a567a02009-07-27 22:09:31 +0000623
cliechti4a567a02009-07-27 22:09:31 +0000624
cliechtie214ff82010-07-21 15:48:47 +0000625
cliechti25375b52010-07-21 23:27:13 +0000626
cliechti7aed8332009-08-05 14:19:31 +0000627:rfc:`2217` Network ports
628-------------------------
629
630.. warning:: This implementation is currently in an experimental state. Use
631 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000632
cliechtiec4ac1b2009-08-02 00:47:21 +0000633.. class:: rfc2217.Serial
634
cliechtif4566342009-08-04 00:07:19 +0000635 This implements a :rfc:`2217` compatible client. Port names are URLs_ in the
636 form: ``rfc2217://<host>:<port>[/<option>[/<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000637
cliechtiec4ac1b2009-08-02 00:47:21 +0000638 This class API is compatible to :class:`Serial` with a few exceptions:
639
640 - numbers as port name are not allowed, only URLs in the form described
641 above.
642 - writeTimeout is not implemented
643 - The current implementation starts a thread that keeps reading from the
644 (internal) socket. The thread is managed automatically by the
645 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
646 However it may be a problem for user applications that like to use select
647 instead of threads.
648
649 Due to the nature of the network and protocol involved there are a few
650 extra points to keep in mind:
651
652 - All operations have an additional latency time.
653 - Setting control lines (RTS/CTS) needs more time.
654 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
655 cache is updated depends entirely on the server. The server itself may
656 implement a polling at a certain rate and quick changes may be invisible.
657 - The network layer also has buffers. This means that :meth:`flush`,
658 :meth:`flushInput` and :meth:`flushOutput` may work with additional delay.
659 Likewise :meth:`inWaiting` returns the size of the data arrived at the
660 object internal buffer and excludes any bytes in the network buffers or
661 any server side buffer.
662 - Closing and immediately reopening the same port may fail due to time
663 needed by the server to get ready again.
664
665 Not implemented yet / Possible problems with the implementation:
666
cliechti8611bf42009-08-03 00:34:03 +0000667 - :rfc:`2217` flow control between client and server (objects internal
668 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000669 - No authentication support (servers may not prompt for a password etc.)
670 - No encryption.
671
672 Due to lack of authentication and encryption it is not suitable to use this
673 client for connections across the internet and should only be used in
674 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000675
cliechti7c640ed2009-08-02 00:54:51 +0000676 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000677
cliechti7aed8332009-08-05 14:19:31 +0000678
679.. class:: rfc2217.PortManager
680
681 This class provides helper functions for implementing :rfc:`2217`
682 compatible servers.
683
684 Basically, it implements every thing needed for the :rfc:`2217` protocol.
685 It just does not open sockets and read/write to serial ports (though it
686 changes other port settings). The user of this class must take care of the
687 data transmission itself. The reason for that is, that this way, this class
688 supports all programming models such as threads and select.
689
690 Usage examples can be found in the examples where two TCP/IP - serial
691 converters are shown, one using threads (the single port server) and an
692 other using select (the multi port server).
693
694 .. note:: Each new client connection must create a new instance as this
695 object (and the :rfc:`2217` protocol) has internal state.
696
697 .. method:: __init__(serial_port, connection, debug_output=False)
698
699 :param serial_port: a :class:`Serial` instance that is managed.
700 :param connection: an object implementing :meth:`write`, used to write
701 to the network.
cliechti86844e82009-08-12 00:05:33 +0000702 :param debug_output: enables debug messages: a :class:`logging.Logger`
703 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000704
705 Initializes the Manager and starts negotiating with client in Telnet
706 and :rfc:`2217` protocol. The negotiation starts immediately so that
707 the class should be instantiated in the moment the client connects.
708
709 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000710 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000711 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
712 the :meth:`filter` method.
713
714 The *connection* object must implement a :meth:`write(data)` function.
715 This function must ensure that *data* is written at once (no user data
716 mixed in, i.e. it must be thread-safe). All data must be sent in its
717 raw form (:meth:`escape` must not be used) as it is used to send Telnet
718 and :rfc:`2217` control commands.
719
cliechti86844e82009-08-12 00:05:33 +0000720 For diagnostics of the connection or the implementation, *debug_output*
721 can be set to an instance of a :class:`logging.Logger` (e.g.
722 ``logging.getLogger('rfc2217.server')``). The caller should configure
723 the logger using ``setLevel`` for the desired detail level of the logs.
724
cliechti7aed8332009-08-05 14:19:31 +0000725 .. method:: escape(data)
726
727 :param data: data to be sent over the network.
728 :return: data, escaped for Telnet/:rfc:`2217`
729
730 A generator that escapes all data to be compatible with :rfc:`2217`.
731 Implementors of servers should use this function to process all data
732 sent over the network.
733
734 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000735 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000736
737 .. method:: filter(data)
738
739 :param data: data read from the network, including Telnet and
740 :rfc:`2217` controls.
741 :return: data, free from Telnet and :rfc:`2217` controls.
742
743 A generator that filters and processes all data related to :rfc:`2217`.
744 Implementors of servers should use this function to process all data
745 received from the network.
746
747 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000748 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000749
cliechti7cb78e82009-08-05 15:47:57 +0000750 .. method:: check_modem_lines(force_notification=False)
751
752 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000753
754 This function needs to be called periodically (e.g. every second) when
755 the server wants to send NOTIFY_MODEMSTATE messages. This is required
756 to support the client for reading CTS/DSR/RI/CD status lines.
757
758 The function reads the status line and issues the notifications
759 automatically.
760
761 .. versionadded:: 2.5
762
cliechtiec4ac1b2009-08-02 00:47:21 +0000763.. seealso::
764
765 :rfc:`2217` - Telnet Com Port Control Option
766
767
cliechtic1c37602009-07-21 01:34:57 +0000768Exceptions
769==========
770
771.. exception:: SerialException
772
773 Base class for serial port exceptions.
774
cliechti4a567a02009-07-27 22:09:31 +0000775 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000776 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000777
cliechtic1c37602009-07-21 01:34:57 +0000778.. exception:: SerialTimeoutException
779
780 Exception that is raised on write timeouts.
781
782
783Constants
784=========
785
cliechti87686242009-08-18 00:58:31 +0000786*Parity*
787
cliechtic1c37602009-07-21 01:34:57 +0000788.. data:: PARITY_NONE
789.. data:: PARITY_EVEN
790.. data:: PARITY_ODD
791.. data:: PARITY_MARK
792.. data:: PARITY_SPACE
793
cliechti87686242009-08-18 00:58:31 +0000794*Stop bits*
795
cliechtic1c37602009-07-21 01:34:57 +0000796.. data:: STOPBITS_ONE
797.. data:: STOPBITS_ONE_POINT_FIVE
798.. data:: STOPBITS_TWO
799
cliechti06281be2011-08-25 23:08:29 +0000800Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +0000801bits.
802
cliechti87686242009-08-18 00:58:31 +0000803*Byte size*
804
cliechtic1c37602009-07-21 01:34:57 +0000805.. data:: FIVEBITS
806.. data:: SIXBITS
807.. data:: SEVENBITS
808.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000809
cliechti87686242009-08-18 00:58:31 +0000810
811*Others*
812
cliechti8611bf42009-08-03 00:34:03 +0000813Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000814software flow control:
cliechti6066f842009-07-24 00:05:45 +0000815
cliechti5b7d16a2009-07-21 21:53:59 +0000816.. data:: XON
817.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000818
cliechti4a567a02009-07-27 22:09:31 +0000819Module version:
cliechtif81362e2009-07-25 03:44:33 +0000820
821.. data:: VERSION
822
823 A string indicating the pySerial version, such as ``2.5``.
824
cliechti87686242009-08-18 00:58:31 +0000825 .. versionadded:: 2.3
826
cliechti44eb98f2011-03-21 21:41:10 +0000827
cliechtie542b362011-03-18 00:49:16 +0000828Module functions and attributes
829===============================
cliechtif81362e2009-07-25 03:44:33 +0000830
831.. function:: device(number)
832
833 :param number: Port number.
834 :return: String containing device name.
835 :deprecated: Use device names directly.
836
837 Convert a port number to a platform dependent device name. Unfortunately
838 this does not work well for all platforms; e.g. some may miss USB-Serial
839 converters and enumerate only internal serial ports.
840
841 The conversion may be made off-line, that is, there is no guarantee that
842 the returned device name really exists on the system.
cliechti7c640ed2009-08-02 00:54:51 +0000843
844
cliechtie3ab3532009-08-05 12:40:38 +0000845.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +0000846
cliechti86844e82009-08-12 00:05:33 +0000847 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +0000848 :param do_not_open: When set to true, the serial port is not opened.
849 :return: an instance of :class:`Serial` or a compatible object.
850
851 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +0000852 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +0000853 to support both, local ports and remote ports. There is also support
854 for other types, see :ref:`URL <URLs>` section below.
cliechti7c640ed2009-08-02 00:54:51 +0000855
cliechtid7e7ce22009-08-03 02:01:07 +0000856 The port is not opened when a keyword parameter called *do_not_open* is
857 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +0000858
859 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +0000860
cliechti87686242009-08-18 00:58:31 +0000861
cliechtie542b362011-03-18 00:49:16 +0000862.. attribute:: protocol_handler_packages
863
864 This attribute is a list of package names (strings) that is searched for
865 protocol handlers.
866
867 e.g. we want to support a URL ``foobar://``. A module
868 ``my_handlers.protocol_foobar`` is provided by the user::
869
870 serial.protocol_handler_packages.append("my_handlers")
871 s = serial.serial_for_url("foobar://")
872
873 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
874 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
875 ``PACKAGE`` from this list.
876
877 .. versionadded:: 2.6
878
879
cliechti25375b52010-07-21 23:27:13 +0000880.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +0000881
882 :param sequence: String or list of integers
cliechti25375b52010-07-21 23:27:13 +0000883 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +0000884
cliechtie542b362011-03-18 00:49:16 +0000885 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +0000886 compatible to Python 2.x and 3.x.
887
cliechtie542b362011-03-18 00:49:16 +0000888 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +0000889 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +0000890 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +0000891
892 This function is used internally and in the unit tests.
893
cliechtie542b362011-03-18 00:49:16 +0000894 .. versionadded:: 2.5
895
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200896.. function:: iterbytes(b)
897
898 :param b: bytes, bytearray or memoryview
899 :returns: a generator that yields bytes
900
901 Some versions of Python (3.x) would return integers instead of bytes when
902 looping over an instance of ``bytes``. This helper function ensures that
903 bytes are returned.
904
905 .. versionadded:: 3.0
906
cliechtie214ff82010-07-21 15:48:47 +0000907
cliechti86844e82009-08-12 00:05:33 +0000908.. _URLs:
cliechtif4566342009-08-04 00:07:19 +0000909
910URLs
911----
cliechtie214ff82010-07-21 15:48:47 +0000912The function :func:`serial_for_url` accepts the following types of URLs:
cliechtif4566342009-08-04 00:07:19 +0000913
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200914- ``rfc2217://<host>:<port>[?<option>[&<option>...]]``
915- ``socket://<host>:<port>[?logging={debug|info|warning|error}]``
916- ``loop://[?logging={debug|info|warning|error}]``
Chris Liechtie9e27ff2015-08-20 23:46:51 +0200917- ``spy://port[?option[=value][&option[=value]]]``
cliechtif4566342009-08-04 00:07:19 +0000918
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200919.. versionchanged:: 3.0 Options are specified with ``?`` and ``&`` instead of ``/``
920
cliechtie214ff82010-07-21 15:48:47 +0000921Device names are also supported, e.g.:
cliechti87686242009-08-18 00:58:31 +0000922
923- ``/dev/ttyUSB0`` (Linux)
924- ``COM3`` (Windows)
925
cliechtie542b362011-03-18 00:49:16 +0000926Future releases of pySerial might add more types. Since pySerial 2.6 it is also
927possible for the user to add protocol handlers using
928:attr:`protocol_handler_packages`.
cliechtif4566342009-08-04 00:07:19 +0000929
cliechtiab90e072009-08-06 01:44:34 +0000930``rfc2217://``
931 Used to connect to :rfc:`2217` compatible servers. All serial port
cliechti25375b52010-07-21 23:27:13 +0000932 functions are supported. Implemented by :class:`rfc2217.Serial`.
cliechtiab90e072009-08-06 01:44:34 +0000933
934 Supported options in the URL are:
935
936 - ``ign_set_control`` does not wait for acknowledges to SET_CONTROL. This
937 option can be used for non compliant servers (i.e. when getting an
938 ``remote rejected value for option 'control'`` error when connecting).
939
940 - ``poll_modem``: The client issues NOTIFY_MODEMSTATE requests when status
941 lines are read (CTS/DTR/RI/CD). Without this option it relies on the server
942 sending the notifications automatically (that's what the RFC suggests and
943 most servers do). Enable this option when :meth:`getCTS` does not work as
944 expected, i.e. for servers that do not send notifications.
945
cliechtidfe2d272009-08-10 22:19:41 +0000946 - ``timeout=<value>``: Change network timeout (default 3 seconds). This is
947 useful when the server takes a little more time to send its answers. The
948 timeout applies to the initial Telnet / :rfc:`2271` negotiation as well
949 as changing port settings or control line change commands.
950
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200951 - ``logging={debug|info|warning|error}``: Prints diagnostic messages (not
cliechtic64ba692009-08-12 00:32:47 +0000952 useful for end users). It uses the logging module and a logger called
953 ``pySerial.rfc2217`` so that the application can setup up logging
954 handlers etc. It will call :meth:`logging.basicConfig` which initializes
955 for output on ``sys.stderr`` (if no logging was set up already).
cliechtiab90e072009-08-06 01:44:34 +0000956
957``socket://``
958 The purpose of this connection type is that applications using pySerial can
959 connect to TCP/IP to serial port converters that do not support :rfc:`2217`.
960
961 Uses a TCP/IP socket. All serial port settings, control and status lines
962 are ignored. Only data is transmitted and received.
963
964 Supported options in the URL are:
965
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200966 - ``logging={debug|info|warning|error}``: Prints diagnostic messages (not
cliechtic64ba692009-08-12 00:32:47 +0000967 useful for end users). It uses the logging module and a logger called
968 ``pySerial.socket`` so that the application can setup up logging handlers
969 etc. It will call :meth:`logging.basicConfig` which initializes for
970 output on ``sys.stderr`` (if no logging was set up already).
cliechtiab90e072009-08-06 01:44:34 +0000971
cliechti41973a92009-08-06 02:18:21 +0000972``loop://``
cliechtie214ff82010-07-21 15:48:47 +0000973 The least useful type. It simulates a loop back connection
974 (``RX<->TX`` ``RTS<->CTS`` ``DTR<->DSR``). It could be used to test
975 applications or run the unit tests.
cliechti41973a92009-08-06 02:18:21 +0000976
977 Supported options in the URL are:
978
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200979 - ``logging={debug|info|warning|error}``: Prints diagnostic messages (not
cliechtic64ba692009-08-12 00:32:47 +0000980 useful for end users). It uses the logging module and a logger called
981 ``pySerial.loop`` so that the application can setup up logging handlers
982 etc. It will call :meth:`logging.basicConfig` which initializes for
983 output on ``sys.stderr`` (if no logging was set up already).
cliechti41973a92009-08-06 02:18:21 +0000984
cliechticdc660c2011-11-03 23:24:31 +0000985``hwgrep://``
986 This type uses :mod:`serial.tools.list_ports` to obtain a list of ports and
987 searches the list for matches by a regexp (see :py:mod:`re`) that follows
988 the slashes.
989
990 Depending on the capabilities of the list_ports module on the system, it is
991 possible to search for the description or hardware ID of a device, e.g. USB
992 VID:PID or texts.
993
994 Unfortunately, on some systems list_ports only lists a subset of the port
Chris Liechtice621882015-08-06 19:29:31 +0200995 names with no additional information. Currently, on Windows and Linux and
996 OSX it should find additional information.
cliechticdc660c2011-11-03 23:24:31 +0000997
Chris Liechtie9e27ff2015-08-20 23:46:51 +0200998``spy://``
999 Wrapping the native serial port, this protocol makes it possible to
1000 intercept the data received and transmitted as well as the access to the
1001 control lines, break and flush commands.
1002
1003 Supported options in the URL are:
1004
Chris Liechti876801a2015-08-21 23:15:45 +02001005 - ``file=FILENAME`` output to given file or device instead of stderr
Chris Liechtie9e27ff2015-08-20 23:46:51 +02001006 - ``color`` enable ANSI escape sequences to colorize output
1007 - ``raw`` output the read and written data directly (default is to create a
1008 hex dump). In this mode, no control line and other commands are logged.
Chris Liechti686117d2015-08-22 00:19:08 +02001009 - ``all`` also show ``inWaiting()`` and empty ``read()`` calls (hidden by
1010 default because of high traffic).
Chris Liechtie9e27ff2015-08-20 23:46:51 +02001011
1012 Example::
1013
1014 import serial
1015
Chris Liechti876801a2015-08-21 23:15:45 +02001016 with serial.serial_for_url('spy:///dev/ttyUSB0?file=test.txt', timeout=1) as s:
Chris Liechtie9e27ff2015-08-20 23:46:51 +02001017 s.setDTR(False)
1018 s.write('hello world')
1019 s.read(20)
1020 s.setDTR(True)
1021 s.write(serial.to_bytes(range(256)))
1022 s.read(400)
1023 s.sendBreak()
1024
1025 with open('test.txt') as f:
1026 print(f.read())
1027
1028 Outputs::
1029
1030 000000.002 FLSH flushInput
1031 000000.002 DTR inactive
1032 000000.002 TX 0000 68 65 6C 6C 6F 20 77 6F 72 6C 64 hello wo rld
1033 000001.015 RX 0000 68 65 6C 6C 6F 20 77 6F 72 6C 64 hello wo rld
1034 000001.015 DTR active
1035 000001.015 TX 0000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ........ ........
1036 000001.015 TX 0010 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ........ ........
1037 000001.015 TX 0020 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&' ()*+,-./
1038 000001.015 TX 0030 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 01234567 89:;<=>?
1039 000001.015 TX 0040 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F @ABCDEFG HIJKLMNO
1040 000001.016 TX 0050 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F PQRSTUVW XYZ[\]^_
1041 000001.016 TX 0060 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F `abcdefg hijklmno
1042 000001.016 TX 0070 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F pqrstuvw xyz{|}~.
1043 000001.016 TX 0080 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F ........ ........
1044 000001.016 TX 0090 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F ........ ........
1045 000001.016 TX 00A0 A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF ........ ........
1046 000001.016 TX 00B0 B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF ........ ........
1047 000001.016 TX 00C0 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF ........ ........
1048 000001.016 TX 00D0 D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF ........ ........
1049 000001.016 TX 00E0 E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF ........ ........
1050 000001.016 TX 00F0 F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF ........ ........
1051 000002.284 RX 0000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ........ ........
1052 000002.284 RX 0010 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ........ ........
1053 000002.284 RX 0020 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&' ()*+,-./
1054 000002.284 RX 0030 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 01234567 89:;<=>?
1055 000002.284 RX 0040 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F @ABCDEFG HIJKLMNO
1056 000002.284 RX 0050 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F PQRSTUVW XYZ[\]^_
1057 000002.284 RX 0060 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F `abcdefg hijklmno
1058 000002.284 RX 0070 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F pqrstuvw xyz{|}~.
1059 000002.284 RX 0080 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F ........ ........
1060 000002.284 RX 0090 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F ........ ........
1061 000002.284 RX 00A0 A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF ........ ........
1062 000002.284 RX 00B0 B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF ........ ........
1063 000002.284 RX 00C0 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF ........ ........
1064 000002.284 RX 00D0 D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF ........ ........
1065 000002.284 RX 00E0 E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF ........ ........
1066 000002.284 RX 00F0 F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF ........ ........
1067 000002.284 BRK sendBreak 0.25
1068
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001069 .. versionadded:: 3.0
1070
cliechticdc660c2011-11-03 23:24:31 +00001071
cliechti41973a92009-08-06 02:18:21 +00001072
cliechtidfe2d272009-08-10 22:19:41 +00001073Examples:
cliechtif4566342009-08-04 00:07:19 +00001074
cliechtidfe2d272009-08-10 22:19:41 +00001075- ``rfc2217://localhost:7000``
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001076- ``rfc2217://localhost:7000?poll_modem``
1077- ``rfc2217://localhost:7000?ign_set_control&timeout=5.5``
cliechtidfe2d272009-08-10 22:19:41 +00001078- ``socket://localhost:7777``
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001079- ``loop://?logging=debug``
cliechticdc660c2011-11-03 23:24:31 +00001080- ``hwgrep://0451:f432`` (USB VID:PID)
Chris Liechti876801a2015-08-21 23:15:45 +02001081- ``spy://COM54?file=log.txt``
cliechtic56e41d2011-08-25 22:06:38 +00001082
1083Tools
1084=====
1085
1086
1087serial.tools.list_ports
1088-----------------------
1089.. module:: serial.tools.list_ports
1090.. versionadded:: 2.6
1091
cliechti06281be2011-08-25 23:08:29 +00001092This module can be executed to get a list of ports (``python -m
1093serial.tools.list_ports``). It also contains the following functions.
cliechtic56e41d2011-08-25 22:06:38 +00001094
1095
1096.. function:: comports()
1097
1098 :return: an iterable.
1099
1100 The function returns an iterable that yields tuples of three strings:
1101
cliechti06281be2011-08-25 23:08:29 +00001102 - port name as it can be passed to :class:`serial.Serial` or
cliechtic56e41d2011-08-25 22:06:38 +00001103 :func:`serial.serial_for_url`
cliechti06281be2011-08-25 23:08:29 +00001104 - description in human readable form
1105 - sort of hardware ID. E.g. may contain VID:PID of USB-serial adapters.
cliechtic56e41d2011-08-25 22:06:38 +00001106
cliechti06281be2011-08-25 23:08:29 +00001107 Items are returned in no particular order. It may make sense to sort the
1108 items. Also note that the reported strings are different across platforms
1109 and operating systems, even for the same device.
cliechtic56e41d2011-08-25 22:06:38 +00001110
1111 .. note:: Support is limited to a number of operating systems. On some
cliechti06281be2011-08-25 23:08:29 +00001112 systems description and hardware ID will not be available
cliechtic56e41d2011-08-25 22:06:38 +00001113 (``None``).
1114
1115 :platform: Posix (/dev files)
1116 :platform: Linux (/dev files, sysfs and lsusb)
cliechti4cb94662013-10-17 03:17:50 +00001117 :platform: OSX (iokit)
cliechtic56e41d2011-08-25 22:06:38 +00001118 :platform: Windows (setupapi, registry)
1119
1120
1121.. function:: grep(regexp)
1122
1123 :param regexp: regular expression (see stdlib :mod:`re`)
cliechti06281be2011-08-25 23:08:29 +00001124 :return: filtered sequence, see :func:`comports`.
cliechtic56e41d2011-08-25 22:06:38 +00001125
1126 Search for ports using a regular expression. Port name, description and
cliechti06281be2011-08-25 23:08:29 +00001127 hardware ID are searched (case insensitive). The function returns an
1128 iterable that contains the same tuples that :func:`comport` generates but
1129 only those that match the regexp.
cliechtic56e41d2011-08-25 22:06:38 +00001130
1131
1132serial.tools.miniterm
1133-----------------------
1134.. module:: serial.tools.miniterm
1135.. versionadded:: 2.6
1136
1137Miniterm is now available as module instead of example.
1138see :ref:`miniterm` for details.
1139