blob: 75e19e72bec693f5601ea612373ef5d0fa8511b2 [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
Chris Liechti518b0d32015-08-30 02:20:39 +020015 .. method:: __init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, write_timeout=None, dsrdtr=False, inter_byte_timeout=None)
cliechtic1c37602009-07-21 01:34:57 +000016
17 :param port:
Chris Liechti518b0d32015-08-30 02:20:39 +020018 Device name 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 Liechti589c92a2015-09-04 23:04:34 +020050 :param float write_timeout:
cliechti07709e42010-05-21 00:30:09 +000051 Set a write timeout value.
52
Chris Liechti589c92a2015-09-04 23:04:34 +020053 :param float inter_byte_timeout:
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
Chris Liechti32ccf2e2015-12-19 23:42:40 +010065 to :meth:`open` is required.
cliechti4a567a02009-07-27 22:09:31 +000066
Chris Liechti518b0d32015-08-30 02:20:39 +020067 *port* is a device name: depending on operating system. e.g.
68 ``/dev/ttyUSB0`` on GNU/Linux or ``COM3`` on Windows.
cliechtic1c37602009-07-21 01:34:57 +000069
cliechti25375b52010-07-21 23:27:13 +000070 The parameter *baudrate* can be one of the standard values:
71 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
72 9600, 19200, 38400, 57600, 115200.
cliechti28b8fd02011-12-28 21:39:42 +000073 These are well supported on all platforms.
74
75 Standard values above 115200, such as: 230400, 460800, 500000, 576000,
76 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000,
77 4000000 also work on many platforms and devices.
cliechti25375b52010-07-21 23:27:13 +000078
cliechti06281be2011-08-25 23:08:29 +000079 Non-standard values are also supported on some platforms (GNU/Linux, MAC
cliechti25375b52010-07-21 23:27:13 +000080 OSX >= Tiger, Windows). Though, even on these platforms some serial
81 ports may reject non-standard values.
82
Chris Liechti3e0dcc72015-12-18 23:23:26 +010083 Possible values for the parameter *timeout* which controls the behavior
84 of :meth:`read`:
cliechti5134aab2009-07-21 19:47:59 +000085
Chris Liechti3e0dcc72015-12-18 23:23:26 +010086 - ``timeout = None``: wait forever / until requested number of bytes
87 are received
88 - ``timeout = 0``: non-blocking mode, return immediately in any case,
89 returning zero or more, up to the requested number of bytes
cliechtif81362e2009-07-25 03:44:33 +000090 - ``timeout = x``: set timeout to ``x`` seconds (float allowed)
Chris Liechti3e0dcc72015-12-18 23:23:26 +010091 returns immediately when the requested number of bytes are available,
92 otherwise wait until the timeout expires and return all bytes that
93 were received until then.
cliechtic1c37602009-07-21 01:34:57 +000094
Chris Liechti3e0dcc72015-12-18 23:23:26 +010095 :meth:`write` is blocking by default, unless *write_timeout* is set.
96 For possible values refer to the list for *timeout* above.
cliechti07709e42010-05-21 00:30:09 +000097
cliechti8611bf42009-08-03 00:34:03 +000098 Note that enabling both flow control methods (*xonxoff* and *rtscts*)
99 together may not be supported. It is common to use one of the methods
100 at once, not both.
cliechtic1c37602009-07-21 01:34:57 +0000101
cliechti07709e42010-05-21 00:30:09 +0000102 *dsrdtr* is not supported by all platforms (silently ignored). Setting
103 it to ``None`` has the effect that its state follows *rtscts*.
104
cliechti25375b52010-07-21 23:27:13 +0000105 Also consider using the function :func:`serial_for_url` instead of
106 creating Serial instances directly.
107
cliechti5c72e4d2010-07-21 14:04:54 +0000108 .. versionchanged:: 2.5
cliechti06281be2011-08-25 23:08:29 +0000109 *dsrdtr* now defaults to ``False`` (instead of *None*)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100110 .. versionchanged:: 3.0 numbers as *port* argument are no longer supported
cliechti5c72e4d2010-07-21 14:04:54 +0000111
cliechtic1c37602009-07-21 01:34:57 +0000112 .. method:: open()
113
114 Open port.
115
116 .. method:: close()
117
118 Close port immediately.
119
cliechti25375b52010-07-21 23:27:13 +0000120 .. method:: __del__()
121
122 Destructor, close port when serial port instance is freed.
123
cliechtic1c37602009-07-21 01:34:57 +0000124
Chris Liechti256ea212015-08-29 23:57:00 +0200125 The following methods may raise :exc:`SerialException` when applied to a closed
cliechti4a567a02009-07-27 22:09:31 +0000126 port.
cliechtic1c37602009-07-21 01:34:57 +0000127
128 .. method:: read(size=1)
129
cliechtibb5c3c52009-07-23 02:29:27 +0000130 :param size: Number of bytes to read.
131 :return: Bytes read from the port.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100132 :rtype: bytes
cliechtic1c37602009-07-21 01:34:57 +0000133
cliechti4a567a02009-07-27 22:09:31 +0000134 Read *size* bytes from the serial port. If a timeout is set it may
cliechtibb5c3c52009-07-23 02:29:27 +0000135 return less characters as requested. With no timeout it will block
136 until the requested number of bytes is read.
cliechtic1c37602009-07-21 01:34:57 +0000137
cliechti4a567a02009-07-27 22:09:31 +0000138 .. versionchanged:: 2.5
139 Returns an instance of :class:`bytes` when available (Python 2.6
cliechti8611bf42009-08-03 00:34:03 +0000140 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000141
cliechtibb5c3c52009-07-23 02:29:27 +0000142 .. method:: write(data)
143
144 :param data: Data to send.
cliechti4a567a02009-07-27 22:09:31 +0000145 :return: Number of bytes written.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100146 :rtype: int
cliechti6066f842009-07-24 00:05:45 +0000147 :exception SerialTimeoutException:
148 In case a write timeout is configured for the port and the time is
149 exceeded.
150
Chris Liechti9ad044a2015-12-17 19:43:59 +0100151 Write the bytes *data* to the port. This should be of type ``bytes``
152 (or compatible such as ``bytearray`` or ``memoryview``). Unicode
153 strings must be encoded (e.g. ``'hello'.encode('utf-8'``).
cliechtic1c37602009-07-21 01:34:57 +0000154
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100155 .. versionchanged:: 2.5
cliechtiddd78132009-07-28 01:13:28 +0000156 Accepts instances of :class:`bytes` and :class:`bytearray` when
cliechti8611bf42009-08-03 00:34:03 +0000157 available (Python 2.6 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000158
cliechti9a1c6952009-10-20 22:18:28 +0000159 .. versionchanged:: 2.5
160 Write returned ``None`` in previous versions.
161
Chris Liechti518b0d32015-08-30 02:20:39 +0200162 .. method:: flush()
163
164 Flush of file like objects. In this case, wait until all data is
165 written.
166
Chris Liechti256ea212015-08-29 23:57:00 +0200167 .. attribute:: in_waiting
cliechti4a567a02009-07-27 22:09:31 +0000168
Chris Liechti256ea212015-08-29 23:57:00 +0200169 :getter: Get the number of bytes in the input buffer
170 :type: int
171
172 Return the number of bytes in the receive buffer.
cliechti4a567a02009-07-27 22:09:31 +0000173
Chris Liechti518b0d32015-08-30 02:20:39 +0200174 .. versionchanged:: 3.0 changed to property from ``inWaiting()``
cliechtic1c37602009-07-21 01:34:57 +0000175
Chris Liechti518b0d32015-08-30 02:20:39 +0200176 .. attribute:: out_waiting
177
178 :getter: Get the number of bytes in the output buffer
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100179 :type: int
Chris Liechti518b0d32015-08-30 02:20:39 +0200180 :platform: Posix
181 :platform: Windows
182
183 Return the number of bytes in the output buffer.
184
185 .. versionchanged:: 2.7 (Posix support added)
186 .. versionchanged:: 3.0 changed to property from ``outWaiting()``
cliechtic1c37602009-07-21 01:34:57 +0000187
Chris Liechti256ea212015-08-29 23:57:00 +0200188 .. method:: reset_input_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000189
190 Flush input buffer, discarding all it's contents.
191
Chris Liechti518b0d32015-08-30 02:20:39 +0200192 .. versionchanged:: 3.0 renamed from ``flushInput()``
193
Chris Liechti256ea212015-08-29 23:57:00 +0200194 .. method:: reset_output_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000195
196 Clear output buffer, aborting the current output and
197 discarding all that is in the buffer.
198
Chris Liechti48e40e92016-05-24 00:09:12 +0200199 Note, for some USB serial adapters, this may only flush the buffer of
200 the OS and not all the data that may be present in the USB part.
201
Chris Liechti518b0d32015-08-30 02:20:39 +0200202 .. versionchanged:: 3.0 renamed from ``flushOutput()``
203
Chris Liechti256ea212015-08-29 23:57:00 +0200204 .. method:: send_break(duration=0.25)
cliechtic1c37602009-07-21 01:34:57 +0000205
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100206 :param float duration: Time to activate the BREAK condition.
cliechtibb5c3c52009-07-23 02:29:27 +0000207
cliechtic1c37602009-07-21 01:34:57 +0000208 Send break condition. Timed, returns to idle state after given
209 duration.
210
cliechtic1c37602009-07-21 01:34:57 +0000211
Chris Liechti256ea212015-08-29 23:57:00 +0200212 .. attribute:: break_condition
cliechtibb5c3c52009-07-23 02:29:27 +0000213
Chris Liechti256ea212015-08-29 23:57:00 +0200214 :getter: Get the current BREAK state
215 :setter: Control the BREAK state
216 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000217
Chris Liechti256ea212015-08-29 23:57:00 +0200218 When set to ``True`` activate BREAK condition, else disable.
219 Controls TXD. When active, no transmitting is possible.
cliechtic1c37602009-07-21 01:34:57 +0000220
Chris Liechti256ea212015-08-29 23:57:00 +0200221 .. attribute:: rts
cliechtibb5c3c52009-07-23 02:29:27 +0000222
Chris Liechti256ea212015-08-29 23:57:00 +0200223 :setter: Set the state of the RTS line
224 :getter: Return the state of the RTS line
225 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000226
Chris Liechti256ea212015-08-29 23:57:00 +0200227 Set RTS line to specified logic level. It is possible to assign this
228 value before opening the serial port, then the value is applied uppon
229 :meth:`open`.
cliechtic1c37602009-07-21 01:34:57 +0000230
Chris Liechti256ea212015-08-29 23:57:00 +0200231 .. attribute:: dtr
cliechtibb5c3c52009-07-23 02:29:27 +0000232
Chris Liechti256ea212015-08-29 23:57:00 +0200233 :setter: Set the state of the DTR line
234 :getter: Return the state of the DTR line
235 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000236
Chris Liechti256ea212015-08-29 23:57:00 +0200237 Set DTR line to specified logic level. It is possible to assign this
238 value before opening the serial port, then the value is applied uppon
239 :meth:`open`.
cliechtic1c37602009-07-21 01:34:57 +0000240
Chris Liechti518b0d32015-08-30 02:20:39 +0200241 Read-only attributes:
242
243 .. attribute:: name
244
245 :getter: Device name.
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100246 :type: str
Chris Liechti518b0d32015-08-30 02:20:39 +0200247
248 .. versionadded:: 2.5
249
Chris Liechti256ea212015-08-29 23:57:00 +0200250 .. attribute:: cts
251
252 :getter: Get the state of the CTS line
253 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000254
cliechtic1c37602009-07-21 01:34:57 +0000255 Return the state of the CTS line.
256
Chris Liechti256ea212015-08-29 23:57:00 +0200257 .. attribute:: dsr
cliechtic1c37602009-07-21 01:34:57 +0000258
Chris Liechti256ea212015-08-29 23:57:00 +0200259 :getter: Get the state of the DSR line
260 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000261
cliechtic1c37602009-07-21 01:34:57 +0000262 Return the state of the DSR line.
263
Chris Liechti256ea212015-08-29 23:57:00 +0200264 .. attribute:: ri
cliechtic1c37602009-07-21 01:34:57 +0000265
Chris Liechti256ea212015-08-29 23:57:00 +0200266 :getter: Get the state of the RI line
267 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000268
cliechtic1c37602009-07-21 01:34:57 +0000269 Return the state of the RI line.
270
Chris Liechti256ea212015-08-29 23:57:00 +0200271 .. attribute:: cd
cliechtic1c37602009-07-21 01:34:57 +0000272
Chris Liechti256ea212015-08-29 23:57:00 +0200273 :getter: Get the state of the CD line
274 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000275
cliechtic1c37602009-07-21 01:34:57 +0000276 Return the state of the CD line
277
cliechtif81362e2009-07-25 03:44:33 +0000278
cliechti25375b52010-07-21 23:27:13 +0000279 New values can be assigned to the following attributes (properties), the
280 port will be reconfigured, even if it's opened at that time:
cliechtic1c37602009-07-21 01:34:57 +0000281
cliechtiedcdbe42009-07-22 00:48:34 +0000282
283 .. attribute:: port
284
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100285 :type: str
286
cliechtiedcdbe42009-07-22 00:48:34 +0000287 Read or write port. When the port is already open, it will be closed
288 and reopened with the new setting.
289
290 .. attribute:: baudrate
291
Chris Liechti518b0d32015-08-30 02:20:39 +0200292 :getter: Get current baud rate
293 :setter: Set new baud rate
294 :type: int
295
cliechti6066f842009-07-24 00:05:45 +0000296 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000297
298 .. attribute:: bytesize
299
Chris Liechti518b0d32015-08-30 02:20:39 +0200300 :getter: Get current byte size
301 :setter: Set new byte size. Possible values:
302 :const:`FIVEBITS`, :const:`SIXBITS`, :const:`SEVENBITS`,
303 :const:`EIGHTBITS`
304 :type: int
305
cliechti6066f842009-07-24 00:05:45 +0000306 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000307
308 .. attribute:: parity
309
Chris Liechti518b0d32015-08-30 02:20:39 +0200310 :getter: Get current parity setting
311 :setter: Set new parity mode. Possible values:
312 :const:`PARITY_NONE`, :const:`PARITY_EVEN`, :const:`PARITY_ODD`
313 :const:`PARITY_MARK`, :const:`PARITY_SPACE`
314
cliechti6066f842009-07-24 00:05:45 +0000315 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000316
317 .. attribute:: stopbits
318
Chris Liechti518b0d32015-08-30 02:20:39 +0200319 :getter: Get current stop bit setting
320 :setter: Set new stop bit setting. Possible values:
321 :const:`STOPBITS_ONE`, :const:`STOPBITS_ONE_POINT_FIVE`,
322 :const:`STOPBITS_TWO`
323
cliechti6066f842009-07-24 00:05:45 +0000324 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000325
326 .. attribute:: timeout
327
Chris Liechti518b0d32015-08-30 02:20:39 +0200328 :getter: Get current read timeout setting
329 :setter: Set read timeout
330 :type: float (seconds)
331
cliechti6066f842009-07-24 00:05:45 +0000332 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000333
Chris Liechti518b0d32015-08-30 02:20:39 +0200334 .. attribute:: write_timeout
335
336 :getter: Get current write timeout setting
337 :setter: Set write timeout
338 :type: float (seconds)
cliechtiedcdbe42009-07-22 00:48:34 +0000339
cliechti6066f842009-07-24 00:05:45 +0000340 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000341
Chris Liechti518b0d32015-08-30 02:20:39 +0200342 .. versionchanged:: 3.0 renamed from ``writeTimeout``
343
344 .. attribute:: inter_byte_timeout
345
346 :getter: Get current inter byte timeout setting
347 :setter: Disable (``None``) or enable the inter byte timeout
348 :type: float or None
349
350 Read or write current inter byte timeout setting.
351
352 .. versionchanged:: 3.0 renamed from ``interCharTimeout``
353
cliechtiedcdbe42009-07-22 00:48:34 +0000354 .. attribute:: xonxoff
355
Chris Liechti518b0d32015-08-30 02:20:39 +0200356 :getter: Get current software flow control setting
357 :setter: Enable or disable software flow control
358 :type: bool
359
cliechti6066f842009-07-24 00:05:45 +0000360 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000361
362 .. attribute:: rtscts
363
Chris Liechti518b0d32015-08-30 02:20:39 +0200364 :getter: Get current hardware flow control setting
365 :setter: Enable or disable hardware flow control
366 :type: bool
367
cliechti6066f842009-07-24 00:05:45 +0000368 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000369
370 .. attribute:: dsrdtr
371
Chris Liechti518b0d32015-08-30 02:20:39 +0200372 :getter: Get current hardware flow control setting
373 :setter: Enable or disable hardware flow control
374 :type: bool
375
cliechti6066f842009-07-24 00:05:45 +0000376 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000377
Chris Liechtice621882015-08-06 19:29:31 +0200378 .. attribute:: rs485_mode
379
Chris Liechti518b0d32015-08-30 02:20:39 +0200380 :getter: Get the current RS485 settings
381 :setter: Disable (``None``) or enable the RS485 settings
382 :type: :class:`rs485.RS485Settings` or ``None``
383 :platform: Posix (Linux, limited set of hardware)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100384 :platform: Windows (only RTS on TX possible)
Chris Liechtice621882015-08-06 19:29:31 +0200385
386 Attribute to configure RS485 support. When set to an instance of
387 :class:`rs485.RS485Settings` and supported by OS, RTS will be active
388 when data is sent and inactive otherwise (for reception). The
389 :class:`rs485.RS485Settings` class provides additional settings
390 supported on some platforms.
391
392 .. versionadded:: 3.0
393
394
cliechtiedcdbe42009-07-22 00:48:34 +0000395 The following constants are also provided:
396
397 .. attribute:: BAUDRATES
398
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100399 A list of valid baud rates. The list may be incomplete, such that higher
400 and/or intermediate baud rates may also be supported by the device
401 (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000402
403 .. attribute:: BYTESIZES
404
cliechti25375b52010-07-21 23:27:13 +0000405 A list of valid byte sizes for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000406
407 .. attribute:: PARITIES
408
cliechti25375b52010-07-21 23:27:13 +0000409 A list of valid parities for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000410
411 .. attribute:: STOPBITS
412
cliechti25375b52010-07-21 23:27:13 +0000413 A list of valid stop bit widths for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000414
cliechti4a567a02009-07-27 22:09:31 +0000415
cliechti25375b52010-07-21 23:27:13 +0000416 The following methods are for compatibility with the :mod:`io` library.
cliechti4a567a02009-07-27 22:09:31 +0000417
418 .. method:: readable()
419
420 :return: True
cliechtif4566342009-08-04 00:07:19 +0000421
cliechti4a567a02009-07-27 22:09:31 +0000422 .. versionadded:: 2.5
423
424 .. method:: writable()
425
426 :return: True
cliechtif4566342009-08-04 00:07:19 +0000427
cliechti4a567a02009-07-27 22:09:31 +0000428 .. versionadded:: 2.5
429
430 .. method:: seekable()
431
432 :return: False
cliechtif4566342009-08-04 00:07:19 +0000433
cliechti4a567a02009-07-27 22:09:31 +0000434 .. versionadded:: 2.5
435
436 .. method:: readinto(b)
437
cliechti8611bf42009-08-03 00:34:03 +0000438 :param b: bytearray or array instance
cliechti4a567a02009-07-27 22:09:31 +0000439 :return: Number of byte read
440
cliechtid7e7ce22009-08-03 02:01:07 +0000441 Read up to len(b) bytes into :class:`bytearray` *b* and return the
442 number of bytes read.
cliechti4a567a02009-07-27 22:09:31 +0000443
444 .. versionadded:: 2.5
445
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100446 The port settings can be read and written as dictionary. The following
447 keys are supported: ``write_timeout``, ``inter_byte_timeout``,
448 ``dsrdtr``, ``baudrate``, ``timeout``, ``parity``, ``bytesize``,
449 ``rtscts``, ``stopbits``, ``xonxoff``
cliechti25375b52010-07-21 23:27:13 +0000450
Chris Liechti256ea212015-08-29 23:57:00 +0200451 .. method:: get_settings()
cliechti4065dce2009-08-10 00:55:46 +0000452
453 :return: a dictionary with current port settings.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100454 :rtype: dict
cliechti4065dce2009-08-10 00:55:46 +0000455
456 Get a dictionary with port settings. This is useful to backup the
457 current settings so that a later point in time they can be restored
Chris Liechti518b0d32015-08-30 02:20:39 +0200458 using :meth:`apply_settings`.
cliechti4065dce2009-08-10 00:55:46 +0000459
Chris Liechti19688bf2016-05-06 23:48:36 +0200460 Note that the state of control lines (RTS/DTR) are not part of the
461 settings.
cliechti4065dce2009-08-10 00:55:46 +0000462
463 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100464 .. versionchanged:: 3.0 renamed from ``getSettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000465
Chris Liechti256ea212015-08-29 23:57:00 +0200466 .. method:: apply_settings(d)
cliechti4065dce2009-08-10 00:55:46 +0000467
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100468 :param dict d: a dictionary with port settings.
cliechti4065dce2009-08-10 00:55:46 +0000469
Chris Liechti518b0d32015-08-30 02:20:39 +0200470 Applies a dictionary that was created by :meth:`get_settings`. Only
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100471 changes are applied and when a key is missing, it means that the
472 setting stays unchanged.
cliechti4065dce2009-08-10 00:55:46 +0000473
474 Note that control lines (RTS/DTR) are not changed.
475
476 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100477 .. versionchanged:: 3.0 renamed from ``applySettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000478
cliechti25375b52010-07-21 23:27:13 +0000479 Platform specific methods.
480
cliechtic648da92011-12-29 01:17:51 +0000481 .. warning:: Programs using the following methods and attributes are not
482 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000483
484 .. method:: nonblocking()
485
Chris Liechtice621882015-08-06 19:29:31 +0200486 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000487
Chris Liechti59848422016-06-04 22:28:14 +0200488 .. deprecated:: 3.2
489 The serial port is already opened in this mode. This method is not
490 needed and going away.
491
cliechti25375b52010-07-21 23:27:13 +0000492
493 .. method:: fileno()
494
Chris Liechtice621882015-08-06 19:29:31 +0200495 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000496 :return: File descriptor.
497
498 Return file descriptor number for the port that is opened by this object.
499 It is useful when serial ports are used with :mod:`select`.
500
Chris Liechti518b0d32015-08-30 02:20:39 +0200501 .. method:: set_input_flow_control(enable)
cliechti25375b52010-07-21 23:27:13 +0000502
cliechti2f0f8a32011-12-28 22:10:00 +0000503 :platform: Posix
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100504 :param bool enable: Set flow control state.
cliechti25375b52010-07-21 23:27:13 +0000505
cliechti2f0f8a32011-12-28 22:10:00 +0000506 Manually control flow - when software flow control is enabled.
507
508 This will send XON (true) and XOFF (false) to the other device.
509
Chris Liechti518b0d32015-08-30 02:20:39 +0200510 .. versionadded:: 2.7 (Posix support added)
511 .. versionchanged:: 3.0 renamed from ``flowControlOut``
cliechti2f0f8a32011-12-28 22:10:00 +0000512
Chris Liechti518b0d32015-08-30 02:20:39 +0200513 .. method:: set_output_flow_control(enable)
cliechti2f0f8a32011-12-28 22:10:00 +0000514
Chris Liechti518b0d32015-08-30 02:20:39 +0200515 :platform: Posix (HW and SW flow control)
516 :platform: Windows (SW flow control only)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100517 :param bool enable: Set flow control state.
cliechti2f0f8a32011-12-28 22:10:00 +0000518
519 Manually control flow of outgoing data - when hardware or software flow
520 control is enabled.
521
522 Sending will be suspended when called with ``False`` and enabled when
523 called with ``True``.
524
Chris Liechti518b0d32015-08-30 02:20:39 +0200525 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
526 .. versionchanged:: 3.0 renamed from ``setXON``
Chris Liechti256ea212015-08-29 23:57:00 +0200527
Chris Liechti19688bf2016-05-06 23:48:36 +0200528 .. method:: cancel_read()
Chris Liechti256ea212015-08-29 23:57:00 +0200529
Chris Liechti9d893052016-05-18 22:03:29 +0200530 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200531 :platform: Windows
532
Chris Liechti9d893052016-05-18 22:03:29 +0200533 Cancel a pending read operation from an other thread. A blocking
534 :meth:`read` call is aborted immediately. :meth:`read` will not report
535 any error but return all data received up to that point (similar to a
536 timeout).
537
538 On Posix a call to `cancel_read()` may cancel a future :meth:`read` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200539
540 .. versionadded:: 3.1
541
542 .. method:: cancel_write()
543
Chris Liechti9d893052016-05-18 22:03:29 +0200544 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200545 :platform: Windows
546
Chris Liechti9d893052016-05-18 22:03:29 +0200547 Cancel a pending write operation from an other thread. The
548 :meth:`write` method will return immediately (no error indicated).
549 However the OS may still be sending from the buffer, a separate call to
550 :meth:`reset_output_buffer` may be needed.
551
552 On Posix a call to `cancel_write()` may cancel a future :meth:`write` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200553
554 .. versionadded:: 3.1
Chris Liechti518b0d32015-08-30 02:20:39 +0200555
Chris Liechti9a99cb22015-08-30 22:16:50 +0200556 .. note:: The following members are deprecated and will be removed in a
Chris Liechti518b0d32015-08-30 02:20:39 +0200557 future release.
Chris Liechti256ea212015-08-29 23:57:00 +0200558
559 .. attribute:: portstr
560
Chris Liechti518b0d32015-08-30 02:20:39 +0200561 .. deprecated:: 2.5 use :attr:`name` instead
Chris Liechti256ea212015-08-29 23:57:00 +0200562
563 .. method:: inWaiting()
564
Chris Liechti518b0d32015-08-30 02:20:39 +0200565 .. deprecated:: 3.0 see :attr:`in_waiting`
566
567 .. attribute:: writeTimeout
568
569 .. deprecated:: 3.0 see :attr:`write_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200570
571 .. attribute:: interCharTimeout
572
Chris Liechti518b0d32015-08-30 02:20:39 +0200573 .. deprecated:: 3.0 see :attr:`inter_byte_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200574
575 .. method:: sendBreak(duration=0.25)
576
Chris Liechti518b0d32015-08-30 02:20:39 +0200577 .. deprecated:: 3.0 see :meth:`send_break`
Chris Liechti256ea212015-08-29 23:57:00 +0200578
579 .. method:: flushInput()
580
Chris Liechti518b0d32015-08-30 02:20:39 +0200581 .. deprecated:: 3.0 see :meth:`reset_input_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200582
583 .. method:: flushOutput()
584
Chris Liechti518b0d32015-08-30 02:20:39 +0200585 .. deprecated:: 3.0 see :meth:`reset_output_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200586
587 .. method:: setBreak(level=True)
588
Chris Liechti518b0d32015-08-30 02:20:39 +0200589 .. deprecated:: 3.0 see :attr:`break_condition`
Chris Liechti256ea212015-08-29 23:57:00 +0200590
591 .. method:: setRTS(level=True)
592
Chris Liechti518b0d32015-08-30 02:20:39 +0200593 .. deprecated:: 3.0 see :attr:`rts`
Chris Liechti256ea212015-08-29 23:57:00 +0200594
595 .. method:: setDTR(level=True)
596
Chris Liechti518b0d32015-08-30 02:20:39 +0200597 .. deprecated:: 3.0 see :attr:`dtr`
Chris Liechti256ea212015-08-29 23:57:00 +0200598
599 .. method:: getCTS()
600
Chris Liechti518b0d32015-08-30 02:20:39 +0200601 .. deprecated:: 3.0 see :attr:`cts`
Chris Liechti256ea212015-08-29 23:57:00 +0200602
603 .. method:: getDSR()
604
Chris Liechti518b0d32015-08-30 02:20:39 +0200605 .. deprecated:: 3.0 see :attr:`dsr`
Chris Liechti256ea212015-08-29 23:57:00 +0200606
607 .. method:: getRI()
608
Chris Liechti518b0d32015-08-30 02:20:39 +0200609 .. deprecated:: 3.0 see :attr:`ri`
Chris Liechti256ea212015-08-29 23:57:00 +0200610
611 .. method:: getCD()
612
Chris Liechti518b0d32015-08-30 02:20:39 +0200613 .. deprecated:: 3.0 see :attr:`cd`
Chris Liechti256ea212015-08-29 23:57:00 +0200614
615 .. method:: getSettingsDict()
616
Chris Liechti518b0d32015-08-30 02:20:39 +0200617 .. deprecated:: 3.0 see :meth:`get_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200618
619 .. method:: applySettingsDict(d)
620
Chris Liechti518b0d32015-08-30 02:20:39 +0200621 .. deprecated:: 3.0 see :meth:`apply_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200622
623 .. method:: outWaiting()
624
Chris Liechti518b0d32015-08-30 02:20:39 +0200625 .. deprecated:: 3.0 see :attr:`out_waiting`
Chris Liechti256ea212015-08-29 23:57:00 +0200626
627 .. method:: setXON(level=True)
628
Chris Liechti518b0d32015-08-30 02:20:39 +0200629 .. deprecated:: 3.0 see :meth:`set_output_flow_control`
Chris Liechti256ea212015-08-29 23:57:00 +0200630
631 .. method:: flowControlOut(enable)
632
Chris Liechti518b0d32015-08-30 02:20:39 +0200633 .. deprecated:: 3.0 see :meth:`set_input_flow_control`
cliechtif4566342009-08-04 00:07:19 +0000634
cliechtic648da92011-12-29 01:17:51 +0000635 .. attribute:: rtsToggle
636
637 :platform: Windows
638
639 Attribute to configure RTS toggle control setting. When enabled and
640 supported by OS, RTS will be active when data is available and inactive
641 if no data is available.
642
643 .. versionadded:: 2.6
Chris Liechti518b0d32015-08-30 02:20:39 +0200644 .. versionchanged:: 3.0 (removed, see :attr:`rs485_mode` instead)
cliechtic648da92011-12-29 01:17:51 +0000645
646
cliechtic56e41d2011-08-25 22:06:38 +0000647Implementation detail: some attributes and functions are provided by the
648class :class:`SerialBase` and some by the platform specific class and
649others by the base class mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000650
Chris Liechtice621882015-08-06 19:29:31 +0200651RS485 support
652-------------
653The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to
654enable RS485 specific support on some platforms. Currently Windows and Linux
655(only a small number of devices) are supported.
cliechti4a567a02009-07-27 22:09:31 +0000656
Chris Liechtice621882015-08-06 19:29:31 +0200657:attr:`Serial.rs485_mode` needs to be set to an instance of
658:class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature.
cliechti4a567a02009-07-27 22:09:31 +0000659
Chris Liechtice621882015-08-06 19:29:31 +0200660Usage::
cliechti4a567a02009-07-27 22:09:31 +0000661
Chris Liechtice621882015-08-06 19:29:31 +0200662 ser = serial.Serial(...)
663 ser.rs485_mode = serial.rs485.RS485Settings(...)
664 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000665
Chris Liechtice621882015-08-06 19:29:31 +0200666There is a subclass :class:`rs485.RS485` available to emulate the RS485 support
667on regular serial ports.
cliechti4a567a02009-07-27 22:09:31 +0000668
cliechti4a567a02009-07-27 22:09:31 +0000669
Chris Liechtice621882015-08-06 19:29:31 +0200670.. class:: rs485.RS485Settings
cliechti4a567a02009-07-27 22:09:31 +0000671
Chris Liechtice621882015-08-06 19:29:31 +0200672 A class that holds RS485 specific settings which are supported on
673 some platforms.
cliechti4a567a02009-07-27 22:09:31 +0000674
Chris Liechtice621882015-08-06 19:29:31 +0200675 .. versionadded:: 3.0
cliechti4a567a02009-07-27 22:09:31 +0000676
Chris Liechtice621882015-08-06 19:29:31 +0200677 .. 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 +0000678
Chris Liechtice621882015-08-06 19:29:31 +0200679 :param bool rts_level_for_tx:
680 RTS level for transmission
cliechti4a567a02009-07-27 22:09:31 +0000681
Chris Liechtice621882015-08-06 19:29:31 +0200682 :param bool rts_level_for_rx:
683 RTS level for reception
cliechti4a567a02009-07-27 22:09:31 +0000684
Chris Liechtice621882015-08-06 19:29:31 +0200685 :param bool loopback:
686 When set to ``True`` transmitted data is also received.
cliechti4a567a02009-07-27 22:09:31 +0000687
Chris Liechtice621882015-08-06 19:29:31 +0200688 :param float delay_before_tx:
689 Delay after setting RTS but before transmission starts
690
691 :param float delay_before_rx:
692 Delay after transmission ends and resetting RTS
693
694 .. attribute:: rts_level_for_tx
695
696 RTS level for transmission.
697
698 .. attribute:: rts_level_for_rx
699
700 RTS level for reception.
701
702 .. attribute:: loopback
703
704 When set to ``True`` transmitted data is also received.
705
706 .. attribute:: delay_before_tx
707
708 Delay after setting RTS but before transmission starts (seconds as float).
709
710 .. attribute:: delay_before_rx
711
712 Delay after transmission ends and resetting RTS (seconds as float).
cliechti4a567a02009-07-27 22:09:31 +0000713
714
Chris Liechtice621882015-08-06 19:29:31 +0200715.. class:: rs485.RS485
cliechti4a567a02009-07-27 22:09:31 +0000716
Chris Liechtice621882015-08-06 19:29:31 +0200717 A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS
718 according to the RS485 settings.
cliechti4a567a02009-07-27 22:09:31 +0000719
Chris Liechti518b0d32015-08-30 02:20:39 +0200720 Usage::
721
722 ser = serial.rs485.RS485(...)
723 ser.rs485_mode = serial.rs485.RS485Settings(...)
724 ser.write(b'hello')
725
Chris Liechtice621882015-08-06 19:29:31 +0200726 .. warning:: This may work unreliably on some serial ports (control signals not
727 synchronized or delayed compared to data). Using delays may be unreliable
728 (varying times, larger than expected) as the OS may not support very fine
729 grained delays (no smaller than in the order of tens of milliseconds).
cliechti4a567a02009-07-27 22:09:31 +0000730
Chris Liechtice621882015-08-06 19:29:31 +0200731 .. note:: Some implementations support this natively in the class
732 :class:`Serial`. Better performance can be expected when the native version
733 is used.
cliechti4a567a02009-07-27 22:09:31 +0000734
Chris Liechtice621882015-08-06 19:29:31 +0200735 .. note:: The loopback property is ignored by this implementation. The actual
736 behavior depends on the used hardware.
cliechti4a567a02009-07-27 22:09:31 +0000737
cliechti4a567a02009-07-27 22:09:31 +0000738
cliechtie214ff82010-07-21 15:48:47 +0000739
cliechti25375b52010-07-21 23:27:13 +0000740
cliechti7aed8332009-08-05 14:19:31 +0000741:rfc:`2217` Network ports
742-------------------------
743
744.. warning:: This implementation is currently in an experimental state. Use
745 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000746
cliechtiec4ac1b2009-08-02 00:47:21 +0000747.. class:: rfc2217.Serial
748
Chris Liechti2a1befc2015-10-21 17:37:08 +0200749 This implements a :rfc:`2217` compatible client. Port names are :ref:`URL
750 <URLs>` in the form: ``rfc2217://<host>:<port>[?<option>[&<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000751
cliechtiec4ac1b2009-08-02 00:47:21 +0000752 This class API is compatible to :class:`Serial` with a few exceptions:
753
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100754 - ``write_timeout`` is not implemented
cliechtiec4ac1b2009-08-02 00:47:21 +0000755 - The current implementation starts a thread that keeps reading from the
756 (internal) socket. The thread is managed automatically by the
757 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
758 However it may be a problem for user applications that like to use select
759 instead of threads.
760
761 Due to the nature of the network and protocol involved there are a few
762 extra points to keep in mind:
763
764 - All operations have an additional latency time.
765 - Setting control lines (RTS/CTS) needs more time.
766 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
767 cache is updated depends entirely on the server. The server itself may
768 implement a polling at a certain rate and quick changes may be invisible.
769 - The network layer also has buffers. This means that :meth:`flush`,
Chris Liechti518b0d32015-08-30 02:20:39 +0200770 :meth:`reset_input_buffer` and :meth:`reset_output_buffer` may work with
771 additional delay. Likewise :attr:`in_waiting` returns the size of the
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100772 data arrived at the objects internal buffer and excludes any bytes in the
Chris Liechti518b0d32015-08-30 02:20:39 +0200773 network buffers or any server side buffer.
cliechtiec4ac1b2009-08-02 00:47:21 +0000774 - Closing and immediately reopening the same port may fail due to time
775 needed by the server to get ready again.
776
777 Not implemented yet / Possible problems with the implementation:
778
cliechti8611bf42009-08-03 00:34:03 +0000779 - :rfc:`2217` flow control between client and server (objects internal
780 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000781 - No authentication support (servers may not prompt for a password etc.)
782 - No encryption.
783
784 Due to lack of authentication and encryption it is not suitable to use this
785 client for connections across the internet and should only be used in
786 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000787
cliechti7c640ed2009-08-02 00:54:51 +0000788 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000789
cliechti7aed8332009-08-05 14:19:31 +0000790
791.. class:: rfc2217.PortManager
792
793 This class provides helper functions for implementing :rfc:`2217`
794 compatible servers.
795
Chris Liechti518b0d32015-08-30 02:20:39 +0200796 Basically, it implements everything needed for the :rfc:`2217` protocol.
cliechti7aed8332009-08-05 14:19:31 +0000797 It just does not open sockets and read/write to serial ports (though it
798 changes other port settings). The user of this class must take care of the
799 data transmission itself. The reason for that is, that this way, this class
800 supports all programming models such as threads and select.
801
802 Usage examples can be found in the examples where two TCP/IP - serial
803 converters are shown, one using threads (the single port server) and an
804 other using select (the multi port server).
805
806 .. note:: Each new client connection must create a new instance as this
807 object (and the :rfc:`2217` protocol) has internal state.
808
809 .. method:: __init__(serial_port, connection, debug_output=False)
810
811 :param serial_port: a :class:`Serial` instance that is managed.
812 :param connection: an object implementing :meth:`write`, used to write
813 to the network.
cliechti86844e82009-08-12 00:05:33 +0000814 :param debug_output: enables debug messages: a :class:`logging.Logger`
815 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000816
817 Initializes the Manager and starts negotiating with client in Telnet
818 and :rfc:`2217` protocol. The negotiation starts immediately so that
819 the class should be instantiated in the moment the client connects.
820
821 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000822 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000823 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
824 the :meth:`filter` method.
825
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100826 The *connection* object must implement a :meth:`write` function.
cliechti7aed8332009-08-05 14:19:31 +0000827 This function must ensure that *data* is written at once (no user data
828 mixed in, i.e. it must be thread-safe). All data must be sent in its
829 raw form (:meth:`escape` must not be used) as it is used to send Telnet
830 and :rfc:`2217` control commands.
831
cliechti86844e82009-08-12 00:05:33 +0000832 For diagnostics of the connection or the implementation, *debug_output*
833 can be set to an instance of a :class:`logging.Logger` (e.g.
834 ``logging.getLogger('rfc2217.server')``). The caller should configure
835 the logger using ``setLevel`` for the desired detail level of the logs.
836
cliechti7aed8332009-08-05 14:19:31 +0000837 .. method:: escape(data)
838
839 :param data: data to be sent over the network.
840 :return: data, escaped for Telnet/:rfc:`2217`
841
842 A generator that escapes all data to be compatible with :rfc:`2217`.
843 Implementors of servers should use this function to process all data
844 sent over the network.
845
846 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000847 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000848
849 .. method:: filter(data)
850
851 :param data: data read from the network, including Telnet and
852 :rfc:`2217` controls.
853 :return: data, free from Telnet and :rfc:`2217` controls.
854
855 A generator that filters and processes all data related to :rfc:`2217`.
856 Implementors of servers should use this function to process all data
857 received from the network.
858
859 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000860 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000861
cliechti7cb78e82009-08-05 15:47:57 +0000862 .. method:: check_modem_lines(force_notification=False)
863
864 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000865
866 This function needs to be called periodically (e.g. every second) when
867 the server wants to send NOTIFY_MODEMSTATE messages. This is required
868 to support the client for reading CTS/DSR/RI/CD status lines.
869
870 The function reads the status line and issues the notifications
871 automatically.
872
873 .. versionadded:: 2.5
874
cliechtiec4ac1b2009-08-02 00:47:21 +0000875.. seealso::
876
877 :rfc:`2217` - Telnet Com Port Control Option
878
879
cliechtic1c37602009-07-21 01:34:57 +0000880Exceptions
881==========
882
883.. exception:: SerialException
884
885 Base class for serial port exceptions.
886
cliechti4a567a02009-07-27 22:09:31 +0000887 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000888 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000889
cliechtic1c37602009-07-21 01:34:57 +0000890.. exception:: SerialTimeoutException
891
892 Exception that is raised on write timeouts.
893
894
895Constants
896=========
897
cliechti87686242009-08-18 00:58:31 +0000898*Parity*
899
cliechtic1c37602009-07-21 01:34:57 +0000900.. data:: PARITY_NONE
901.. data:: PARITY_EVEN
902.. data:: PARITY_ODD
903.. data:: PARITY_MARK
904.. data:: PARITY_SPACE
905
cliechti87686242009-08-18 00:58:31 +0000906*Stop bits*
907
cliechtic1c37602009-07-21 01:34:57 +0000908.. data:: STOPBITS_ONE
909.. data:: STOPBITS_ONE_POINT_FIVE
910.. data:: STOPBITS_TWO
911
cliechti06281be2011-08-25 23:08:29 +0000912Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +0000913bits.
914
cliechti87686242009-08-18 00:58:31 +0000915*Byte size*
916
cliechtic1c37602009-07-21 01:34:57 +0000917.. data:: FIVEBITS
918.. data:: SIXBITS
919.. data:: SEVENBITS
920.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000921
cliechti87686242009-08-18 00:58:31 +0000922
923*Others*
924
cliechti8611bf42009-08-03 00:34:03 +0000925Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000926software flow control:
cliechti6066f842009-07-24 00:05:45 +0000927
cliechti5b7d16a2009-07-21 21:53:59 +0000928.. data:: XON
929.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000930
cliechti4a567a02009-07-27 22:09:31 +0000931Module version:
cliechtif81362e2009-07-25 03:44:33 +0000932
933.. data:: VERSION
934
Chris Liechti518b0d32015-08-30 02:20:39 +0200935 A string indicating the pySerial version, such as ``3.0``.
cliechtif81362e2009-07-25 03:44:33 +0000936
cliechti87686242009-08-18 00:58:31 +0000937 .. versionadded:: 2.3
938
cliechti44eb98f2011-03-21 21:41:10 +0000939
cliechtie542b362011-03-18 00:49:16 +0000940Module functions and attributes
941===============================
cliechtif81362e2009-07-25 03:44:33 +0000942
943.. function:: device(number)
944
Chris Liechti518b0d32015-08-30 02:20:39 +0200945 .. versionchanged:: 3.0 removed, use ``serial.tools.list_ports`` instead
946
cliechti7c640ed2009-08-02 00:54:51 +0000947
cliechtie3ab3532009-08-05 12:40:38 +0000948.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +0000949
cliechti86844e82009-08-12 00:05:33 +0000950 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +0000951 :param do_not_open: When set to true, the serial port is not opened.
952 :return: an instance of :class:`Serial` or a compatible object.
953
954 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +0000955 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +0000956 to support both, local ports and remote ports. There is also support
Chris Liechti589c92a2015-09-04 23:04:34 +0200957 for other types, see :ref:`URL <URLs>` section.
cliechti7c640ed2009-08-02 00:54:51 +0000958
cliechtid7e7ce22009-08-03 02:01:07 +0000959 The port is not opened when a keyword parameter called *do_not_open* is
960 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +0000961
962 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +0000963
cliechti87686242009-08-18 00:58:31 +0000964
cliechtie542b362011-03-18 00:49:16 +0000965.. attribute:: protocol_handler_packages
966
967 This attribute is a list of package names (strings) that is searched for
968 protocol handlers.
969
970 e.g. we want to support a URL ``foobar://``. A module
971 ``my_handlers.protocol_foobar`` is provided by the user::
972
973 serial.protocol_handler_packages.append("my_handlers")
974 s = serial.serial_for_url("foobar://")
975
976 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
977 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
978 ``PACKAGE`` from this list.
979
980 .. versionadded:: 2.6
981
982
cliechti25375b52010-07-21 23:27:13 +0000983.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +0000984
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100985 :param sequence: bytes, bytearray or memoryview
cliechti25375b52010-07-21 23:27:13 +0000986 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +0000987
cliechtie542b362011-03-18 00:49:16 +0000988 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +0000989 compatible to Python 2.x and 3.x.
990
cliechtie542b362011-03-18 00:49:16 +0000991 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +0000992 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +0000993 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +0000994
995 This function is used internally and in the unit tests.
996
cliechtie542b362011-03-18 00:49:16 +0000997 .. versionadded:: 2.5
998
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100999.. function:: iterbytes(sequence)
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001000
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001001 :param sequence: bytes, bytearray or memoryview
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001002 :returns: a generator that yields bytes
1003
1004 Some versions of Python (3.x) would return integers instead of bytes when
1005 looping over an instance of ``bytes``. This helper function ensures that
1006 bytes are returned.
1007
1008 .. versionadded:: 3.0
1009
cliechtie214ff82010-07-21 15:48:47 +00001010
Chris Liechti811bf382015-10-10 00:54:47 +02001011Threading
1012=========
1013
1014.. module:: serial.threaded
1015.. versionadded:: 3.0
1016
1017.. warning:: This implementation is currently in an experimental state. Use
1018 at your own risk.
1019
1020This module provides classes to simplify working with threads and protocols.
1021
1022.. class:: Protocol
1023
Chris Liechti5665d572015-10-13 23:50:01 +02001024 Protocol as used by the :class:`ReaderThread`. This base class provides empty
Chris Liechti811bf382015-10-10 00:54:47 +02001025 implementations of all methods.
1026
1027
1028 .. method:: connection_made(transport)
1029
1030 :param transport: instance used to write to serial port.
1031
1032 Called when reader thread is started.
1033
1034 .. method:: data_received(data)
1035
Chris Liechtif8a09192015-12-20 23:36:38 +01001036 :param bytes data: received bytes
Chris Liechti811bf382015-10-10 00:54:47 +02001037
1038 Called with snippets received from the serial port.
1039
1040 .. method:: connection_lost(exc)
1041
1042 :param exc: Exception if connection was terminated by error else ``None``
1043
1044 Called when the serial port is closed or the reader loop terminated
1045 otherwise.
1046
1047.. class:: Packetizer(Protocol)
1048
1049 Read binary packets from serial port. Packets are expected to be terminated
1050 with a ``TERMINATOR`` byte (null byte by default).
1051
1052 The class also keeps track of the transport.
1053
1054 .. attribute:: TERMINATOR = b'\\0'
1055
1056 .. method:: __init__()
1057
1058 .. method:: connection_made(transport)
1059
Chris Liechtif8a09192015-12-20 23:36:38 +01001060 Stores transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001061
1062 .. method:: connection_lost(exc)
1063
Chris Liechtif8a09192015-12-20 23:36:38 +01001064 Forgets transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001065
1066 .. method:: data_received(data)
1067
Chris Liechtif8a09192015-12-20 23:36:38 +01001068 :param bytes data: partial received data
1069
1070 Buffer received data and search for :attr:`TERMINATOR`, when found,
Chris Liechti811bf382015-10-10 00:54:47 +02001071 call :meth:`handle_packet`.
1072
1073 .. method:: handle_packet(packet)
1074
Chris Liechti1c4c5992016-01-18 20:46:00 +01001075 :param bytes packet: a packet as defined by ``TERMINATOR``
1076
Chris Liechti811bf382015-10-10 00:54:47 +02001077 Process packets - to be overridden by subclassing.
1078
1079
1080.. class:: LineReader(Packetizer)
1081
1082 Read and write (Unicode) lines from/to serial port.
1083 The encoding is applied.
1084
1085
1086 .. attribute:: TERMINATOR = b'\\r\\n'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001087
1088 Line ending.
1089
Chris Liechti811bf382015-10-10 00:54:47 +02001090 .. attribute:: ENCODING = 'utf-8'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001091
1092 Encoding of the send and received data.
1093
Chris Liechti811bf382015-10-10 00:54:47 +02001094 .. attribute:: UNICODE_HANDLING = 'replace'
1095
Chris Liechti1c4c5992016-01-18 20:46:00 +01001096 Unicode error handly policy.
1097
Chris Liechti811bf382015-10-10 00:54:47 +02001098 .. method:: handle_packet(packet)
1099
Chris Liechti1c4c5992016-01-18 20:46:00 +01001100 :param bytes packet: a packet as defined by ``TERMINATOR``
1101
1102 In this case it will be a line, calls :meth:`handle_line` after applying
1103 the :attr:`ENCODING`.
1104
Chris Liechti811bf382015-10-10 00:54:47 +02001105 .. method:: handle_line(line)
1106
Chris Liechtif8a09192015-12-20 23:36:38 +01001107 :param str line: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001108
1109 Process one line - to be overridden by subclassing.
1110
1111 .. method:: write_line(text)
1112
Chris Liechtif8a09192015-12-20 23:36:38 +01001113 :param str text: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001114
Chris Liechtif8a09192015-12-20 23:36:38 +01001115 Write *text* to the transport. *text* is expected to be a Unicode
1116 string and the encoding is applied before sending and also the
1117 :attr:`TERMINATOR` (new line) is appended.
Chris Liechti811bf382015-10-10 00:54:47 +02001118
1119
Chris Liechti5665d572015-10-13 23:50:01 +02001120.. class:: ReaderThread(threading.Thread)
Chris Liechti811bf382015-10-10 00:54:47 +02001121
1122 Implement a serial port read loop and dispatch to a Protocol instance (like
1123 the :class:`asyncio.Protocol`) but do it with threads.
1124
Chris Liechtif8a09192015-12-20 23:36:38 +01001125 Calls to :meth:`close` will close the serial port but it is also possible
1126 to just :meth:`stop` this thread and continue to use the serial port
1127 instance otherwise.
Chris Liechti811bf382015-10-10 00:54:47 +02001128
1129 .. method:: __init__(serial_instance, protocol_factory)
1130
1131 :param serial_instance: serial port instance (opened) to be used.
1132 :param protocol_factory: a callable that returns a Protocol instance
1133
1134 Initialize thread.
1135
Chris Liechtif8a09192015-12-20 23:36:38 +01001136 Note that the ``serial_instance`` 's timeout is set to one second!
Chris Liechti811bf382015-10-10 00:54:47 +02001137 Other settings are not changed.
1138
1139 .. method:: stop()
1140
1141 Stop the reader thread.
1142
1143 .. method:: run()
1144
1145 The actual reader loop driven by the thread. It calls
1146 :meth:`Protocol.connection_made`, reads from the serial port calling
Chris Liechtif8a09192015-12-20 23:36:38 +01001147 :meth:`Protocol.data_received` and finally calls :meth:`Protocol.connection_lost`
Chris Liechti811bf382015-10-10 00:54:47 +02001148 when :meth:`close` is called or an error occurs.
1149
1150 .. method:: write(data)
1151
Chris Liechtif8a09192015-12-20 23:36:38 +01001152 :param bytes data: data to write
1153
Chris Liechti811bf382015-10-10 00:54:47 +02001154 Thread safe writing (uses lock).
1155
1156 .. method:: close()
1157
1158 Close the serial port and exit reader thread, calls :meth:`stop` (uses lock).
1159
1160 .. method:: connect()
1161
1162 Wait until connection is set up and return the transport and protocol
1163 instances.
1164
1165
1166 This class can be used as context manager, in this case it starts
1167 the thread and connects automatically. The serial port is closed
1168 when the context is left.
1169
1170 .. method:: __enter__()
1171
1172 :returns: protocol
1173
1174 Connect and return protocol instance.
1175
1176 .. method:: __exit__(exc_type, exc_val, exc_tb)
1177
1178 Closes serial port.
1179
Chris Liechtia1436b12015-10-12 23:19:04 +02001180Example::
1181
1182 class PrintLines(LineReader):
1183 def connection_made(self, transport):
1184 super(PrintLines, self).connection_made(transport)
1185 sys.stdout.write('port opened\n')
1186 self.write_line('hello world')
1187
1188 def handle_line(self, data):
1189 sys.stdout.write('line received: {}\n'.format(repr(data)))
1190
1191 def connection_lost(self, exc):
1192 if exc:
1193 traceback.print_exc(exc)
1194 sys.stdout.write('port closed\n')
1195
1196 ser = serial.serial_for_url('loop://', baudrate=115200, timeout=1)
Chris Liechti5665d572015-10-13 23:50:01 +02001197 with ReaderThread(ser, PrintLines) as protocol:
Chris Liechtia1436b12015-10-12 23:19:04 +02001198 protocol.write_line('hello')
1199 time.sleep(2)
1200
Chris Liechti811bf382015-10-10 00:54:47 +02001201
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001202asyncio
1203=======
1204
Chris Liechti589c92a2015-09-04 23:04:34 +02001205.. module:: serial.aio
1206
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001207.. warning:: This implementation is currently in an experimental state. Use
1208 at your own risk.
1209
Chris Liechti589c92a2015-09-04 23:04:34 +02001210Experimental asyncio support is available for Python 3.4 and newer. The module
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001211:mod:`serial.aio` provides a :class:`asyncio.Transport`:
1212``SerialTransport``.
1213
1214
1215A factory function (`asyncio.coroutine`) is provided:
1216
1217.. function:: create_serial_connection(loop, protocol_factory, \*args, \*\*kwargs)
1218
1219 :param loop: The event handler
1220 :param protocol_factory: Factory function for a :class:`asyncio.Protocol`
1221 :param args: Passed to the :class:`serial.Serial` init function
1222 :param kwargs: Passed to the :class:`serial.Serial` init function
1223 :platform: Posix
1224
1225 Get a connection making coroutine.
1226
1227Example::
1228
1229 class Output(asyncio.Protocol):
1230 def connection_made(self, transport):
1231 self.transport = transport
1232 print('port opened', transport)
Chris Liechti589c92a2015-09-04 23:04:34 +02001233 transport.serial.rts = False
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001234 transport.write(b'hello world\n')
1235
1236 def data_received(self, data):
1237 print('data received', repr(data))
1238 self.transport.close()
1239
1240 def connection_lost(self, exc):
1241 print('port closed')
1242 asyncio.get_event_loop().stop()
1243
1244 loop = asyncio.get_event_loop()
Chris Liechti589c92a2015-09-04 23:04:34 +02001245 coro = serial.aio.create_serial_connection(loop, Output, '/dev/ttyUSB0', baudrate=115200)
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001246 loop.run_until_complete(coro)
1247 loop.run_forever()
1248 loop.close()
1249