blob: a3417c2a7bb4ae8594b1c937d128469bf903fb3e [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
Matthew Westfa58a972016-07-08 14:27:04 -0700278 .. attribute:: is_open
279 :getter: Get the state of the serial port, whether it's open.
280 :type: bool
cliechtif81362e2009-07-25 03:44:33 +0000281
cliechti25375b52010-07-21 23:27:13 +0000282 New values can be assigned to the following attributes (properties), the
283 port will be reconfigured, even if it's opened at that time:
cliechtic1c37602009-07-21 01:34:57 +0000284
cliechtiedcdbe42009-07-22 00:48:34 +0000285
286 .. attribute:: port
287
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100288 :type: str
289
cliechtiedcdbe42009-07-22 00:48:34 +0000290 Read or write port. When the port is already open, it will be closed
291 and reopened with the new setting.
292
293 .. attribute:: baudrate
294
Chris Liechti518b0d32015-08-30 02:20:39 +0200295 :getter: Get current baud rate
296 :setter: Set new baud rate
297 :type: int
298
cliechti6066f842009-07-24 00:05:45 +0000299 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000300
301 .. attribute:: bytesize
302
Chris Liechti518b0d32015-08-30 02:20:39 +0200303 :getter: Get current byte size
304 :setter: Set new byte size. Possible values:
305 :const:`FIVEBITS`, :const:`SIXBITS`, :const:`SEVENBITS`,
306 :const:`EIGHTBITS`
307 :type: int
308
cliechti6066f842009-07-24 00:05:45 +0000309 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000310
311 .. attribute:: parity
312
Chris Liechti518b0d32015-08-30 02:20:39 +0200313 :getter: Get current parity setting
314 :setter: Set new parity mode. Possible values:
315 :const:`PARITY_NONE`, :const:`PARITY_EVEN`, :const:`PARITY_ODD`
316 :const:`PARITY_MARK`, :const:`PARITY_SPACE`
317
cliechti6066f842009-07-24 00:05:45 +0000318 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000319
320 .. attribute:: stopbits
321
Chris Liechti518b0d32015-08-30 02:20:39 +0200322 :getter: Get current stop bit setting
323 :setter: Set new stop bit setting. Possible values:
324 :const:`STOPBITS_ONE`, :const:`STOPBITS_ONE_POINT_FIVE`,
325 :const:`STOPBITS_TWO`
326
cliechti6066f842009-07-24 00:05:45 +0000327 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000328
329 .. attribute:: timeout
330
Chris Liechti518b0d32015-08-30 02:20:39 +0200331 :getter: Get current read timeout setting
332 :setter: Set read timeout
333 :type: float (seconds)
334
cliechti6066f842009-07-24 00:05:45 +0000335 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000336
Chris Liechti518b0d32015-08-30 02:20:39 +0200337 .. attribute:: write_timeout
338
339 :getter: Get current write timeout setting
340 :setter: Set write timeout
341 :type: float (seconds)
cliechtiedcdbe42009-07-22 00:48:34 +0000342
cliechti6066f842009-07-24 00:05:45 +0000343 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000344
Chris Liechti518b0d32015-08-30 02:20:39 +0200345 .. versionchanged:: 3.0 renamed from ``writeTimeout``
346
347 .. attribute:: inter_byte_timeout
348
349 :getter: Get current inter byte timeout setting
350 :setter: Disable (``None``) or enable the inter byte timeout
351 :type: float or None
352
353 Read or write current inter byte timeout setting.
354
355 .. versionchanged:: 3.0 renamed from ``interCharTimeout``
356
cliechtiedcdbe42009-07-22 00:48:34 +0000357 .. attribute:: xonxoff
358
Chris Liechti518b0d32015-08-30 02:20:39 +0200359 :getter: Get current software flow control setting
360 :setter: Enable or disable software flow control
361 :type: bool
362
cliechti6066f842009-07-24 00:05:45 +0000363 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000364
365 .. attribute:: rtscts
366
Chris Liechti518b0d32015-08-30 02:20:39 +0200367 :getter: Get current hardware flow control setting
368 :setter: Enable or disable hardware flow control
369 :type: bool
370
cliechti6066f842009-07-24 00:05:45 +0000371 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000372
373 .. attribute:: dsrdtr
374
Chris Liechti518b0d32015-08-30 02:20:39 +0200375 :getter: Get current hardware flow control setting
376 :setter: Enable or disable hardware flow control
377 :type: bool
378
cliechti6066f842009-07-24 00:05:45 +0000379 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000380
Chris Liechtice621882015-08-06 19:29:31 +0200381 .. attribute:: rs485_mode
382
Chris Liechti518b0d32015-08-30 02:20:39 +0200383 :getter: Get the current RS485 settings
384 :setter: Disable (``None``) or enable the RS485 settings
385 :type: :class:`rs485.RS485Settings` or ``None``
386 :platform: Posix (Linux, limited set of hardware)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100387 :platform: Windows (only RTS on TX possible)
Chris Liechtice621882015-08-06 19:29:31 +0200388
389 Attribute to configure RS485 support. When set to an instance of
390 :class:`rs485.RS485Settings` and supported by OS, RTS will be active
391 when data is sent and inactive otherwise (for reception). The
392 :class:`rs485.RS485Settings` class provides additional settings
393 supported on some platforms.
394
395 .. versionadded:: 3.0
396
397
cliechtiedcdbe42009-07-22 00:48:34 +0000398 The following constants are also provided:
399
400 .. attribute:: BAUDRATES
401
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100402 A list of valid baud rates. The list may be incomplete, such that higher
403 and/or intermediate baud rates may also be supported by the device
404 (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000405
406 .. attribute:: BYTESIZES
407
cliechti25375b52010-07-21 23:27:13 +0000408 A list of valid byte sizes for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000409
410 .. attribute:: PARITIES
411
cliechti25375b52010-07-21 23:27:13 +0000412 A list of valid parities for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000413
414 .. attribute:: STOPBITS
415
cliechti25375b52010-07-21 23:27:13 +0000416 A list of valid stop bit widths for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000417
cliechti4a567a02009-07-27 22:09:31 +0000418
cliechti25375b52010-07-21 23:27:13 +0000419 The following methods are for compatibility with the :mod:`io` library.
cliechti4a567a02009-07-27 22:09:31 +0000420
421 .. method:: readable()
422
423 :return: True
cliechtif4566342009-08-04 00:07:19 +0000424
cliechti4a567a02009-07-27 22:09:31 +0000425 .. versionadded:: 2.5
426
427 .. method:: writable()
428
429 :return: True
cliechtif4566342009-08-04 00:07:19 +0000430
cliechti4a567a02009-07-27 22:09:31 +0000431 .. versionadded:: 2.5
432
433 .. method:: seekable()
434
435 :return: False
cliechtif4566342009-08-04 00:07:19 +0000436
cliechti4a567a02009-07-27 22:09:31 +0000437 .. versionadded:: 2.5
438
439 .. method:: readinto(b)
440
cliechti8611bf42009-08-03 00:34:03 +0000441 :param b: bytearray or array instance
cliechti4a567a02009-07-27 22:09:31 +0000442 :return: Number of byte read
443
cliechtid7e7ce22009-08-03 02:01:07 +0000444 Read up to len(b) bytes into :class:`bytearray` *b* and return the
445 number of bytes read.
cliechti4a567a02009-07-27 22:09:31 +0000446
447 .. versionadded:: 2.5
448
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100449 The port settings can be read and written as dictionary. The following
450 keys are supported: ``write_timeout``, ``inter_byte_timeout``,
451 ``dsrdtr``, ``baudrate``, ``timeout``, ``parity``, ``bytesize``,
452 ``rtscts``, ``stopbits``, ``xonxoff``
cliechti25375b52010-07-21 23:27:13 +0000453
Chris Liechti256ea212015-08-29 23:57:00 +0200454 .. method:: get_settings()
cliechti4065dce2009-08-10 00:55:46 +0000455
456 :return: a dictionary with current port settings.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100457 :rtype: dict
cliechti4065dce2009-08-10 00:55:46 +0000458
459 Get a dictionary with port settings. This is useful to backup the
460 current settings so that a later point in time they can be restored
Chris Liechti518b0d32015-08-30 02:20:39 +0200461 using :meth:`apply_settings`.
cliechti4065dce2009-08-10 00:55:46 +0000462
Chris Liechti19688bf2016-05-06 23:48:36 +0200463 Note that the state of control lines (RTS/DTR) are not part of the
464 settings.
cliechti4065dce2009-08-10 00:55:46 +0000465
466 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100467 .. versionchanged:: 3.0 renamed from ``getSettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000468
Chris Liechti256ea212015-08-29 23:57:00 +0200469 .. method:: apply_settings(d)
cliechti4065dce2009-08-10 00:55:46 +0000470
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100471 :param dict d: a dictionary with port settings.
cliechti4065dce2009-08-10 00:55:46 +0000472
Chris Liechti518b0d32015-08-30 02:20:39 +0200473 Applies a dictionary that was created by :meth:`get_settings`. Only
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100474 changes are applied and when a key is missing, it means that the
475 setting stays unchanged.
cliechti4065dce2009-08-10 00:55:46 +0000476
477 Note that control lines (RTS/DTR) are not changed.
478
479 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100480 .. versionchanged:: 3.0 renamed from ``applySettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000481
cliechti25375b52010-07-21 23:27:13 +0000482 Platform specific methods.
483
cliechtic648da92011-12-29 01:17:51 +0000484 .. warning:: Programs using the following methods and attributes are not
485 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000486
487 .. method:: nonblocking()
488
Chris Liechtice621882015-08-06 19:29:31 +0200489 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000490
Chris Liechti59848422016-06-04 22:28:14 +0200491 .. deprecated:: 3.2
492 The serial port is already opened in this mode. This method is not
493 needed and going away.
494
cliechti25375b52010-07-21 23:27:13 +0000495
496 .. method:: fileno()
497
Chris Liechtice621882015-08-06 19:29:31 +0200498 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000499 :return: File descriptor.
500
501 Return file descriptor number for the port that is opened by this object.
502 It is useful when serial ports are used with :mod:`select`.
503
Chris Liechti518b0d32015-08-30 02:20:39 +0200504 .. method:: set_input_flow_control(enable)
cliechti25375b52010-07-21 23:27:13 +0000505
cliechti2f0f8a32011-12-28 22:10:00 +0000506 :platform: Posix
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100507 :param bool enable: Set flow control state.
cliechti25375b52010-07-21 23:27:13 +0000508
cliechti2f0f8a32011-12-28 22:10:00 +0000509 Manually control flow - when software flow control is enabled.
510
511 This will send XON (true) and XOFF (false) to the other device.
512
Chris Liechti518b0d32015-08-30 02:20:39 +0200513 .. versionadded:: 2.7 (Posix support added)
514 .. versionchanged:: 3.0 renamed from ``flowControlOut``
cliechti2f0f8a32011-12-28 22:10:00 +0000515
Chris Liechti518b0d32015-08-30 02:20:39 +0200516 .. method:: set_output_flow_control(enable)
cliechti2f0f8a32011-12-28 22:10:00 +0000517
Chris Liechti518b0d32015-08-30 02:20:39 +0200518 :platform: Posix (HW and SW flow control)
519 :platform: Windows (SW flow control only)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100520 :param bool enable: Set flow control state.
cliechti2f0f8a32011-12-28 22:10:00 +0000521
522 Manually control flow of outgoing data - when hardware or software flow
523 control is enabled.
524
525 Sending will be suspended when called with ``False`` and enabled when
526 called with ``True``.
527
Chris Liechti518b0d32015-08-30 02:20:39 +0200528 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
529 .. versionchanged:: 3.0 renamed from ``setXON``
Chris Liechti256ea212015-08-29 23:57:00 +0200530
Chris Liechti19688bf2016-05-06 23:48:36 +0200531 .. method:: cancel_read()
Chris Liechti256ea212015-08-29 23:57:00 +0200532
Chris Liechti9d893052016-05-18 22:03:29 +0200533 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200534 :platform: Windows
535
Chris Liechti9d893052016-05-18 22:03:29 +0200536 Cancel a pending read operation from an other thread. A blocking
537 :meth:`read` call is aborted immediately. :meth:`read` will not report
538 any error but return all data received up to that point (similar to a
539 timeout).
540
541 On Posix a call to `cancel_read()` may cancel a future :meth:`read` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200542
543 .. versionadded:: 3.1
544
545 .. method:: cancel_write()
546
Chris Liechti9d893052016-05-18 22:03:29 +0200547 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200548 :platform: Windows
549
Chris Liechti9d893052016-05-18 22:03:29 +0200550 Cancel a pending write operation from an other thread. The
551 :meth:`write` method will return immediately (no error indicated).
552 However the OS may still be sending from the buffer, a separate call to
553 :meth:`reset_output_buffer` may be needed.
554
555 On Posix a call to `cancel_write()` may cancel a future :meth:`write` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200556
557 .. versionadded:: 3.1
Chris Liechti518b0d32015-08-30 02:20:39 +0200558
Chris Liechti9a99cb22015-08-30 22:16:50 +0200559 .. note:: The following members are deprecated and will be removed in a
Chris Liechti518b0d32015-08-30 02:20:39 +0200560 future release.
Chris Liechti256ea212015-08-29 23:57:00 +0200561
562 .. attribute:: portstr
563
Chris Liechti518b0d32015-08-30 02:20:39 +0200564 .. deprecated:: 2.5 use :attr:`name` instead
Chris Liechti256ea212015-08-29 23:57:00 +0200565
566 .. method:: inWaiting()
567
Chris Liechti518b0d32015-08-30 02:20:39 +0200568 .. deprecated:: 3.0 see :attr:`in_waiting`
569
Matthew Westfa58a972016-07-08 14:27:04 -0700570 .. method:: isOpen()
571
572 .. deprecated:: 3.0 see :attr:`is_open`
573
Chris Liechti518b0d32015-08-30 02:20:39 +0200574 .. attribute:: writeTimeout
575
576 .. deprecated:: 3.0 see :attr:`write_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200577
578 .. attribute:: interCharTimeout
579
Chris Liechti518b0d32015-08-30 02:20:39 +0200580 .. deprecated:: 3.0 see :attr:`inter_byte_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200581
582 .. method:: sendBreak(duration=0.25)
583
Chris Liechti518b0d32015-08-30 02:20:39 +0200584 .. deprecated:: 3.0 see :meth:`send_break`
Chris Liechti256ea212015-08-29 23:57:00 +0200585
586 .. method:: flushInput()
587
Chris Liechti518b0d32015-08-30 02:20:39 +0200588 .. deprecated:: 3.0 see :meth:`reset_input_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200589
590 .. method:: flushOutput()
591
Chris Liechti518b0d32015-08-30 02:20:39 +0200592 .. deprecated:: 3.0 see :meth:`reset_output_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200593
594 .. method:: setBreak(level=True)
595
Chris Liechti518b0d32015-08-30 02:20:39 +0200596 .. deprecated:: 3.0 see :attr:`break_condition`
Chris Liechti256ea212015-08-29 23:57:00 +0200597
598 .. method:: setRTS(level=True)
599
Chris Liechti518b0d32015-08-30 02:20:39 +0200600 .. deprecated:: 3.0 see :attr:`rts`
Chris Liechti256ea212015-08-29 23:57:00 +0200601
602 .. method:: setDTR(level=True)
603
Chris Liechti518b0d32015-08-30 02:20:39 +0200604 .. deprecated:: 3.0 see :attr:`dtr`
Chris Liechti256ea212015-08-29 23:57:00 +0200605
606 .. method:: getCTS()
607
Chris Liechti518b0d32015-08-30 02:20:39 +0200608 .. deprecated:: 3.0 see :attr:`cts`
Chris Liechti256ea212015-08-29 23:57:00 +0200609
610 .. method:: getDSR()
611
Chris Liechti518b0d32015-08-30 02:20:39 +0200612 .. deprecated:: 3.0 see :attr:`dsr`
Chris Liechti256ea212015-08-29 23:57:00 +0200613
614 .. method:: getRI()
615
Chris Liechti518b0d32015-08-30 02:20:39 +0200616 .. deprecated:: 3.0 see :attr:`ri`
Chris Liechti256ea212015-08-29 23:57:00 +0200617
618 .. method:: getCD()
619
Chris Liechti518b0d32015-08-30 02:20:39 +0200620 .. deprecated:: 3.0 see :attr:`cd`
Chris Liechti256ea212015-08-29 23:57:00 +0200621
622 .. method:: getSettingsDict()
623
Chris Liechti518b0d32015-08-30 02:20:39 +0200624 .. deprecated:: 3.0 see :meth:`get_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200625
626 .. method:: applySettingsDict(d)
627
Chris Liechti518b0d32015-08-30 02:20:39 +0200628 .. deprecated:: 3.0 see :meth:`apply_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200629
630 .. method:: outWaiting()
631
Chris Liechti518b0d32015-08-30 02:20:39 +0200632 .. deprecated:: 3.0 see :attr:`out_waiting`
Chris Liechti256ea212015-08-29 23:57:00 +0200633
634 .. method:: setXON(level=True)
635
Chris Liechti518b0d32015-08-30 02:20:39 +0200636 .. deprecated:: 3.0 see :meth:`set_output_flow_control`
Chris Liechti256ea212015-08-29 23:57:00 +0200637
638 .. method:: flowControlOut(enable)
639
Chris Liechti518b0d32015-08-30 02:20:39 +0200640 .. deprecated:: 3.0 see :meth:`set_input_flow_control`
cliechtif4566342009-08-04 00:07:19 +0000641
cliechtic648da92011-12-29 01:17:51 +0000642 .. attribute:: rtsToggle
643
644 :platform: Windows
645
646 Attribute to configure RTS toggle control setting. When enabled and
647 supported by OS, RTS will be active when data is available and inactive
648 if no data is available.
649
650 .. versionadded:: 2.6
Chris Liechti518b0d32015-08-30 02:20:39 +0200651 .. versionchanged:: 3.0 (removed, see :attr:`rs485_mode` instead)
cliechtic648da92011-12-29 01:17:51 +0000652
653
cliechtic56e41d2011-08-25 22:06:38 +0000654Implementation detail: some attributes and functions are provided by the
655class :class:`SerialBase` and some by the platform specific class and
656others by the base class mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000657
Chris Liechtice621882015-08-06 19:29:31 +0200658RS485 support
659-------------
660The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to
661enable RS485 specific support on some platforms. Currently Windows and Linux
662(only a small number of devices) are supported.
cliechti4a567a02009-07-27 22:09:31 +0000663
Chris Liechtice621882015-08-06 19:29:31 +0200664:attr:`Serial.rs485_mode` needs to be set to an instance of
665:class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature.
cliechti4a567a02009-07-27 22:09:31 +0000666
Chris Liechtice621882015-08-06 19:29:31 +0200667Usage::
cliechti4a567a02009-07-27 22:09:31 +0000668
Chris Liechtice621882015-08-06 19:29:31 +0200669 ser = serial.Serial(...)
670 ser.rs485_mode = serial.rs485.RS485Settings(...)
671 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000672
Chris Liechtice621882015-08-06 19:29:31 +0200673There is a subclass :class:`rs485.RS485` available to emulate the RS485 support
674on regular serial ports.
cliechti4a567a02009-07-27 22:09:31 +0000675
cliechti4a567a02009-07-27 22:09:31 +0000676
Chris Liechtice621882015-08-06 19:29:31 +0200677.. class:: rs485.RS485Settings
cliechti4a567a02009-07-27 22:09:31 +0000678
Chris Liechtice621882015-08-06 19:29:31 +0200679 A class that holds RS485 specific settings which are supported on
680 some platforms.
cliechti4a567a02009-07-27 22:09:31 +0000681
Chris Liechtice621882015-08-06 19:29:31 +0200682 .. versionadded:: 3.0
cliechti4a567a02009-07-27 22:09:31 +0000683
Chris Liechtice621882015-08-06 19:29:31 +0200684 .. 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 +0000685
Chris Liechtice621882015-08-06 19:29:31 +0200686 :param bool rts_level_for_tx:
687 RTS level for transmission
cliechti4a567a02009-07-27 22:09:31 +0000688
Chris Liechtice621882015-08-06 19:29:31 +0200689 :param bool rts_level_for_rx:
690 RTS level for reception
cliechti4a567a02009-07-27 22:09:31 +0000691
Chris Liechtice621882015-08-06 19:29:31 +0200692 :param bool loopback:
693 When set to ``True`` transmitted data is also received.
cliechti4a567a02009-07-27 22:09:31 +0000694
Chris Liechtice621882015-08-06 19:29:31 +0200695 :param float delay_before_tx:
696 Delay after setting RTS but before transmission starts
697
698 :param float delay_before_rx:
699 Delay after transmission ends and resetting RTS
700
701 .. attribute:: rts_level_for_tx
702
703 RTS level for transmission.
704
705 .. attribute:: rts_level_for_rx
706
707 RTS level for reception.
708
709 .. attribute:: loopback
710
711 When set to ``True`` transmitted data is also received.
712
713 .. attribute:: delay_before_tx
714
715 Delay after setting RTS but before transmission starts (seconds as float).
716
717 .. attribute:: delay_before_rx
718
719 Delay after transmission ends and resetting RTS (seconds as float).
cliechti4a567a02009-07-27 22:09:31 +0000720
721
Chris Liechtice621882015-08-06 19:29:31 +0200722.. class:: rs485.RS485
cliechti4a567a02009-07-27 22:09:31 +0000723
Chris Liechtice621882015-08-06 19:29:31 +0200724 A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS
725 according to the RS485 settings.
cliechti4a567a02009-07-27 22:09:31 +0000726
Chris Liechti518b0d32015-08-30 02:20:39 +0200727 Usage::
728
729 ser = serial.rs485.RS485(...)
730 ser.rs485_mode = serial.rs485.RS485Settings(...)
731 ser.write(b'hello')
732
Chris Liechtice621882015-08-06 19:29:31 +0200733 .. warning:: This may work unreliably on some serial ports (control signals not
734 synchronized or delayed compared to data). Using delays may be unreliable
735 (varying times, larger than expected) as the OS may not support very fine
736 grained delays (no smaller than in the order of tens of milliseconds).
cliechti4a567a02009-07-27 22:09:31 +0000737
Chris Liechtice621882015-08-06 19:29:31 +0200738 .. note:: Some implementations support this natively in the class
739 :class:`Serial`. Better performance can be expected when the native version
740 is used.
cliechti4a567a02009-07-27 22:09:31 +0000741
Chris Liechtice621882015-08-06 19:29:31 +0200742 .. note:: The loopback property is ignored by this implementation. The actual
743 behavior depends on the used hardware.
cliechti4a567a02009-07-27 22:09:31 +0000744
cliechti4a567a02009-07-27 22:09:31 +0000745
cliechtie214ff82010-07-21 15:48:47 +0000746
cliechti25375b52010-07-21 23:27:13 +0000747
cliechti7aed8332009-08-05 14:19:31 +0000748:rfc:`2217` Network ports
749-------------------------
750
751.. warning:: This implementation is currently in an experimental state. Use
752 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000753
cliechtiec4ac1b2009-08-02 00:47:21 +0000754.. class:: rfc2217.Serial
755
Chris Liechti2a1befc2015-10-21 17:37:08 +0200756 This implements a :rfc:`2217` compatible client. Port names are :ref:`URL
757 <URLs>` in the form: ``rfc2217://<host>:<port>[?<option>[&<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000758
cliechtiec4ac1b2009-08-02 00:47:21 +0000759 This class API is compatible to :class:`Serial` with a few exceptions:
760
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100761 - ``write_timeout`` is not implemented
cliechtiec4ac1b2009-08-02 00:47:21 +0000762 - The current implementation starts a thread that keeps reading from the
763 (internal) socket. The thread is managed automatically by the
764 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
765 However it may be a problem for user applications that like to use select
766 instead of threads.
767
768 Due to the nature of the network and protocol involved there are a few
769 extra points to keep in mind:
770
771 - All operations have an additional latency time.
772 - Setting control lines (RTS/CTS) needs more time.
773 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
774 cache is updated depends entirely on the server. The server itself may
775 implement a polling at a certain rate and quick changes may be invisible.
776 - The network layer also has buffers. This means that :meth:`flush`,
Chris Liechti518b0d32015-08-30 02:20:39 +0200777 :meth:`reset_input_buffer` and :meth:`reset_output_buffer` may work with
778 additional delay. Likewise :attr:`in_waiting` returns the size of the
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100779 data arrived at the objects internal buffer and excludes any bytes in the
Chris Liechti518b0d32015-08-30 02:20:39 +0200780 network buffers or any server side buffer.
cliechtiec4ac1b2009-08-02 00:47:21 +0000781 - Closing and immediately reopening the same port may fail due to time
782 needed by the server to get ready again.
783
784 Not implemented yet / Possible problems with the implementation:
785
cliechti8611bf42009-08-03 00:34:03 +0000786 - :rfc:`2217` flow control between client and server (objects internal
787 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000788 - No authentication support (servers may not prompt for a password etc.)
789 - No encryption.
790
791 Due to lack of authentication and encryption it is not suitable to use this
792 client for connections across the internet and should only be used in
793 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000794
cliechti7c640ed2009-08-02 00:54:51 +0000795 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000796
cliechti7aed8332009-08-05 14:19:31 +0000797
798.. class:: rfc2217.PortManager
799
800 This class provides helper functions for implementing :rfc:`2217`
801 compatible servers.
802
Chris Liechti518b0d32015-08-30 02:20:39 +0200803 Basically, it implements everything needed for the :rfc:`2217` protocol.
cliechti7aed8332009-08-05 14:19:31 +0000804 It just does not open sockets and read/write to serial ports (though it
805 changes other port settings). The user of this class must take care of the
806 data transmission itself. The reason for that is, that this way, this class
807 supports all programming models such as threads and select.
808
809 Usage examples can be found in the examples where two TCP/IP - serial
810 converters are shown, one using threads (the single port server) and an
811 other using select (the multi port server).
812
813 .. note:: Each new client connection must create a new instance as this
814 object (and the :rfc:`2217` protocol) has internal state.
815
816 .. method:: __init__(serial_port, connection, debug_output=False)
817
818 :param serial_port: a :class:`Serial` instance that is managed.
819 :param connection: an object implementing :meth:`write`, used to write
820 to the network.
cliechti86844e82009-08-12 00:05:33 +0000821 :param debug_output: enables debug messages: a :class:`logging.Logger`
822 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000823
824 Initializes the Manager and starts negotiating with client in Telnet
825 and :rfc:`2217` protocol. The negotiation starts immediately so that
826 the class should be instantiated in the moment the client connects.
827
828 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000829 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000830 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
831 the :meth:`filter` method.
832
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100833 The *connection* object must implement a :meth:`write` function.
cliechti7aed8332009-08-05 14:19:31 +0000834 This function must ensure that *data* is written at once (no user data
835 mixed in, i.e. it must be thread-safe). All data must be sent in its
836 raw form (:meth:`escape` must not be used) as it is used to send Telnet
837 and :rfc:`2217` control commands.
838
cliechti86844e82009-08-12 00:05:33 +0000839 For diagnostics of the connection or the implementation, *debug_output*
840 can be set to an instance of a :class:`logging.Logger` (e.g.
841 ``logging.getLogger('rfc2217.server')``). The caller should configure
842 the logger using ``setLevel`` for the desired detail level of the logs.
843
cliechti7aed8332009-08-05 14:19:31 +0000844 .. method:: escape(data)
845
846 :param data: data to be sent over the network.
847 :return: data, escaped for Telnet/:rfc:`2217`
848
849 A generator that escapes all data to be compatible with :rfc:`2217`.
850 Implementors of servers should use this function to process all data
851 sent over the network.
852
853 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000854 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000855
856 .. method:: filter(data)
857
858 :param data: data read from the network, including Telnet and
859 :rfc:`2217` controls.
860 :return: data, free from Telnet and :rfc:`2217` controls.
861
862 A generator that filters and processes all data related to :rfc:`2217`.
863 Implementors of servers should use this function to process all data
864 received from the network.
865
866 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000867 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000868
cliechti7cb78e82009-08-05 15:47:57 +0000869 .. method:: check_modem_lines(force_notification=False)
870
871 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000872
873 This function needs to be called periodically (e.g. every second) when
874 the server wants to send NOTIFY_MODEMSTATE messages. This is required
875 to support the client for reading CTS/DSR/RI/CD status lines.
876
877 The function reads the status line and issues the notifications
878 automatically.
879
880 .. versionadded:: 2.5
881
cliechtiec4ac1b2009-08-02 00:47:21 +0000882.. seealso::
883
884 :rfc:`2217` - Telnet Com Port Control Option
885
886
cliechtic1c37602009-07-21 01:34:57 +0000887Exceptions
888==========
889
890.. exception:: SerialException
891
892 Base class for serial port exceptions.
893
cliechti4a567a02009-07-27 22:09:31 +0000894 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000895 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000896
cliechtic1c37602009-07-21 01:34:57 +0000897.. exception:: SerialTimeoutException
898
899 Exception that is raised on write timeouts.
900
901
902Constants
903=========
904
cliechti87686242009-08-18 00:58:31 +0000905*Parity*
906
cliechtic1c37602009-07-21 01:34:57 +0000907.. data:: PARITY_NONE
908.. data:: PARITY_EVEN
909.. data:: PARITY_ODD
910.. data:: PARITY_MARK
911.. data:: PARITY_SPACE
912
cliechti87686242009-08-18 00:58:31 +0000913*Stop bits*
914
cliechtic1c37602009-07-21 01:34:57 +0000915.. data:: STOPBITS_ONE
916.. data:: STOPBITS_ONE_POINT_FIVE
917.. data:: STOPBITS_TWO
918
cliechti06281be2011-08-25 23:08:29 +0000919Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +0000920bits.
921
cliechti87686242009-08-18 00:58:31 +0000922*Byte size*
923
cliechtic1c37602009-07-21 01:34:57 +0000924.. data:: FIVEBITS
925.. data:: SIXBITS
926.. data:: SEVENBITS
927.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000928
cliechti87686242009-08-18 00:58:31 +0000929
930*Others*
931
cliechti8611bf42009-08-03 00:34:03 +0000932Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000933software flow control:
cliechti6066f842009-07-24 00:05:45 +0000934
cliechti5b7d16a2009-07-21 21:53:59 +0000935.. data:: XON
936.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000937
cliechti4a567a02009-07-27 22:09:31 +0000938Module version:
cliechtif81362e2009-07-25 03:44:33 +0000939
940.. data:: VERSION
941
Chris Liechti518b0d32015-08-30 02:20:39 +0200942 A string indicating the pySerial version, such as ``3.0``.
cliechtif81362e2009-07-25 03:44:33 +0000943
cliechti87686242009-08-18 00:58:31 +0000944 .. versionadded:: 2.3
945
cliechti44eb98f2011-03-21 21:41:10 +0000946
cliechtie542b362011-03-18 00:49:16 +0000947Module functions and attributes
948===============================
cliechtif81362e2009-07-25 03:44:33 +0000949
950.. function:: device(number)
951
Chris Liechti518b0d32015-08-30 02:20:39 +0200952 .. versionchanged:: 3.0 removed, use ``serial.tools.list_ports`` instead
953
cliechti7c640ed2009-08-02 00:54:51 +0000954
cliechtie3ab3532009-08-05 12:40:38 +0000955.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +0000956
cliechti86844e82009-08-12 00:05:33 +0000957 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +0000958 :param do_not_open: When set to true, the serial port is not opened.
959 :return: an instance of :class:`Serial` or a compatible object.
960
961 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +0000962 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +0000963 to support both, local ports and remote ports. There is also support
Chris Liechti589c92a2015-09-04 23:04:34 +0200964 for other types, see :ref:`URL <URLs>` section.
cliechti7c640ed2009-08-02 00:54:51 +0000965
cliechtid7e7ce22009-08-03 02:01:07 +0000966 The port is not opened when a keyword parameter called *do_not_open* is
967 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +0000968
969 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +0000970
cliechti87686242009-08-18 00:58:31 +0000971
cliechtie542b362011-03-18 00:49:16 +0000972.. attribute:: protocol_handler_packages
973
974 This attribute is a list of package names (strings) that is searched for
975 protocol handlers.
976
977 e.g. we want to support a URL ``foobar://``. A module
978 ``my_handlers.protocol_foobar`` is provided by the user::
979
980 serial.protocol_handler_packages.append("my_handlers")
981 s = serial.serial_for_url("foobar://")
982
983 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
984 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
985 ``PACKAGE`` from this list.
986
987 .. versionadded:: 2.6
988
989
cliechti25375b52010-07-21 23:27:13 +0000990.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +0000991
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100992 :param sequence: bytes, bytearray or memoryview
cliechti25375b52010-07-21 23:27:13 +0000993 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +0000994
cliechtie542b362011-03-18 00:49:16 +0000995 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +0000996 compatible to Python 2.x and 3.x.
997
cliechtie542b362011-03-18 00:49:16 +0000998 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +0000999 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +00001000 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +00001001
1002 This function is used internally and in the unit tests.
1003
cliechtie542b362011-03-18 00:49:16 +00001004 .. versionadded:: 2.5
1005
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001006.. function:: iterbytes(sequence)
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001007
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001008 :param sequence: bytes, bytearray or memoryview
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001009 :returns: a generator that yields bytes
1010
1011 Some versions of Python (3.x) would return integers instead of bytes when
1012 looping over an instance of ``bytes``. This helper function ensures that
1013 bytes are returned.
1014
1015 .. versionadded:: 3.0
1016
cliechtie214ff82010-07-21 15:48:47 +00001017
Chris Liechti811bf382015-10-10 00:54:47 +02001018Threading
1019=========
1020
1021.. module:: serial.threaded
1022.. versionadded:: 3.0
1023
1024.. warning:: This implementation is currently in an experimental state. Use
1025 at your own risk.
1026
1027This module provides classes to simplify working with threads and protocols.
1028
1029.. class:: Protocol
1030
Chris Liechti5665d572015-10-13 23:50:01 +02001031 Protocol as used by the :class:`ReaderThread`. This base class provides empty
Chris Liechti811bf382015-10-10 00:54:47 +02001032 implementations of all methods.
1033
1034
1035 .. method:: connection_made(transport)
1036
1037 :param transport: instance used to write to serial port.
1038
1039 Called when reader thread is started.
1040
1041 .. method:: data_received(data)
1042
Chris Liechtif8a09192015-12-20 23:36:38 +01001043 :param bytes data: received bytes
Chris Liechti811bf382015-10-10 00:54:47 +02001044
1045 Called with snippets received from the serial port.
1046
1047 .. method:: connection_lost(exc)
1048
1049 :param exc: Exception if connection was terminated by error else ``None``
1050
1051 Called when the serial port is closed or the reader loop terminated
1052 otherwise.
1053
1054.. class:: Packetizer(Protocol)
1055
1056 Read binary packets from serial port. Packets are expected to be terminated
1057 with a ``TERMINATOR`` byte (null byte by default).
1058
1059 The class also keeps track of the transport.
1060
1061 .. attribute:: TERMINATOR = b'\\0'
1062
1063 .. method:: __init__()
1064
1065 .. method:: connection_made(transport)
1066
Chris Liechtif8a09192015-12-20 23:36:38 +01001067 Stores transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001068
1069 .. method:: connection_lost(exc)
1070
Chris Liechtif8a09192015-12-20 23:36:38 +01001071 Forgets transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001072
1073 .. method:: data_received(data)
1074
Chris Liechtif8a09192015-12-20 23:36:38 +01001075 :param bytes data: partial received data
1076
1077 Buffer received data and search for :attr:`TERMINATOR`, when found,
Chris Liechti811bf382015-10-10 00:54:47 +02001078 call :meth:`handle_packet`.
1079
1080 .. method:: handle_packet(packet)
1081
Chris Liechti1c4c5992016-01-18 20:46:00 +01001082 :param bytes packet: a packet as defined by ``TERMINATOR``
1083
Chris Liechti811bf382015-10-10 00:54:47 +02001084 Process packets - to be overridden by subclassing.
1085
1086
1087.. class:: LineReader(Packetizer)
1088
1089 Read and write (Unicode) lines from/to serial port.
1090 The encoding is applied.
1091
1092
1093 .. attribute:: TERMINATOR = b'\\r\\n'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001094
1095 Line ending.
1096
Chris Liechti811bf382015-10-10 00:54:47 +02001097 .. attribute:: ENCODING = 'utf-8'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001098
1099 Encoding of the send and received data.
1100
Chris Liechti811bf382015-10-10 00:54:47 +02001101 .. attribute:: UNICODE_HANDLING = 'replace'
1102
Chris Liechti1c4c5992016-01-18 20:46:00 +01001103 Unicode error handly policy.
1104
Chris Liechti811bf382015-10-10 00:54:47 +02001105 .. method:: handle_packet(packet)
1106
Chris Liechti1c4c5992016-01-18 20:46:00 +01001107 :param bytes packet: a packet as defined by ``TERMINATOR``
1108
1109 In this case it will be a line, calls :meth:`handle_line` after applying
1110 the :attr:`ENCODING`.
1111
Chris Liechti811bf382015-10-10 00:54:47 +02001112 .. method:: handle_line(line)
1113
Chris Liechtif8a09192015-12-20 23:36:38 +01001114 :param str line: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001115
1116 Process one line - to be overridden by subclassing.
1117
1118 .. method:: write_line(text)
1119
Chris Liechtif8a09192015-12-20 23:36:38 +01001120 :param str text: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001121
Chris Liechtif8a09192015-12-20 23:36:38 +01001122 Write *text* to the transport. *text* is expected to be a Unicode
1123 string and the encoding is applied before sending and also the
1124 :attr:`TERMINATOR` (new line) is appended.
Chris Liechti811bf382015-10-10 00:54:47 +02001125
1126
Chris Liechti5665d572015-10-13 23:50:01 +02001127.. class:: ReaderThread(threading.Thread)
Chris Liechti811bf382015-10-10 00:54:47 +02001128
1129 Implement a serial port read loop and dispatch to a Protocol instance (like
1130 the :class:`asyncio.Protocol`) but do it with threads.
1131
Chris Liechtif8a09192015-12-20 23:36:38 +01001132 Calls to :meth:`close` will close the serial port but it is also possible
1133 to just :meth:`stop` this thread and continue to use the serial port
1134 instance otherwise.
Chris Liechti811bf382015-10-10 00:54:47 +02001135
1136 .. method:: __init__(serial_instance, protocol_factory)
1137
1138 :param serial_instance: serial port instance (opened) to be used.
1139 :param protocol_factory: a callable that returns a Protocol instance
1140
1141 Initialize thread.
1142
Chris Liechtif8a09192015-12-20 23:36:38 +01001143 Note that the ``serial_instance`` 's timeout is set to one second!
Chris Liechti811bf382015-10-10 00:54:47 +02001144 Other settings are not changed.
1145
1146 .. method:: stop()
1147
1148 Stop the reader thread.
1149
1150 .. method:: run()
1151
1152 The actual reader loop driven by the thread. It calls
1153 :meth:`Protocol.connection_made`, reads from the serial port calling
Chris Liechtif8a09192015-12-20 23:36:38 +01001154 :meth:`Protocol.data_received` and finally calls :meth:`Protocol.connection_lost`
Chris Liechti811bf382015-10-10 00:54:47 +02001155 when :meth:`close` is called or an error occurs.
1156
1157 .. method:: write(data)
1158
Chris Liechtif8a09192015-12-20 23:36:38 +01001159 :param bytes data: data to write
1160
Chris Liechti811bf382015-10-10 00:54:47 +02001161 Thread safe writing (uses lock).
1162
1163 .. method:: close()
1164
1165 Close the serial port and exit reader thread, calls :meth:`stop` (uses lock).
1166
1167 .. method:: connect()
1168
1169 Wait until connection is set up and return the transport and protocol
1170 instances.
1171
1172
1173 This class can be used as context manager, in this case it starts
1174 the thread and connects automatically. The serial port is closed
1175 when the context is left.
1176
1177 .. method:: __enter__()
1178
1179 :returns: protocol
1180
1181 Connect and return protocol instance.
1182
1183 .. method:: __exit__(exc_type, exc_val, exc_tb)
1184
1185 Closes serial port.
1186
Chris Liechtia1436b12015-10-12 23:19:04 +02001187Example::
1188
1189 class PrintLines(LineReader):
1190 def connection_made(self, transport):
1191 super(PrintLines, self).connection_made(transport)
1192 sys.stdout.write('port opened\n')
1193 self.write_line('hello world')
1194
1195 def handle_line(self, data):
1196 sys.stdout.write('line received: {}\n'.format(repr(data)))
1197
1198 def connection_lost(self, exc):
1199 if exc:
1200 traceback.print_exc(exc)
1201 sys.stdout.write('port closed\n')
1202
1203 ser = serial.serial_for_url('loop://', baudrate=115200, timeout=1)
Chris Liechti5665d572015-10-13 23:50:01 +02001204 with ReaderThread(ser, PrintLines) as protocol:
Chris Liechtia1436b12015-10-12 23:19:04 +02001205 protocol.write_line('hello')
1206 time.sleep(2)
1207
Chris Liechti811bf382015-10-10 00:54:47 +02001208
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001209asyncio
1210=======
1211
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001212``asyncio`` was introduced with Python 3.4. Experimental support for pySerial
1213is provided via a separate distribution `pyserial-asyncio`_.
Chris Liechti589c92a2015-09-04 23:04:34 +02001214
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001215It is currently under developement, see:
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001216
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001217- http://pyserial-asyncio.readthedocs.io/
1218- https://github.com/pyserial/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001219
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001220.. _`pyserial-asyncio`: https://pypi.python.org/pypi/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001221