blob: 2045244a7ad25cb84a7f46f9db7c2210d4e094a7 [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
Rob Gaddi636cc642017-02-24 11:39:46 -080015 .. 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, exclusive=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).
Chris Liechti1c4bc812017-03-08 02:44:04 +010055
Rob Gaddi636cc642017-02-24 11:39:46 -080056 :param bool exclusive:
Rob Gaddib1e3e322017-02-24 11:40:06 -080057 Set exclusive access mode (POSIX only). A port cannot be opened in
58 exclusive access mode if it is already open in exclusive access mode.
cliechtic1c37602009-07-21 01:34:57 +000059
cliechti6066f842009-07-24 00:05:45 +000060 :exception ValueError:
cliechti4a567a02009-07-27 22:09:31 +000061 Will be raised when parameter are out of range, e.g. baud rate, data bits.
cliechti6066f842009-07-24 00:05:45 +000062
63 :exception SerialException:
64 In case the device can not be found or can not be configured.
65
66
cliechti4a567a02009-07-27 22:09:31 +000067 The port is immediately opened on object creation, when a *port* is
68 given. It is not opened when *port* is :const:`None` and a successive call
Chris Liechti32ccf2e2015-12-19 23:42:40 +010069 to :meth:`open` is required.
cliechti4a567a02009-07-27 22:09:31 +000070
Chris Liechti518b0d32015-08-30 02:20:39 +020071 *port* is a device name: depending on operating system. e.g.
72 ``/dev/ttyUSB0`` on GNU/Linux or ``COM3`` on Windows.
cliechtic1c37602009-07-21 01:34:57 +000073
cliechti25375b52010-07-21 23:27:13 +000074 The parameter *baudrate* can be one of the standard values:
75 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
76 9600, 19200, 38400, 57600, 115200.
cliechti28b8fd02011-12-28 21:39:42 +000077 These are well supported on all platforms.
78
79 Standard values above 115200, such as: 230400, 460800, 500000, 576000,
80 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000,
81 4000000 also work on many platforms and devices.
cliechti25375b52010-07-21 23:27:13 +000082
cliechti06281be2011-08-25 23:08:29 +000083 Non-standard values are also supported on some platforms (GNU/Linux, MAC
cliechti25375b52010-07-21 23:27:13 +000084 OSX >= Tiger, Windows). Though, even on these platforms some serial
85 ports may reject non-standard values.
86
Chris Liechti3e0dcc72015-12-18 23:23:26 +010087 Possible values for the parameter *timeout* which controls the behavior
88 of :meth:`read`:
cliechti5134aab2009-07-21 19:47:59 +000089
Chris Liechti3e0dcc72015-12-18 23:23:26 +010090 - ``timeout = None``: wait forever / until requested number of bytes
91 are received
92 - ``timeout = 0``: non-blocking mode, return immediately in any case,
93 returning zero or more, up to the requested number of bytes
cliechtif81362e2009-07-25 03:44:33 +000094 - ``timeout = x``: set timeout to ``x`` seconds (float allowed)
Chris Liechti3e0dcc72015-12-18 23:23:26 +010095 returns immediately when the requested number of bytes are available,
96 otherwise wait until the timeout expires and return all bytes that
97 were received until then.
cliechtic1c37602009-07-21 01:34:57 +000098
Chris Liechti3e0dcc72015-12-18 23:23:26 +010099 :meth:`write` is blocking by default, unless *write_timeout* is set.
100 For possible values refer to the list for *timeout* above.
cliechti07709e42010-05-21 00:30:09 +0000101
cliechti8611bf42009-08-03 00:34:03 +0000102 Note that enabling both flow control methods (*xonxoff* and *rtscts*)
103 together may not be supported. It is common to use one of the methods
104 at once, not both.
cliechtic1c37602009-07-21 01:34:57 +0000105
cliechti07709e42010-05-21 00:30:09 +0000106 *dsrdtr* is not supported by all platforms (silently ignored). Setting
107 it to ``None`` has the effect that its state follows *rtscts*.
108
cliechti25375b52010-07-21 23:27:13 +0000109 Also consider using the function :func:`serial_for_url` instead of
110 creating Serial instances directly.
111
cliechti5c72e4d2010-07-21 14:04:54 +0000112 .. versionchanged:: 2.5
cliechti06281be2011-08-25 23:08:29 +0000113 *dsrdtr* now defaults to ``False`` (instead of *None*)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100114 .. versionchanged:: 3.0 numbers as *port* argument are no longer supported
Chris Liechti1c4bc812017-03-08 02:44:04 +0100115 .. versionadded:: 3.3 ``exclusive`` flag
cliechti5c72e4d2010-07-21 14:04:54 +0000116
cliechtic1c37602009-07-21 01:34:57 +0000117 .. method:: open()
118
Chris Liechtiaefbf032016-09-30 23:57:22 +0200119 Open port. The state of :attr:`rts` and :attr:`dtr` is applied.
120
Chris Liechti5e953c32016-09-28 18:18:25 +0200121 .. note::
Chris Liechtiaefbf032016-09-30 23:57:22 +0200122
Chris Liechti5e953c32016-09-28 18:18:25 +0200123 Some OS and/or drivers may activate RTS and or DTR automatically,
124 as soon as the port is opened. There may be a glitch on RTS/DTR
Jakub Wilkbbe6d832017-04-08 15:43:36 +0200125 when :attr:`rts` or :attr:`dtr` are set differently from their
Chris Liechti5e953c32016-09-28 18:18:25 +0200126 default value (``True`` / active).
cliechtic1c37602009-07-21 01:34:57 +0000127
Chris Liechtiaefbf032016-09-30 23:57:22 +0200128 .. note::
129
130 For compatibility reasons, no error is reported when applying
131 :attr:`rts` or :attr:`dtr` fails on POSIX due to EINVAL (22) or
132 ENOTTY (25).
133
cliechtic1c37602009-07-21 01:34:57 +0000134 .. method:: close()
135
136 Close port immediately.
137
cliechti25375b52010-07-21 23:27:13 +0000138 .. method:: __del__()
139
140 Destructor, close port when serial port instance is freed.
141
cliechtic1c37602009-07-21 01:34:57 +0000142
Chris Liechti256ea212015-08-29 23:57:00 +0200143 The following methods may raise :exc:`SerialException` when applied to a closed
cliechti4a567a02009-07-27 22:09:31 +0000144 port.
cliechtic1c37602009-07-21 01:34:57 +0000145
146 .. method:: read(size=1)
147
cliechtibb5c3c52009-07-23 02:29:27 +0000148 :param size: Number of bytes to read.
149 :return: Bytes read from the port.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100150 :rtype: bytes
cliechtic1c37602009-07-21 01:34:57 +0000151
cliechti4a567a02009-07-27 22:09:31 +0000152 Read *size* bytes from the serial port. If a timeout is set it may
cliechtibb5c3c52009-07-23 02:29:27 +0000153 return less characters as requested. With no timeout it will block
154 until the requested number of bytes is read.
cliechtic1c37602009-07-21 01:34:57 +0000155
cliechti4a567a02009-07-27 22:09:31 +0000156 .. versionchanged:: 2.5
157 Returns an instance of :class:`bytes` when available (Python 2.6
cliechti8611bf42009-08-03 00:34:03 +0000158 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000159
cliechtibb5c3c52009-07-23 02:29:27 +0000160 .. method:: write(data)
161
162 :param data: Data to send.
cliechti4a567a02009-07-27 22:09:31 +0000163 :return: Number of bytes written.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100164 :rtype: int
cliechti6066f842009-07-24 00:05:45 +0000165 :exception SerialTimeoutException:
166 In case a write timeout is configured for the port and the time is
167 exceeded.
168
Chris Liechti9ad044a2015-12-17 19:43:59 +0100169 Write the bytes *data* to the port. This should be of type ``bytes``
170 (or compatible such as ``bytearray`` or ``memoryview``). Unicode
171 strings must be encoded (e.g. ``'hello'.encode('utf-8'``).
cliechtic1c37602009-07-21 01:34:57 +0000172
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100173 .. versionchanged:: 2.5
cliechtiddd78132009-07-28 01:13:28 +0000174 Accepts instances of :class:`bytes` and :class:`bytearray` when
cliechti8611bf42009-08-03 00:34:03 +0000175 available (Python 2.6 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000176
cliechti9a1c6952009-10-20 22:18:28 +0000177 .. versionchanged:: 2.5
178 Write returned ``None`` in previous versions.
179
Chris Liechti518b0d32015-08-30 02:20:39 +0200180 .. method:: flush()
181
182 Flush of file like objects. In this case, wait until all data is
183 written.
184
Chris Liechti256ea212015-08-29 23:57:00 +0200185 .. attribute:: in_waiting
cliechti4a567a02009-07-27 22:09:31 +0000186
Chris Liechti256ea212015-08-29 23:57:00 +0200187 :getter: Get the number of bytes in the input buffer
188 :type: int
189
190 Return the number of bytes in the receive buffer.
cliechti4a567a02009-07-27 22:09:31 +0000191
Chris Liechti518b0d32015-08-30 02:20:39 +0200192 .. versionchanged:: 3.0 changed to property from ``inWaiting()``
cliechtic1c37602009-07-21 01:34:57 +0000193
Chris Liechti518b0d32015-08-30 02:20:39 +0200194 .. attribute:: out_waiting
195
196 :getter: Get the number of bytes in the output buffer
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100197 :type: int
Chris Liechti518b0d32015-08-30 02:20:39 +0200198 :platform: Posix
199 :platform: Windows
200
201 Return the number of bytes in the output buffer.
202
203 .. versionchanged:: 2.7 (Posix support added)
204 .. versionchanged:: 3.0 changed to property from ``outWaiting()``
cliechtic1c37602009-07-21 01:34:57 +0000205
Chris Liechti256ea212015-08-29 23:57:00 +0200206 .. method:: reset_input_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000207
Chris Liechti4f988d42017-03-20 23:59:40 +0100208 Flush input buffer, discarding all its contents.
cliechtic1c37602009-07-21 01:34:57 +0000209
Chris Liechti518b0d32015-08-30 02:20:39 +0200210 .. versionchanged:: 3.0 renamed from ``flushInput()``
211
Chris Liechti256ea212015-08-29 23:57:00 +0200212 .. method:: reset_output_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000213
214 Clear output buffer, aborting the current output and
215 discarding all that is in the buffer.
216
Chris Liechti48e40e92016-05-24 00:09:12 +0200217 Note, for some USB serial adapters, this may only flush the buffer of
218 the OS and not all the data that may be present in the USB part.
219
Chris Liechti518b0d32015-08-30 02:20:39 +0200220 .. versionchanged:: 3.0 renamed from ``flushOutput()``
221
Chris Liechti256ea212015-08-29 23:57:00 +0200222 .. method:: send_break(duration=0.25)
cliechtic1c37602009-07-21 01:34:57 +0000223
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100224 :param float duration: Time to activate the BREAK condition.
cliechtibb5c3c52009-07-23 02:29:27 +0000225
cliechtic1c37602009-07-21 01:34:57 +0000226 Send break condition. Timed, returns to idle state after given
227 duration.
228
cliechtic1c37602009-07-21 01:34:57 +0000229
Chris Liechti256ea212015-08-29 23:57:00 +0200230 .. attribute:: break_condition
cliechtibb5c3c52009-07-23 02:29:27 +0000231
Chris Liechti256ea212015-08-29 23:57:00 +0200232 :getter: Get the current BREAK state
233 :setter: Control the BREAK state
234 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000235
Chris Liechti256ea212015-08-29 23:57:00 +0200236 When set to ``True`` activate BREAK condition, else disable.
237 Controls TXD. When active, no transmitting is possible.
cliechtic1c37602009-07-21 01:34:57 +0000238
Chris Liechti256ea212015-08-29 23:57:00 +0200239 .. attribute:: rts
cliechtibb5c3c52009-07-23 02:29:27 +0000240
Chris Liechti256ea212015-08-29 23:57:00 +0200241 :setter: Set the state of the RTS line
242 :getter: Return the state of the RTS line
243 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000244
Chris Liechti256ea212015-08-29 23:57:00 +0200245 Set RTS line to specified logic level. It is possible to assign this
Connor Weeksc9661f72018-04-06 09:49:15 -0600246 value before opening the serial port, then the value is applied upon
Chris Liechti5e953c32016-09-28 18:18:25 +0200247 :meth:`open` (with restrictions, see :meth:`open`).
cliechtic1c37602009-07-21 01:34:57 +0000248
Chris Liechti256ea212015-08-29 23:57:00 +0200249 .. attribute:: dtr
cliechtibb5c3c52009-07-23 02:29:27 +0000250
Chris Liechti256ea212015-08-29 23:57:00 +0200251 :setter: Set the state of the DTR line
252 :getter: Return the state of the DTR line
253 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000254
Chris Liechti256ea212015-08-29 23:57:00 +0200255 Set DTR line to specified logic level. It is possible to assign this
Connor Weeksc9661f72018-04-06 09:49:15 -0600256 value before opening the serial port, then the value is applied upon
Chris Liechti5e953c32016-09-28 18:18:25 +0200257 :meth:`open` (with restrictions, see :meth:`open`).
cliechtic1c37602009-07-21 01:34:57 +0000258
Chris Liechti518b0d32015-08-30 02:20:39 +0200259 Read-only attributes:
260
261 .. attribute:: name
262
263 :getter: Device name.
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100264 :type: str
Chris Liechti518b0d32015-08-30 02:20:39 +0200265
266 .. versionadded:: 2.5
267
Chris Liechti256ea212015-08-29 23:57:00 +0200268 .. attribute:: cts
269
270 :getter: Get the state of the CTS line
271 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000272
cliechtic1c37602009-07-21 01:34:57 +0000273 Return the state of the CTS line.
274
Chris Liechti256ea212015-08-29 23:57:00 +0200275 .. attribute:: dsr
cliechtic1c37602009-07-21 01:34:57 +0000276
Chris Liechti256ea212015-08-29 23:57:00 +0200277 :getter: Get the state of the DSR line
278 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000279
cliechtic1c37602009-07-21 01:34:57 +0000280 Return the state of the DSR line.
281
Chris Liechti256ea212015-08-29 23:57:00 +0200282 .. attribute:: ri
cliechtic1c37602009-07-21 01:34:57 +0000283
Chris Liechti256ea212015-08-29 23:57:00 +0200284 :getter: Get the state of the RI line
285 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000286
cliechtic1c37602009-07-21 01:34:57 +0000287 Return the state of the RI line.
288
Chris Liechti256ea212015-08-29 23:57:00 +0200289 .. attribute:: cd
cliechtic1c37602009-07-21 01:34:57 +0000290
Chris Liechti256ea212015-08-29 23:57:00 +0200291 :getter: Get the state of the CD line
292 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000293
cliechtic1c37602009-07-21 01:34:57 +0000294 Return the state of the CD line
295
Matthew Westfa58a972016-07-08 14:27:04 -0700296 .. attribute:: is_open
Chris Liechtiaefbf032016-09-30 23:57:22 +0200297
Chris Liechti5e953c32016-09-28 18:18:25 +0200298 :getter: Get the state of the serial port, whether it's open.
299 :type: bool
cliechtif81362e2009-07-25 03:44:33 +0000300
cliechti25375b52010-07-21 23:27:13 +0000301 New values can be assigned to the following attributes (properties), the
302 port will be reconfigured, even if it's opened at that time:
cliechtic1c37602009-07-21 01:34:57 +0000303
cliechtiedcdbe42009-07-22 00:48:34 +0000304
305 .. attribute:: port
306
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100307 :type: str
308
cliechtiedcdbe42009-07-22 00:48:34 +0000309 Read or write port. When the port is already open, it will be closed
310 and reopened with the new setting.
311
312 .. attribute:: baudrate
313
Chris Liechti518b0d32015-08-30 02:20:39 +0200314 :getter: Get current baud rate
315 :setter: Set new baud rate
316 :type: int
317
cliechti6066f842009-07-24 00:05:45 +0000318 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000319
320 .. attribute:: bytesize
321
Chris Liechti518b0d32015-08-30 02:20:39 +0200322 :getter: Get current byte size
323 :setter: Set new byte size. Possible values:
324 :const:`FIVEBITS`, :const:`SIXBITS`, :const:`SEVENBITS`,
325 :const:`EIGHTBITS`
326 :type: int
327
cliechti6066f842009-07-24 00:05:45 +0000328 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000329
330 .. attribute:: parity
331
Chris Liechti518b0d32015-08-30 02:20:39 +0200332 :getter: Get current parity setting
333 :setter: Set new parity mode. Possible values:
334 :const:`PARITY_NONE`, :const:`PARITY_EVEN`, :const:`PARITY_ODD`
335 :const:`PARITY_MARK`, :const:`PARITY_SPACE`
336
cliechti6066f842009-07-24 00:05:45 +0000337 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000338
339 .. attribute:: stopbits
340
Chris Liechti518b0d32015-08-30 02:20:39 +0200341 :getter: Get current stop bit setting
342 :setter: Set new stop bit setting. Possible values:
343 :const:`STOPBITS_ONE`, :const:`STOPBITS_ONE_POINT_FIVE`,
344 :const:`STOPBITS_TWO`
345
cliechti6066f842009-07-24 00:05:45 +0000346 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000347
348 .. attribute:: timeout
349
Chris Liechti518b0d32015-08-30 02:20:39 +0200350 :getter: Get current read timeout setting
351 :setter: Set read timeout
352 :type: float (seconds)
353
cliechti6066f842009-07-24 00:05:45 +0000354 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000355
Chris Liechti518b0d32015-08-30 02:20:39 +0200356 .. attribute:: write_timeout
357
358 :getter: Get current write timeout setting
359 :setter: Set write timeout
360 :type: float (seconds)
cliechtiedcdbe42009-07-22 00:48:34 +0000361
cliechti6066f842009-07-24 00:05:45 +0000362 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000363
Chris Liechti518b0d32015-08-30 02:20:39 +0200364 .. versionchanged:: 3.0 renamed from ``writeTimeout``
365
366 .. attribute:: inter_byte_timeout
367
368 :getter: Get current inter byte timeout setting
369 :setter: Disable (``None``) or enable the inter byte timeout
370 :type: float or None
371
372 Read or write current inter byte timeout setting.
373
374 .. versionchanged:: 3.0 renamed from ``interCharTimeout``
375
cliechtiedcdbe42009-07-22 00:48:34 +0000376 .. attribute:: xonxoff
377
Chris Liechti518b0d32015-08-30 02:20:39 +0200378 :getter: Get current software flow control setting
379 :setter: Enable or disable software flow control
380 :type: bool
381
cliechti6066f842009-07-24 00:05:45 +0000382 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000383
384 .. attribute:: rtscts
385
Chris Liechti518b0d32015-08-30 02:20:39 +0200386 :getter: Get current hardware flow control setting
387 :setter: Enable or disable hardware flow control
388 :type: bool
389
cliechti6066f842009-07-24 00:05:45 +0000390 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000391
392 .. attribute:: dsrdtr
393
Chris Liechti518b0d32015-08-30 02:20:39 +0200394 :getter: Get current hardware flow control setting
395 :setter: Enable or disable hardware flow control
396 :type: bool
397
cliechti6066f842009-07-24 00:05:45 +0000398 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000399
Chris Liechtice621882015-08-06 19:29:31 +0200400 .. attribute:: rs485_mode
401
Chris Liechti518b0d32015-08-30 02:20:39 +0200402 :getter: Get the current RS485 settings
403 :setter: Disable (``None``) or enable the RS485 settings
404 :type: :class:`rs485.RS485Settings` or ``None``
405 :platform: Posix (Linux, limited set of hardware)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100406 :platform: Windows (only RTS on TX possible)
Chris Liechtice621882015-08-06 19:29:31 +0200407
408 Attribute to configure RS485 support. When set to an instance of
409 :class:`rs485.RS485Settings` and supported by OS, RTS will be active
410 when data is sent and inactive otherwise (for reception). The
411 :class:`rs485.RS485Settings` class provides additional settings
412 supported on some platforms.
413
414 .. versionadded:: 3.0
415
416
cliechtiedcdbe42009-07-22 00:48:34 +0000417 The following constants are also provided:
418
419 .. attribute:: BAUDRATES
420
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100421 A list of valid baud rates. The list may be incomplete, such that higher
422 and/or intermediate baud rates may also be supported by the device
423 (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000424
425 .. attribute:: BYTESIZES
426
cliechti25375b52010-07-21 23:27:13 +0000427 A list of valid byte sizes for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000428
429 .. attribute:: PARITIES
430
cliechti25375b52010-07-21 23:27:13 +0000431 A list of valid parities for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000432
433 .. attribute:: STOPBITS
434
cliechti25375b52010-07-21 23:27:13 +0000435 A list of valid stop bit widths for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000436
cliechti4a567a02009-07-27 22:09:31 +0000437
cliechti25375b52010-07-21 23:27:13 +0000438 The following methods are for compatibility with the :mod:`io` library.
cliechti4a567a02009-07-27 22:09:31 +0000439
440 .. method:: readable()
441
442 :return: True
cliechtif4566342009-08-04 00:07:19 +0000443
cliechti4a567a02009-07-27 22:09:31 +0000444 .. versionadded:: 2.5
445
446 .. method:: writable()
447
448 :return: True
cliechtif4566342009-08-04 00:07:19 +0000449
cliechti4a567a02009-07-27 22:09:31 +0000450 .. versionadded:: 2.5
451
452 .. method:: seekable()
453
454 :return: False
cliechtif4566342009-08-04 00:07:19 +0000455
cliechti4a567a02009-07-27 22:09:31 +0000456 .. versionadded:: 2.5
457
458 .. method:: readinto(b)
459
cliechti8611bf42009-08-03 00:34:03 +0000460 :param b: bytearray or array instance
cliechti4a567a02009-07-27 22:09:31 +0000461 :return: Number of byte read
462
cliechtid7e7ce22009-08-03 02:01:07 +0000463 Read up to len(b) bytes into :class:`bytearray` *b* and return the
464 number of bytes read.
cliechti4a567a02009-07-27 22:09:31 +0000465
466 .. versionadded:: 2.5
467
Chris Liechti22d39002018-05-08 03:39:20 +0200468 .. method:: readline(size=-1)
469
470 Provided via :meth:`io.IOBase.readline`
471
472 .. method:: readlines(hint=-1)
473
474 Provided via :meth:`io.IOBase.readlines`
475
476 .. method:: writelines(lines)
477
478 Provided via :meth:`io.IOBase.writelines`
479
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100480 The port settings can be read and written as dictionary. The following
481 keys are supported: ``write_timeout``, ``inter_byte_timeout``,
482 ``dsrdtr``, ``baudrate``, ``timeout``, ``parity``, ``bytesize``,
483 ``rtscts``, ``stopbits``, ``xonxoff``
cliechti25375b52010-07-21 23:27:13 +0000484
Chris Liechti256ea212015-08-29 23:57:00 +0200485 .. method:: get_settings()
cliechti4065dce2009-08-10 00:55:46 +0000486
487 :return: a dictionary with current port settings.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100488 :rtype: dict
cliechti4065dce2009-08-10 00:55:46 +0000489
490 Get a dictionary with port settings. This is useful to backup the
491 current settings so that a later point in time they can be restored
Chris Liechti518b0d32015-08-30 02:20:39 +0200492 using :meth:`apply_settings`.
cliechti4065dce2009-08-10 00:55:46 +0000493
Chris Liechti19688bf2016-05-06 23:48:36 +0200494 Note that the state of control lines (RTS/DTR) are not part of the
495 settings.
cliechti4065dce2009-08-10 00:55:46 +0000496
497 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100498 .. versionchanged:: 3.0 renamed from ``getSettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000499
Chris Liechti256ea212015-08-29 23:57:00 +0200500 .. method:: apply_settings(d)
cliechti4065dce2009-08-10 00:55:46 +0000501
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100502 :param dict d: a dictionary with port settings.
cliechti4065dce2009-08-10 00:55:46 +0000503
Chris Liechti518b0d32015-08-30 02:20:39 +0200504 Applies a dictionary that was created by :meth:`get_settings`. Only
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100505 changes are applied and when a key is missing, it means that the
506 setting stays unchanged.
cliechti4065dce2009-08-10 00:55:46 +0000507
508 Note that control lines (RTS/DTR) are not changed.
509
510 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100511 .. versionchanged:: 3.0 renamed from ``applySettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000512
Chris Liechtic9f89962016-08-12 21:04:37 +0200513
Chris Liechti9e205742016-10-31 22:37:13 +0100514 .. _context-manager:
515
Chris Liechtic9f89962016-08-12 21:04:37 +0200516 This class can be used as context manager. The serial port is closed when
517 the context is left.
518
519 .. method:: __enter__()
520
521 :returns: Serial instance
522
523 Returns the instance that was used in the ``with`` statement.
524
525 Example:
526
527 >>> with serial.serial_for_url(port) as s:
528 ... s.write(b'hello')
529
Chris Liechtifd70a552017-07-20 23:46:34 +0200530 The port is opened automatically:
Chris Liechtic9f89962016-08-12 21:04:37 +0200531
Chris Liechtifd70a552017-07-20 23:46:34 +0200532 >>> port = serial.Serial()
533 >>> port.port = '...'
534 >>> with port as s:
Chris Liechtic9f89962016-08-12 21:04:37 +0200535 ... s.write(b'hello')
536
Chris Liechtifd70a552017-07-20 23:46:34 +0200537 Which also means that ``with`` statements can be used repeatedly,
538 each time opening and closing the port.
539
540 .. versionchanged:: 3.4 the port is automatically opened
541
Chris Liechtic9f89962016-08-12 21:04:37 +0200542
543 .. method:: __exit__(exc_type, exc_val, exc_tb)
544
Chris Liechti4f988d42017-03-20 23:59:40 +0100545 Closes serial port (exceptions are not handled by ``__exit__``).
Chris Liechtic9f89962016-08-12 21:04:37 +0200546
547
cliechti25375b52010-07-21 23:27:13 +0000548 Platform specific methods.
549
cliechtic648da92011-12-29 01:17:51 +0000550 .. warning:: Programs using the following methods and attributes are not
551 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000552
553 .. method:: nonblocking()
554
Chris Liechtice621882015-08-06 19:29:31 +0200555 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000556
Chris Liechti59848422016-06-04 22:28:14 +0200557 .. deprecated:: 3.2
558 The serial port is already opened in this mode. This method is not
559 needed and going away.
560
cliechti25375b52010-07-21 23:27:13 +0000561
562 .. method:: fileno()
563
Chris Liechtice621882015-08-06 19:29:31 +0200564 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000565 :return: File descriptor.
566
567 Return file descriptor number for the port that is opened by this object.
568 It is useful when serial ports are used with :mod:`select`.
569
Chris Liechti518b0d32015-08-30 02:20:39 +0200570 .. method:: set_input_flow_control(enable)
cliechti25375b52010-07-21 23:27:13 +0000571
cliechti2f0f8a32011-12-28 22:10:00 +0000572 :platform: Posix
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100573 :param bool enable: Set flow control state.
cliechti25375b52010-07-21 23:27:13 +0000574
cliechti2f0f8a32011-12-28 22:10:00 +0000575 Manually control flow - when software flow control is enabled.
576
577 This will send XON (true) and XOFF (false) to the other device.
578
Chris Liechti518b0d32015-08-30 02:20:39 +0200579 .. versionadded:: 2.7 (Posix support added)
580 .. versionchanged:: 3.0 renamed from ``flowControlOut``
cliechti2f0f8a32011-12-28 22:10:00 +0000581
Chris Liechti518b0d32015-08-30 02:20:39 +0200582 .. method:: set_output_flow_control(enable)
cliechti2f0f8a32011-12-28 22:10:00 +0000583
Chris Liechti518b0d32015-08-30 02:20:39 +0200584 :platform: Posix (HW and SW flow control)
585 :platform: Windows (SW flow control only)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100586 :param bool enable: Set flow control state.
cliechti2f0f8a32011-12-28 22:10:00 +0000587
588 Manually control flow of outgoing data - when hardware or software flow
589 control is enabled.
590
591 Sending will be suspended when called with ``False`` and enabled when
592 called with ``True``.
593
Chris Liechti518b0d32015-08-30 02:20:39 +0200594 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
595 .. versionchanged:: 3.0 renamed from ``setXON``
Chris Liechti256ea212015-08-29 23:57:00 +0200596
Chris Liechti19688bf2016-05-06 23:48:36 +0200597 .. method:: cancel_read()
Chris Liechti256ea212015-08-29 23:57:00 +0200598
Chris Liechti9d893052016-05-18 22:03:29 +0200599 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200600 :platform: Windows
601
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100602 Cancel a pending read operation from another thread. A blocking
Chris Liechti9d893052016-05-18 22:03:29 +0200603 :meth:`read` call is aborted immediately. :meth:`read` will not report
604 any error but return all data received up to that point (similar to a
605 timeout).
606
607 On Posix a call to `cancel_read()` may cancel a future :meth:`read` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200608
609 .. versionadded:: 3.1
610
611 .. method:: cancel_write()
612
Chris Liechti9d893052016-05-18 22:03:29 +0200613 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200614 :platform: Windows
615
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100616 Cancel a pending write operation from another thread. The
Chris Liechti9d893052016-05-18 22:03:29 +0200617 :meth:`write` method will return immediately (no error indicated).
618 However the OS may still be sending from the buffer, a separate call to
619 :meth:`reset_output_buffer` may be needed.
620
621 On Posix a call to `cancel_write()` may cancel a future :meth:`write` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200622
623 .. versionadded:: 3.1
Chris Liechti518b0d32015-08-30 02:20:39 +0200624
Chris Liechti9a99cb22015-08-30 22:16:50 +0200625 .. note:: The following members are deprecated and will be removed in a
Chris Liechti518b0d32015-08-30 02:20:39 +0200626 future release.
Chris Liechti256ea212015-08-29 23:57:00 +0200627
628 .. attribute:: portstr
629
Chris Liechti518b0d32015-08-30 02:20:39 +0200630 .. deprecated:: 2.5 use :attr:`name` instead
Chris Liechti256ea212015-08-29 23:57:00 +0200631
632 .. method:: inWaiting()
633
Chris Liechti518b0d32015-08-30 02:20:39 +0200634 .. deprecated:: 3.0 see :attr:`in_waiting`
635
Matthew Westfa58a972016-07-08 14:27:04 -0700636 .. method:: isOpen()
637
Chris Liechtifd70a552017-07-20 23:46:34 +0200638 .. deprecated:: 3.0 see :attr:`is_open`
Matthew Westfa58a972016-07-08 14:27:04 -0700639
Chris Liechti518b0d32015-08-30 02:20:39 +0200640 .. attribute:: writeTimeout
641
642 .. deprecated:: 3.0 see :attr:`write_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200643
644 .. attribute:: interCharTimeout
645
Chris Liechti518b0d32015-08-30 02:20:39 +0200646 .. deprecated:: 3.0 see :attr:`inter_byte_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200647
648 .. method:: sendBreak(duration=0.25)
649
Chris Liechti518b0d32015-08-30 02:20:39 +0200650 .. deprecated:: 3.0 see :meth:`send_break`
Chris Liechti256ea212015-08-29 23:57:00 +0200651
652 .. method:: flushInput()
653
Chris Liechti518b0d32015-08-30 02:20:39 +0200654 .. deprecated:: 3.0 see :meth:`reset_input_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200655
656 .. method:: flushOutput()
657
Chris Liechti518b0d32015-08-30 02:20:39 +0200658 .. deprecated:: 3.0 see :meth:`reset_output_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200659
660 .. method:: setBreak(level=True)
661
Chris Liechti518b0d32015-08-30 02:20:39 +0200662 .. deprecated:: 3.0 see :attr:`break_condition`
Chris Liechti256ea212015-08-29 23:57:00 +0200663
664 .. method:: setRTS(level=True)
665
Chris Liechti518b0d32015-08-30 02:20:39 +0200666 .. deprecated:: 3.0 see :attr:`rts`
Chris Liechti256ea212015-08-29 23:57:00 +0200667
668 .. method:: setDTR(level=True)
669
Chris Liechti518b0d32015-08-30 02:20:39 +0200670 .. deprecated:: 3.0 see :attr:`dtr`
Chris Liechti256ea212015-08-29 23:57:00 +0200671
672 .. method:: getCTS()
673
Chris Liechti518b0d32015-08-30 02:20:39 +0200674 .. deprecated:: 3.0 see :attr:`cts`
Chris Liechti256ea212015-08-29 23:57:00 +0200675
676 .. method:: getDSR()
677
Chris Liechti518b0d32015-08-30 02:20:39 +0200678 .. deprecated:: 3.0 see :attr:`dsr`
Chris Liechti256ea212015-08-29 23:57:00 +0200679
680 .. method:: getRI()
681
Chris Liechti518b0d32015-08-30 02:20:39 +0200682 .. deprecated:: 3.0 see :attr:`ri`
Chris Liechti256ea212015-08-29 23:57:00 +0200683
684 .. method:: getCD()
685
Chris Liechti518b0d32015-08-30 02:20:39 +0200686 .. deprecated:: 3.0 see :attr:`cd`
Chris Liechti256ea212015-08-29 23:57:00 +0200687
688 .. method:: getSettingsDict()
689
Chris Liechti518b0d32015-08-30 02:20:39 +0200690 .. deprecated:: 3.0 see :meth:`get_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200691
692 .. method:: applySettingsDict(d)
693
Chris Liechti518b0d32015-08-30 02:20:39 +0200694 .. deprecated:: 3.0 see :meth:`apply_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200695
696 .. method:: outWaiting()
697
Chris Liechti518b0d32015-08-30 02:20:39 +0200698 .. deprecated:: 3.0 see :attr:`out_waiting`
Chris Liechti256ea212015-08-29 23:57:00 +0200699
700 .. method:: setXON(level=True)
701
Chris Liechti518b0d32015-08-30 02:20:39 +0200702 .. deprecated:: 3.0 see :meth:`set_output_flow_control`
Chris Liechti256ea212015-08-29 23:57:00 +0200703
704 .. method:: flowControlOut(enable)
705
Chris Liechti518b0d32015-08-30 02:20:39 +0200706 .. deprecated:: 3.0 see :meth:`set_input_flow_control`
cliechtif4566342009-08-04 00:07:19 +0000707
cliechtic648da92011-12-29 01:17:51 +0000708 .. attribute:: rtsToggle
709
710 :platform: Windows
711
712 Attribute to configure RTS toggle control setting. When enabled and
713 supported by OS, RTS will be active when data is available and inactive
714 if no data is available.
715
716 .. versionadded:: 2.6
Chris Liechti518b0d32015-08-30 02:20:39 +0200717 .. versionchanged:: 3.0 (removed, see :attr:`rs485_mode` instead)
cliechtic648da92011-12-29 01:17:51 +0000718
719
cliechtic56e41d2011-08-25 22:06:38 +0000720Implementation detail: some attributes and functions are provided by the
Chris Liechti22d39002018-05-08 03:39:20 +0200721class :class:`serial.SerialBase` which inherits from :class:`io.RawIOBase`
722and some by the platform specific class and others by the base class
723mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000724
Chris Liechti4f988d42017-03-20 23:59:40 +0100725
Chris Liechtice621882015-08-06 19:29:31 +0200726RS485 support
727-------------
728The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to
729enable RS485 specific support on some platforms. Currently Windows and Linux
730(only a small number of devices) are supported.
cliechti4a567a02009-07-27 22:09:31 +0000731
Chris Liechtice621882015-08-06 19:29:31 +0200732:attr:`Serial.rs485_mode` needs to be set to an instance of
733:class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature.
cliechti4a567a02009-07-27 22:09:31 +0000734
Chris Liechtice621882015-08-06 19:29:31 +0200735Usage::
cliechti4a567a02009-07-27 22:09:31 +0000736
Chris Liechti11538242016-09-06 22:20:09 +0200737 import serial
738 import serial.rs485
Chris Liechtice621882015-08-06 19:29:31 +0200739 ser = serial.Serial(...)
740 ser.rs485_mode = serial.rs485.RS485Settings(...)
741 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000742
Chris Liechtice621882015-08-06 19:29:31 +0200743There is a subclass :class:`rs485.RS485` available to emulate the RS485 support
Chris Liechti11538242016-09-06 22:20:09 +0200744on regular serial ports (``serial.rs485`` needs to be imported).
cliechti4a567a02009-07-27 22:09:31 +0000745
cliechti4a567a02009-07-27 22:09:31 +0000746
Chris Liechtice621882015-08-06 19:29:31 +0200747.. class:: rs485.RS485Settings
cliechti4a567a02009-07-27 22:09:31 +0000748
Chris Liechtice621882015-08-06 19:29:31 +0200749 A class that holds RS485 specific settings which are supported on
750 some platforms.
cliechti4a567a02009-07-27 22:09:31 +0000751
Chris Liechtice621882015-08-06 19:29:31 +0200752 .. versionadded:: 3.0
cliechti4a567a02009-07-27 22:09:31 +0000753
Chris Liechtice621882015-08-06 19:29:31 +0200754 .. 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 +0000755
Chris Liechtice621882015-08-06 19:29:31 +0200756 :param bool rts_level_for_tx:
757 RTS level for transmission
cliechti4a567a02009-07-27 22:09:31 +0000758
Chris Liechtice621882015-08-06 19:29:31 +0200759 :param bool rts_level_for_rx:
760 RTS level for reception
cliechti4a567a02009-07-27 22:09:31 +0000761
Chris Liechtice621882015-08-06 19:29:31 +0200762 :param bool loopback:
763 When set to ``True`` transmitted data is also received.
cliechti4a567a02009-07-27 22:09:31 +0000764
Chris Liechtice621882015-08-06 19:29:31 +0200765 :param float delay_before_tx:
766 Delay after setting RTS but before transmission starts
767
768 :param float delay_before_rx:
769 Delay after transmission ends and resetting RTS
770
771 .. attribute:: rts_level_for_tx
772
773 RTS level for transmission.
774
775 .. attribute:: rts_level_for_rx
776
777 RTS level for reception.
778
779 .. attribute:: loopback
780
781 When set to ``True`` transmitted data is also received.
782
783 .. attribute:: delay_before_tx
784
785 Delay after setting RTS but before transmission starts (seconds as float).
786
787 .. attribute:: delay_before_rx
788
789 Delay after transmission ends and resetting RTS (seconds as float).
cliechti4a567a02009-07-27 22:09:31 +0000790
791
Chris Liechtice621882015-08-06 19:29:31 +0200792.. class:: rs485.RS485
cliechti4a567a02009-07-27 22:09:31 +0000793
Chris Liechtice621882015-08-06 19:29:31 +0200794 A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS
795 according to the RS485 settings.
cliechti4a567a02009-07-27 22:09:31 +0000796
Chris Liechti518b0d32015-08-30 02:20:39 +0200797 Usage::
798
799 ser = serial.rs485.RS485(...)
800 ser.rs485_mode = serial.rs485.RS485Settings(...)
801 ser.write(b'hello')
802
Chris Liechtice621882015-08-06 19:29:31 +0200803 .. warning:: This may work unreliably on some serial ports (control signals not
804 synchronized or delayed compared to data). Using delays may be unreliable
805 (varying times, larger than expected) as the OS may not support very fine
806 grained delays (no smaller than in the order of tens of milliseconds).
cliechti4a567a02009-07-27 22:09:31 +0000807
Chris Liechtice621882015-08-06 19:29:31 +0200808 .. note:: Some implementations support this natively in the class
809 :class:`Serial`. Better performance can be expected when the native version
810 is used.
cliechti4a567a02009-07-27 22:09:31 +0000811
Chris Liechtice621882015-08-06 19:29:31 +0200812 .. note:: The loopback property is ignored by this implementation. The actual
813 behavior depends on the used hardware.
cliechti4a567a02009-07-27 22:09:31 +0000814
cliechti4a567a02009-07-27 22:09:31 +0000815
cliechtie214ff82010-07-21 15:48:47 +0000816
cliechti7aed8332009-08-05 14:19:31 +0000817:rfc:`2217` Network ports
818-------------------------
819
820.. warning:: This implementation is currently in an experimental state. Use
821 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000822
cliechtiec4ac1b2009-08-02 00:47:21 +0000823.. class:: rfc2217.Serial
824
Chris Liechti2a1befc2015-10-21 17:37:08 +0200825 This implements a :rfc:`2217` compatible client. Port names are :ref:`URL
826 <URLs>` in the form: ``rfc2217://<host>:<port>[?<option>[&<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000827
cliechtiec4ac1b2009-08-02 00:47:21 +0000828 This class API is compatible to :class:`Serial` with a few exceptions:
829
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100830 - ``write_timeout`` is not implemented
cliechtiec4ac1b2009-08-02 00:47:21 +0000831 - The current implementation starts a thread that keeps reading from the
832 (internal) socket. The thread is managed automatically by the
833 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
834 However it may be a problem for user applications that like to use select
835 instead of threads.
836
837 Due to the nature of the network and protocol involved there are a few
838 extra points to keep in mind:
839
840 - All operations have an additional latency time.
841 - Setting control lines (RTS/CTS) needs more time.
842 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
843 cache is updated depends entirely on the server. The server itself may
844 implement a polling at a certain rate and quick changes may be invisible.
845 - The network layer also has buffers. This means that :meth:`flush`,
Chris Liechti518b0d32015-08-30 02:20:39 +0200846 :meth:`reset_input_buffer` and :meth:`reset_output_buffer` may work with
847 additional delay. Likewise :attr:`in_waiting` returns the size of the
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100848 data arrived at the objects internal buffer and excludes any bytes in the
Chris Liechti518b0d32015-08-30 02:20:39 +0200849 network buffers or any server side buffer.
cliechtiec4ac1b2009-08-02 00:47:21 +0000850 - Closing and immediately reopening the same port may fail due to time
851 needed by the server to get ready again.
852
853 Not implemented yet / Possible problems with the implementation:
854
cliechti8611bf42009-08-03 00:34:03 +0000855 - :rfc:`2217` flow control between client and server (objects internal
856 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000857 - No authentication support (servers may not prompt for a password etc.)
858 - No encryption.
859
860 Due to lack of authentication and encryption it is not suitable to use this
861 client for connections across the internet and should only be used in
862 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000863
cliechti7c640ed2009-08-02 00:54:51 +0000864 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000865
cliechti7aed8332009-08-05 14:19:31 +0000866
867.. class:: rfc2217.PortManager
868
869 This class provides helper functions for implementing :rfc:`2217`
870 compatible servers.
871
Chris Liechti518b0d32015-08-30 02:20:39 +0200872 Basically, it implements everything needed for the :rfc:`2217` protocol.
cliechti7aed8332009-08-05 14:19:31 +0000873 It just does not open sockets and read/write to serial ports (though it
874 changes other port settings). The user of this class must take care of the
875 data transmission itself. The reason for that is, that this way, this class
876 supports all programming models such as threads and select.
877
878 Usage examples can be found in the examples where two TCP/IP - serial
879 converters are shown, one using threads (the single port server) and an
880 other using select (the multi port server).
881
882 .. note:: Each new client connection must create a new instance as this
883 object (and the :rfc:`2217` protocol) has internal state.
884
885 .. method:: __init__(serial_port, connection, debug_output=False)
886
887 :param serial_port: a :class:`Serial` instance that is managed.
888 :param connection: an object implementing :meth:`write`, used to write
889 to the network.
cliechti86844e82009-08-12 00:05:33 +0000890 :param debug_output: enables debug messages: a :class:`logging.Logger`
891 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000892
893 Initializes the Manager and starts negotiating with client in Telnet
894 and :rfc:`2217` protocol. The negotiation starts immediately so that
895 the class should be instantiated in the moment the client connects.
896
897 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000898 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000899 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
900 the :meth:`filter` method.
901
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100902 The *connection* object must implement a :meth:`write` function.
cliechti7aed8332009-08-05 14:19:31 +0000903 This function must ensure that *data* is written at once (no user data
904 mixed in, i.e. it must be thread-safe). All data must be sent in its
905 raw form (:meth:`escape` must not be used) as it is used to send Telnet
906 and :rfc:`2217` control commands.
907
cliechti86844e82009-08-12 00:05:33 +0000908 For diagnostics of the connection or the implementation, *debug_output*
909 can be set to an instance of a :class:`logging.Logger` (e.g.
910 ``logging.getLogger('rfc2217.server')``). The caller should configure
911 the logger using ``setLevel`` for the desired detail level of the logs.
912
cliechti7aed8332009-08-05 14:19:31 +0000913 .. method:: escape(data)
914
915 :param data: data to be sent over the network.
916 :return: data, escaped for Telnet/:rfc:`2217`
917
918 A generator that escapes all data to be compatible with :rfc:`2217`.
919 Implementors of servers should use this function to process all data
920 sent over the network.
921
922 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000923 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000924
925 .. method:: filter(data)
926
927 :param data: data read from the network, including Telnet and
928 :rfc:`2217` controls.
929 :return: data, free from Telnet and :rfc:`2217` controls.
930
931 A generator that filters and processes all data related to :rfc:`2217`.
932 Implementors of servers should use this function to process all data
933 received from the network.
934
935 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000936 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000937
cliechti7cb78e82009-08-05 15:47:57 +0000938 .. method:: check_modem_lines(force_notification=False)
939
940 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000941
942 This function needs to be called periodically (e.g. every second) when
943 the server wants to send NOTIFY_MODEMSTATE messages. This is required
944 to support the client for reading CTS/DSR/RI/CD status lines.
945
946 The function reads the status line and issues the notifications
947 automatically.
948
949 .. versionadded:: 2.5
950
cliechtiec4ac1b2009-08-02 00:47:21 +0000951.. seealso::
952
953 :rfc:`2217` - Telnet Com Port Control Option
954
955
cliechtic1c37602009-07-21 01:34:57 +0000956Exceptions
957==========
958
959.. exception:: SerialException
960
961 Base class for serial port exceptions.
962
cliechti4a567a02009-07-27 22:09:31 +0000963 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000964 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000965
cliechtic1c37602009-07-21 01:34:57 +0000966.. exception:: SerialTimeoutException
967
968 Exception that is raised on write timeouts.
969
970
971Constants
972=========
973
cliechti87686242009-08-18 00:58:31 +0000974*Parity*
975
cliechtic1c37602009-07-21 01:34:57 +0000976.. data:: PARITY_NONE
977.. data:: PARITY_EVEN
978.. data:: PARITY_ODD
979.. data:: PARITY_MARK
980.. data:: PARITY_SPACE
981
cliechti87686242009-08-18 00:58:31 +0000982*Stop bits*
983
cliechtic1c37602009-07-21 01:34:57 +0000984.. data:: STOPBITS_ONE
985.. data:: STOPBITS_ONE_POINT_FIVE
986.. data:: STOPBITS_TWO
987
cliechti06281be2011-08-25 23:08:29 +0000988Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +0000989bits.
990
cliechti87686242009-08-18 00:58:31 +0000991*Byte size*
992
cliechtic1c37602009-07-21 01:34:57 +0000993.. data:: FIVEBITS
994.. data:: SIXBITS
995.. data:: SEVENBITS
996.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000997
cliechti87686242009-08-18 00:58:31 +0000998
999*Others*
1000
cliechti8611bf42009-08-03 00:34:03 +00001001Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +00001002software flow control:
cliechti6066f842009-07-24 00:05:45 +00001003
cliechti5b7d16a2009-07-21 21:53:59 +00001004.. data:: XON
1005.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +00001006
cliechti4a567a02009-07-27 22:09:31 +00001007Module version:
cliechtif81362e2009-07-25 03:44:33 +00001008
1009.. data:: VERSION
1010
Chris Liechti518b0d32015-08-30 02:20:39 +02001011 A string indicating the pySerial version, such as ``3.0``.
cliechtif81362e2009-07-25 03:44:33 +00001012
cliechti87686242009-08-18 00:58:31 +00001013 .. versionadded:: 2.3
1014
cliechti44eb98f2011-03-21 21:41:10 +00001015
cliechtie542b362011-03-18 00:49:16 +00001016Module functions and attributes
1017===============================
cliechtif81362e2009-07-25 03:44:33 +00001018
1019.. function:: device(number)
1020
Chris Liechti518b0d32015-08-30 02:20:39 +02001021 .. versionchanged:: 3.0 removed, use ``serial.tools.list_ports`` instead
1022
cliechti7c640ed2009-08-02 00:54:51 +00001023
cliechtie3ab3532009-08-05 12:40:38 +00001024.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +00001025
cliechti86844e82009-08-12 00:05:33 +00001026 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +00001027 :param do_not_open: When set to true, the serial port is not opened.
1028 :return: an instance of :class:`Serial` or a compatible object.
1029
1030 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +00001031 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +00001032 to support both, local ports and remote ports. There is also support
Chris Liechti589c92a2015-09-04 23:04:34 +02001033 for other types, see :ref:`URL <URLs>` section.
cliechti7c640ed2009-08-02 00:54:51 +00001034
cliechtid7e7ce22009-08-03 02:01:07 +00001035 The port is not opened when a keyword parameter called *do_not_open* is
1036 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +00001037
1038 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +00001039
cliechti87686242009-08-18 00:58:31 +00001040
cliechtie542b362011-03-18 00:49:16 +00001041.. attribute:: protocol_handler_packages
1042
1043 This attribute is a list of package names (strings) that is searched for
1044 protocol handlers.
1045
1046 e.g. we want to support a URL ``foobar://``. A module
1047 ``my_handlers.protocol_foobar`` is provided by the user::
1048
1049 serial.protocol_handler_packages.append("my_handlers")
1050 s = serial.serial_for_url("foobar://")
1051
1052 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
1053 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
1054 ``PACKAGE`` from this list.
1055
1056 .. versionadded:: 2.6
1057
1058
cliechti25375b52010-07-21 23:27:13 +00001059.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +00001060
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001061 :param sequence: bytes, bytearray or memoryview
cliechti25375b52010-07-21 23:27:13 +00001062 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +00001063
cliechtie542b362011-03-18 00:49:16 +00001064 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +00001065 compatible to Python 2.x and 3.x.
1066
cliechtie542b362011-03-18 00:49:16 +00001067 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +00001068 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +00001069 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +00001070
1071 This function is used internally and in the unit tests.
1072
cliechtie542b362011-03-18 00:49:16 +00001073 .. versionadded:: 2.5
1074
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001075.. function:: iterbytes(sequence)
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001076
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001077 :param sequence: bytes, bytearray or memoryview
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001078 :returns: a generator that yields bytes
1079
1080 Some versions of Python (3.x) would return integers instead of bytes when
Chris Liechti4f988d42017-03-20 23:59:40 +01001081 looping over an instance of ``bytes``. This helper function ensures that
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001082 bytes are returned.
1083
1084 .. versionadded:: 3.0
1085
cliechtie214ff82010-07-21 15:48:47 +00001086
Chris Liechti811bf382015-10-10 00:54:47 +02001087Threading
1088=========
1089
1090.. module:: serial.threaded
1091.. versionadded:: 3.0
1092
1093.. warning:: This implementation is currently in an experimental state. Use
1094 at your own risk.
1095
1096This module provides classes to simplify working with threads and protocols.
1097
1098.. class:: Protocol
1099
Chris Liechti5665d572015-10-13 23:50:01 +02001100 Protocol as used by the :class:`ReaderThread`. This base class provides empty
Chris Liechti811bf382015-10-10 00:54:47 +02001101 implementations of all methods.
1102
1103
1104 .. method:: connection_made(transport)
1105
1106 :param transport: instance used to write to serial port.
1107
1108 Called when reader thread is started.
1109
1110 .. method:: data_received(data)
1111
Chris Liechtif8a09192015-12-20 23:36:38 +01001112 :param bytes data: received bytes
Chris Liechti811bf382015-10-10 00:54:47 +02001113
1114 Called with snippets received from the serial port.
1115
1116 .. method:: connection_lost(exc)
1117
1118 :param exc: Exception if connection was terminated by error else ``None``
1119
1120 Called when the serial port is closed or the reader loop terminated
1121 otherwise.
1122
1123.. class:: Packetizer(Protocol)
1124
1125 Read binary packets from serial port. Packets are expected to be terminated
1126 with a ``TERMINATOR`` byte (null byte by default).
1127
1128 The class also keeps track of the transport.
1129
1130 .. attribute:: TERMINATOR = b'\\0'
1131
1132 .. method:: __init__()
1133
1134 .. method:: connection_made(transport)
1135
Chris Liechtif8a09192015-12-20 23:36:38 +01001136 Stores transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001137
1138 .. method:: connection_lost(exc)
1139
Chris Liechtif8a09192015-12-20 23:36:38 +01001140 Forgets transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001141
1142 .. method:: data_received(data)
1143
Chris Liechtif8a09192015-12-20 23:36:38 +01001144 :param bytes data: partial received data
1145
1146 Buffer received data and search for :attr:`TERMINATOR`, when found,
Chris Liechti811bf382015-10-10 00:54:47 +02001147 call :meth:`handle_packet`.
1148
1149 .. method:: handle_packet(packet)
1150
Chris Liechti1c4c5992016-01-18 20:46:00 +01001151 :param bytes packet: a packet as defined by ``TERMINATOR``
1152
Chris Liechti811bf382015-10-10 00:54:47 +02001153 Process packets - to be overridden by subclassing.
1154
1155
1156.. class:: LineReader(Packetizer)
1157
1158 Read and write (Unicode) lines from/to serial port.
1159 The encoding is applied.
1160
1161
1162 .. attribute:: TERMINATOR = b'\\r\\n'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001163
1164 Line ending.
1165
Chris Liechti811bf382015-10-10 00:54:47 +02001166 .. attribute:: ENCODING = 'utf-8'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001167
1168 Encoding of the send and received data.
1169
Chris Liechti811bf382015-10-10 00:54:47 +02001170 .. attribute:: UNICODE_HANDLING = 'replace'
1171
Chris Liechti1c4c5992016-01-18 20:46:00 +01001172 Unicode error handly policy.
1173
Chris Liechti811bf382015-10-10 00:54:47 +02001174 .. method:: handle_packet(packet)
1175
Chris Liechti1c4c5992016-01-18 20:46:00 +01001176 :param bytes packet: a packet as defined by ``TERMINATOR``
1177
1178 In this case it will be a line, calls :meth:`handle_line` after applying
1179 the :attr:`ENCODING`.
1180
Chris Liechti811bf382015-10-10 00:54:47 +02001181 .. method:: handle_line(line)
1182
Chris Liechtif8a09192015-12-20 23:36:38 +01001183 :param str line: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001184
1185 Process one line - to be overridden by subclassing.
1186
1187 .. method:: write_line(text)
1188
Chris Liechtif8a09192015-12-20 23:36:38 +01001189 :param str text: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001190
Chris Liechtif8a09192015-12-20 23:36:38 +01001191 Write *text* to the transport. *text* is expected to be a Unicode
1192 string and the encoding is applied before sending and also the
1193 :attr:`TERMINATOR` (new line) is appended.
Chris Liechti811bf382015-10-10 00:54:47 +02001194
1195
Chris Liechti5665d572015-10-13 23:50:01 +02001196.. class:: ReaderThread(threading.Thread)
Chris Liechti811bf382015-10-10 00:54:47 +02001197
1198 Implement a serial port read loop and dispatch to a Protocol instance (like
1199 the :class:`asyncio.Protocol`) but do it with threads.
1200
Chris Liechtif8a09192015-12-20 23:36:38 +01001201 Calls to :meth:`close` will close the serial port but it is also possible
1202 to just :meth:`stop` this thread and continue to use the serial port
1203 instance otherwise.
Chris Liechti811bf382015-10-10 00:54:47 +02001204
1205 .. method:: __init__(serial_instance, protocol_factory)
1206
1207 :param serial_instance: serial port instance (opened) to be used.
1208 :param protocol_factory: a callable that returns a Protocol instance
1209
1210 Initialize thread.
1211
Chris Liechtif8a09192015-12-20 23:36:38 +01001212 Note that the ``serial_instance`` 's timeout is set to one second!
Chris Liechti811bf382015-10-10 00:54:47 +02001213 Other settings are not changed.
1214
1215 .. method:: stop()
1216
1217 Stop the reader thread.
1218
1219 .. method:: run()
1220
1221 The actual reader loop driven by the thread. It calls
1222 :meth:`Protocol.connection_made`, reads from the serial port calling
Chris Liechtif8a09192015-12-20 23:36:38 +01001223 :meth:`Protocol.data_received` and finally calls :meth:`Protocol.connection_lost`
Chris Liechti811bf382015-10-10 00:54:47 +02001224 when :meth:`close` is called or an error occurs.
1225
1226 .. method:: write(data)
1227
Chris Liechtif8a09192015-12-20 23:36:38 +01001228 :param bytes data: data to write
1229
Chris Liechti811bf382015-10-10 00:54:47 +02001230 Thread safe writing (uses lock).
1231
1232 .. method:: close()
1233
1234 Close the serial port and exit reader thread, calls :meth:`stop` (uses lock).
1235
1236 .. method:: connect()
1237
1238 Wait until connection is set up and return the transport and protocol
1239 instances.
1240
1241
1242 This class can be used as context manager, in this case it starts
1243 the thread and connects automatically. The serial port is closed
1244 when the context is left.
1245
1246 .. method:: __enter__()
1247
1248 :returns: protocol
1249
1250 Connect and return protocol instance.
1251
1252 .. method:: __exit__(exc_type, exc_val, exc_tb)
1253
1254 Closes serial port.
1255
Chris Liechtia1436b12015-10-12 23:19:04 +02001256Example::
1257
1258 class PrintLines(LineReader):
1259 def connection_made(self, transport):
1260 super(PrintLines, self).connection_made(transport)
1261 sys.stdout.write('port opened\n')
1262 self.write_line('hello world')
1263
1264 def handle_line(self, data):
1265 sys.stdout.write('line received: {}\n'.format(repr(data)))
1266
1267 def connection_lost(self, exc):
1268 if exc:
1269 traceback.print_exc(exc)
1270 sys.stdout.write('port closed\n')
1271
1272 ser = serial.serial_for_url('loop://', baudrate=115200, timeout=1)
Chris Liechti5665d572015-10-13 23:50:01 +02001273 with ReaderThread(ser, PrintLines) as protocol:
Chris Liechtia1436b12015-10-12 23:19:04 +02001274 protocol.write_line('hello')
1275 time.sleep(2)
1276
Chris Liechti811bf382015-10-10 00:54:47 +02001277
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001278asyncio
1279=======
1280
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001281``asyncio`` was introduced with Python 3.4. Experimental support for pySerial
1282is provided via a separate distribution `pyserial-asyncio`_.
Chris Liechti589c92a2015-09-04 23:04:34 +02001283
Jakub Wilk772a6fa2016-12-20 21:59:27 +01001284It is currently under development, see:
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001285
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001286- http://pyserial-asyncio.readthedocs.io/
1287- https://github.com/pyserial/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001288
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001289.. _`pyserial-asyncio`: https://pypi.python.org/pypi/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001290