blob: 76177ddd455126ea19cd7637c1f2af4d82a2b977 [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:
Chris Liechtiba3f6e72019-02-08 01:22:50 +010039 Set a read timeout value in seconds.
cliechtic1c37602009-07-21 01:34:57 +000040
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:
Chris Liechtiba3f6e72019-02-08 01:22:50 +010051 Set a write timeout value in seconds.
cliechti07709e42010-05-21 00:30:09 +000052
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
David Pattersondaaf33e2018-06-16 21:13:15 -0400160 .. method:: read_until(expected=LF, size=None)
161
162 :param expected: The byte string to search for.
163 :param size: Number of bytes to read.
164 :return: Bytes read from the port.
165 :rtype: bytes
166
David Patterson997ea882018-06-20 17:56:26 -0400167 Read until an expected sequence is found ('\\n' by default), the size
David Pattersondaaf33e2018-06-16 21:13:15 -0400168 is exceeded or until timeout occurs. If a timeout is set it may
169 return less characters as requested. With no timeout it will block
170 until the requested number of bytes is read.
171
172 .. versionchanged:: 2.5
173 Returns an instance of :class:`bytes` when available (Python 2.6
174 and newer) and :class:`str` otherwise.
175
cliechtibb5c3c52009-07-23 02:29:27 +0000176 .. method:: write(data)
177
178 :param data: Data to send.
cliechti4a567a02009-07-27 22:09:31 +0000179 :return: Number of bytes written.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100180 :rtype: int
cliechti6066f842009-07-24 00:05:45 +0000181 :exception SerialTimeoutException:
182 In case a write timeout is configured for the port and the time is
183 exceeded.
184
Chris Liechti9ad044a2015-12-17 19:43:59 +0100185 Write the bytes *data* to the port. This should be of type ``bytes``
186 (or compatible such as ``bytearray`` or ``memoryview``). Unicode
187 strings must be encoded (e.g. ``'hello'.encode('utf-8'``).
cliechtic1c37602009-07-21 01:34:57 +0000188
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100189 .. versionchanged:: 2.5
cliechtiddd78132009-07-28 01:13:28 +0000190 Accepts instances of :class:`bytes` and :class:`bytearray` when
cliechti8611bf42009-08-03 00:34:03 +0000191 available (Python 2.6 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000192
cliechti9a1c6952009-10-20 22:18:28 +0000193 .. versionchanged:: 2.5
194 Write returned ``None`` in previous versions.
195
Chris Liechti518b0d32015-08-30 02:20:39 +0200196 .. method:: flush()
197
198 Flush of file like objects. In this case, wait until all data is
199 written.
200
Chris Liechti256ea212015-08-29 23:57:00 +0200201 .. attribute:: in_waiting
cliechti4a567a02009-07-27 22:09:31 +0000202
Chris Liechti256ea212015-08-29 23:57:00 +0200203 :getter: Get the number of bytes in the input buffer
204 :type: int
205
206 Return the number of bytes in the receive buffer.
cliechti4a567a02009-07-27 22:09:31 +0000207
Chris Liechti518b0d32015-08-30 02:20:39 +0200208 .. versionchanged:: 3.0 changed to property from ``inWaiting()``
cliechtic1c37602009-07-21 01:34:57 +0000209
Chris Liechti518b0d32015-08-30 02:20:39 +0200210 .. attribute:: out_waiting
211
212 :getter: Get the number of bytes in the output buffer
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100213 :type: int
Chris Liechti518b0d32015-08-30 02:20:39 +0200214 :platform: Posix
215 :platform: Windows
216
217 Return the number of bytes in the output buffer.
218
219 .. versionchanged:: 2.7 (Posix support added)
220 .. versionchanged:: 3.0 changed to property from ``outWaiting()``
cliechtic1c37602009-07-21 01:34:57 +0000221
Chris Liechti256ea212015-08-29 23:57:00 +0200222 .. method:: reset_input_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000223
Chris Liechti4f988d42017-03-20 23:59:40 +0100224 Flush input buffer, discarding all its contents.
cliechtic1c37602009-07-21 01:34:57 +0000225
Chris Liechti518b0d32015-08-30 02:20:39 +0200226 .. versionchanged:: 3.0 renamed from ``flushInput()``
227
Chris Liechti256ea212015-08-29 23:57:00 +0200228 .. method:: reset_output_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000229
230 Clear output buffer, aborting the current output and
231 discarding all that is in the buffer.
232
Chris Liechti48e40e92016-05-24 00:09:12 +0200233 Note, for some USB serial adapters, this may only flush the buffer of
234 the OS and not all the data that may be present in the USB part.
235
Chris Liechti518b0d32015-08-30 02:20:39 +0200236 .. versionchanged:: 3.0 renamed from ``flushOutput()``
237
Chris Liechti256ea212015-08-29 23:57:00 +0200238 .. method:: send_break(duration=0.25)
cliechtic1c37602009-07-21 01:34:57 +0000239
Chris Liechtiba3f6e72019-02-08 01:22:50 +0100240 :param float duration: Time in seconds, to activate the BREAK condition.
cliechtibb5c3c52009-07-23 02:29:27 +0000241
cliechtic1c37602009-07-21 01:34:57 +0000242 Send break condition. Timed, returns to idle state after given
243 duration.
244
cliechtic1c37602009-07-21 01:34:57 +0000245
Chris Liechti256ea212015-08-29 23:57:00 +0200246 .. attribute:: break_condition
cliechtibb5c3c52009-07-23 02:29:27 +0000247
Chris Liechti256ea212015-08-29 23:57:00 +0200248 :getter: Get the current BREAK state
249 :setter: Control the BREAK state
250 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000251
Chris Liechti256ea212015-08-29 23:57:00 +0200252 When set to ``True`` activate BREAK condition, else disable.
253 Controls TXD. When active, no transmitting is possible.
cliechtic1c37602009-07-21 01:34:57 +0000254
Chris Liechti256ea212015-08-29 23:57:00 +0200255 .. attribute:: rts
cliechtibb5c3c52009-07-23 02:29:27 +0000256
Chris Liechti256ea212015-08-29 23:57:00 +0200257 :setter: Set the state of the RTS line
258 :getter: Return the state of the RTS line
259 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000260
Chris Liechti256ea212015-08-29 23:57:00 +0200261 Set RTS line to specified logic level. It is possible to assign this
Connor Weeksc9661f72018-04-06 09:49:15 -0600262 value before opening the serial port, then the value is applied upon
Chris Liechti5e953c32016-09-28 18:18:25 +0200263 :meth:`open` (with restrictions, see :meth:`open`).
cliechtic1c37602009-07-21 01:34:57 +0000264
Chris Liechti256ea212015-08-29 23:57:00 +0200265 .. attribute:: dtr
cliechtibb5c3c52009-07-23 02:29:27 +0000266
Chris Liechti256ea212015-08-29 23:57:00 +0200267 :setter: Set the state of the DTR line
268 :getter: Return the state of the DTR line
269 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000270
Chris Liechti256ea212015-08-29 23:57:00 +0200271 Set DTR line to specified logic level. It is possible to assign this
Connor Weeksc9661f72018-04-06 09:49:15 -0600272 value before opening the serial port, then the value is applied upon
Chris Liechti5e953c32016-09-28 18:18:25 +0200273 :meth:`open` (with restrictions, see :meth:`open`).
cliechtic1c37602009-07-21 01:34:57 +0000274
Chris Liechti518b0d32015-08-30 02:20:39 +0200275 Read-only attributes:
276
277 .. attribute:: name
278
279 :getter: Device name.
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100280 :type: str
Chris Liechti518b0d32015-08-30 02:20:39 +0200281
282 .. versionadded:: 2.5
283
Chris Liechti256ea212015-08-29 23:57:00 +0200284 .. attribute:: cts
285
286 :getter: Get the state of the CTS line
287 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000288
cliechtic1c37602009-07-21 01:34:57 +0000289 Return the state of the CTS line.
290
Chris Liechti256ea212015-08-29 23:57:00 +0200291 .. attribute:: dsr
cliechtic1c37602009-07-21 01:34:57 +0000292
Chris Liechti256ea212015-08-29 23:57:00 +0200293 :getter: Get the state of the DSR line
294 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000295
cliechtic1c37602009-07-21 01:34:57 +0000296 Return the state of the DSR line.
297
Chris Liechti256ea212015-08-29 23:57:00 +0200298 .. attribute:: ri
cliechtic1c37602009-07-21 01:34:57 +0000299
Chris Liechti256ea212015-08-29 23:57:00 +0200300 :getter: Get the state of the RI line
301 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000302
cliechtic1c37602009-07-21 01:34:57 +0000303 Return the state of the RI line.
304
Chris Liechti256ea212015-08-29 23:57:00 +0200305 .. attribute:: cd
cliechtic1c37602009-07-21 01:34:57 +0000306
Chris Liechti256ea212015-08-29 23:57:00 +0200307 :getter: Get the state of the CD line
308 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000309
cliechtic1c37602009-07-21 01:34:57 +0000310 Return the state of the CD line
311
Matthew Westfa58a972016-07-08 14:27:04 -0700312 .. attribute:: is_open
Chris Liechtiaefbf032016-09-30 23:57:22 +0200313
Chris Liechti5e953c32016-09-28 18:18:25 +0200314 :getter: Get the state of the serial port, whether it's open.
315 :type: bool
cliechtif81362e2009-07-25 03:44:33 +0000316
cliechti25375b52010-07-21 23:27:13 +0000317 New values can be assigned to the following attributes (properties), the
318 port will be reconfigured, even if it's opened at that time:
cliechtic1c37602009-07-21 01:34:57 +0000319
cliechtiedcdbe42009-07-22 00:48:34 +0000320
321 .. attribute:: port
322
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100323 :type: str
324
cliechtiedcdbe42009-07-22 00:48:34 +0000325 Read or write port. When the port is already open, it will be closed
326 and reopened with the new setting.
327
328 .. attribute:: baudrate
329
Chris Liechti518b0d32015-08-30 02:20:39 +0200330 :getter: Get current baud rate
331 :setter: Set new baud rate
332 :type: int
333
cliechti6066f842009-07-24 00:05:45 +0000334 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000335
336 .. attribute:: bytesize
337
Chris Liechti518b0d32015-08-30 02:20:39 +0200338 :getter: Get current byte size
339 :setter: Set new byte size. Possible values:
340 :const:`FIVEBITS`, :const:`SIXBITS`, :const:`SEVENBITS`,
341 :const:`EIGHTBITS`
342 :type: int
343
cliechti6066f842009-07-24 00:05:45 +0000344 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000345
346 .. attribute:: parity
347
Chris Liechti518b0d32015-08-30 02:20:39 +0200348 :getter: Get current parity setting
349 :setter: Set new parity mode. Possible values:
350 :const:`PARITY_NONE`, :const:`PARITY_EVEN`, :const:`PARITY_ODD`
351 :const:`PARITY_MARK`, :const:`PARITY_SPACE`
352
cliechti6066f842009-07-24 00:05:45 +0000353 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000354
355 .. attribute:: stopbits
356
Chris Liechti518b0d32015-08-30 02:20:39 +0200357 :getter: Get current stop bit setting
358 :setter: Set new stop bit setting. Possible values:
359 :const:`STOPBITS_ONE`, :const:`STOPBITS_ONE_POINT_FIVE`,
360 :const:`STOPBITS_TWO`
361
cliechti6066f842009-07-24 00:05:45 +0000362 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000363
364 .. attribute:: timeout
365
Chris Liechti518b0d32015-08-30 02:20:39 +0200366 :getter: Get current read timeout setting
367 :setter: Set read timeout
368 :type: float (seconds)
369
cliechti6066f842009-07-24 00:05:45 +0000370 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000371
Chris Liechti518b0d32015-08-30 02:20:39 +0200372 .. attribute:: write_timeout
373
374 :getter: Get current write timeout setting
375 :setter: Set write timeout
376 :type: float (seconds)
cliechtiedcdbe42009-07-22 00:48:34 +0000377
cliechti6066f842009-07-24 00:05:45 +0000378 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000379
Chris Liechti518b0d32015-08-30 02:20:39 +0200380 .. versionchanged:: 3.0 renamed from ``writeTimeout``
381
382 .. attribute:: inter_byte_timeout
383
384 :getter: Get current inter byte timeout setting
385 :setter: Disable (``None``) or enable the inter byte timeout
386 :type: float or None
387
388 Read or write current inter byte timeout setting.
389
390 .. versionchanged:: 3.0 renamed from ``interCharTimeout``
391
cliechtiedcdbe42009-07-22 00:48:34 +0000392 .. attribute:: xonxoff
393
Chris Liechti518b0d32015-08-30 02:20:39 +0200394 :getter: Get current software flow control setting
395 :setter: Enable or disable software flow control
396 :type: bool
397
cliechti6066f842009-07-24 00:05:45 +0000398 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000399
400 .. attribute:: rtscts
401
Chris Liechti518b0d32015-08-30 02:20:39 +0200402 :getter: Get current hardware flow control setting
403 :setter: Enable or disable hardware flow control
404 :type: bool
405
cliechti6066f842009-07-24 00:05:45 +0000406 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000407
408 .. attribute:: dsrdtr
409
Chris Liechti518b0d32015-08-30 02:20:39 +0200410 :getter: Get current hardware flow control setting
411 :setter: Enable or disable hardware flow control
412 :type: bool
413
cliechti6066f842009-07-24 00:05:45 +0000414 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000415
Chris Liechtice621882015-08-06 19:29:31 +0200416 .. attribute:: rs485_mode
417
Chris Liechti518b0d32015-08-30 02:20:39 +0200418 :getter: Get the current RS485 settings
419 :setter: Disable (``None``) or enable the RS485 settings
420 :type: :class:`rs485.RS485Settings` or ``None``
421 :platform: Posix (Linux, limited set of hardware)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100422 :platform: Windows (only RTS on TX possible)
Chris Liechtice621882015-08-06 19:29:31 +0200423
424 Attribute to configure RS485 support. When set to an instance of
425 :class:`rs485.RS485Settings` and supported by OS, RTS will be active
426 when data is sent and inactive otherwise (for reception). The
427 :class:`rs485.RS485Settings` class provides additional settings
428 supported on some platforms.
429
430 .. versionadded:: 3.0
431
432
cliechtiedcdbe42009-07-22 00:48:34 +0000433 The following constants are also provided:
434
435 .. attribute:: BAUDRATES
436
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100437 A list of valid baud rates. The list may be incomplete, such that higher
438 and/or intermediate baud rates may also be supported by the device
439 (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000440
441 .. attribute:: BYTESIZES
442
cliechti25375b52010-07-21 23:27:13 +0000443 A list of valid byte sizes for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000444
445 .. attribute:: PARITIES
446
cliechti25375b52010-07-21 23:27:13 +0000447 A list of valid parities for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000448
449 .. attribute:: STOPBITS
450
cliechti25375b52010-07-21 23:27:13 +0000451 A list of valid stop bit widths for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000452
cliechti4a567a02009-07-27 22:09:31 +0000453
cliechti25375b52010-07-21 23:27:13 +0000454 The following methods are for compatibility with the :mod:`io` library.
cliechti4a567a02009-07-27 22:09:31 +0000455
456 .. method:: readable()
457
458 :return: True
cliechtif4566342009-08-04 00:07:19 +0000459
cliechti4a567a02009-07-27 22:09:31 +0000460 .. versionadded:: 2.5
461
462 .. method:: writable()
463
464 :return: True
cliechtif4566342009-08-04 00:07:19 +0000465
cliechti4a567a02009-07-27 22:09:31 +0000466 .. versionadded:: 2.5
467
468 .. method:: seekable()
469
470 :return: False
cliechtif4566342009-08-04 00:07:19 +0000471
cliechti4a567a02009-07-27 22:09:31 +0000472 .. versionadded:: 2.5
473
474 .. method:: readinto(b)
475
cliechti8611bf42009-08-03 00:34:03 +0000476 :param b: bytearray or array instance
cliechti4a567a02009-07-27 22:09:31 +0000477 :return: Number of byte read
478
cliechtid7e7ce22009-08-03 02:01:07 +0000479 Read up to len(b) bytes into :class:`bytearray` *b* and return the
480 number of bytes read.
cliechti4a567a02009-07-27 22:09:31 +0000481
482 .. versionadded:: 2.5
483
Chris Liechti22d39002018-05-08 03:39:20 +0200484 .. method:: readline(size=-1)
485
486 Provided via :meth:`io.IOBase.readline`
487
488 .. method:: readlines(hint=-1)
489
490 Provided via :meth:`io.IOBase.readlines`
491
492 .. method:: writelines(lines)
493
494 Provided via :meth:`io.IOBase.writelines`
495
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100496 The port settings can be read and written as dictionary. The following
497 keys are supported: ``write_timeout``, ``inter_byte_timeout``,
498 ``dsrdtr``, ``baudrate``, ``timeout``, ``parity``, ``bytesize``,
499 ``rtscts``, ``stopbits``, ``xonxoff``
cliechti25375b52010-07-21 23:27:13 +0000500
Chris Liechti256ea212015-08-29 23:57:00 +0200501 .. method:: get_settings()
cliechti4065dce2009-08-10 00:55:46 +0000502
503 :return: a dictionary with current port settings.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100504 :rtype: dict
cliechti4065dce2009-08-10 00:55:46 +0000505
506 Get a dictionary with port settings. This is useful to backup the
507 current settings so that a later point in time they can be restored
Chris Liechti518b0d32015-08-30 02:20:39 +0200508 using :meth:`apply_settings`.
cliechti4065dce2009-08-10 00:55:46 +0000509
Chris Liechti19688bf2016-05-06 23:48:36 +0200510 Note that the state of control lines (RTS/DTR) are not part of the
511 settings.
cliechti4065dce2009-08-10 00:55:46 +0000512
513 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100514 .. versionchanged:: 3.0 renamed from ``getSettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000515
Chris Liechti256ea212015-08-29 23:57:00 +0200516 .. method:: apply_settings(d)
cliechti4065dce2009-08-10 00:55:46 +0000517
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100518 :param dict d: a dictionary with port settings.
cliechti4065dce2009-08-10 00:55:46 +0000519
Chris Liechti518b0d32015-08-30 02:20:39 +0200520 Applies a dictionary that was created by :meth:`get_settings`. Only
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100521 changes are applied and when a key is missing, it means that the
522 setting stays unchanged.
cliechti4065dce2009-08-10 00:55:46 +0000523
524 Note that control lines (RTS/DTR) are not changed.
525
526 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100527 .. versionchanged:: 3.0 renamed from ``applySettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000528
Chris Liechtic9f89962016-08-12 21:04:37 +0200529
Chris Liechti9e205742016-10-31 22:37:13 +0100530 .. _context-manager:
531
Chris Liechtic9f89962016-08-12 21:04:37 +0200532 This class can be used as context manager. The serial port is closed when
533 the context is left.
534
535 .. method:: __enter__()
536
537 :returns: Serial instance
538
539 Returns the instance that was used in the ``with`` statement.
540
541 Example:
542
543 >>> with serial.serial_for_url(port) as s:
544 ... s.write(b'hello')
545
Chris Liechtifd70a552017-07-20 23:46:34 +0200546 The port is opened automatically:
Chris Liechtic9f89962016-08-12 21:04:37 +0200547
Chris Liechtifd70a552017-07-20 23:46:34 +0200548 >>> port = serial.Serial()
549 >>> port.port = '...'
550 >>> with port as s:
Chris Liechtic9f89962016-08-12 21:04:37 +0200551 ... s.write(b'hello')
552
Chris Liechtifd70a552017-07-20 23:46:34 +0200553 Which also means that ``with`` statements can be used repeatedly,
554 each time opening and closing the port.
555
556 .. versionchanged:: 3.4 the port is automatically opened
557
Chris Liechtic9f89962016-08-12 21:04:37 +0200558
559 .. method:: __exit__(exc_type, exc_val, exc_tb)
560
Chris Liechti4f988d42017-03-20 23:59:40 +0100561 Closes serial port (exceptions are not handled by ``__exit__``).
Chris Liechtic9f89962016-08-12 21:04:37 +0200562
563
cliechti25375b52010-07-21 23:27:13 +0000564 Platform specific methods.
565
cliechtic648da92011-12-29 01:17:51 +0000566 .. warning:: Programs using the following methods and attributes are not
567 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000568
569 .. method:: nonblocking()
570
Chris Liechtice621882015-08-06 19:29:31 +0200571 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000572
Chris Liechti59848422016-06-04 22:28:14 +0200573 .. deprecated:: 3.2
574 The serial port is already opened in this mode. This method is not
575 needed and going away.
576
cliechti25375b52010-07-21 23:27:13 +0000577
578 .. method:: fileno()
579
Chris Liechtice621882015-08-06 19:29:31 +0200580 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000581 :return: File descriptor.
582
583 Return file descriptor number for the port that is opened by this object.
584 It is useful when serial ports are used with :mod:`select`.
585
Chris Liechti518b0d32015-08-30 02:20:39 +0200586 .. method:: set_input_flow_control(enable)
cliechti25375b52010-07-21 23:27:13 +0000587
cliechti2f0f8a32011-12-28 22:10:00 +0000588 :platform: Posix
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100589 :param bool enable: Set flow control state.
cliechti25375b52010-07-21 23:27:13 +0000590
cliechti2f0f8a32011-12-28 22:10:00 +0000591 Manually control flow - when software flow control is enabled.
592
593 This will send XON (true) and XOFF (false) to the other device.
594
Chris Liechti518b0d32015-08-30 02:20:39 +0200595 .. versionadded:: 2.7 (Posix support added)
596 .. versionchanged:: 3.0 renamed from ``flowControlOut``
cliechti2f0f8a32011-12-28 22:10:00 +0000597
Chris Liechti518b0d32015-08-30 02:20:39 +0200598 .. method:: set_output_flow_control(enable)
cliechti2f0f8a32011-12-28 22:10:00 +0000599
Chris Liechti518b0d32015-08-30 02:20:39 +0200600 :platform: Posix (HW and SW flow control)
601 :platform: Windows (SW flow control only)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100602 :param bool enable: Set flow control state.
cliechti2f0f8a32011-12-28 22:10:00 +0000603
604 Manually control flow of outgoing data - when hardware or software flow
605 control is enabled.
606
607 Sending will be suspended when called with ``False`` and enabled when
608 called with ``True``.
609
Chris Liechti518b0d32015-08-30 02:20:39 +0200610 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
611 .. versionchanged:: 3.0 renamed from ``setXON``
Chris Liechti256ea212015-08-29 23:57:00 +0200612
Chris Liechti19688bf2016-05-06 23:48:36 +0200613 .. method:: cancel_read()
Chris Liechti256ea212015-08-29 23:57:00 +0200614
Chris Liechti9d893052016-05-18 22:03:29 +0200615 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200616 :platform: Windows
617
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100618 Cancel a pending read operation from another thread. A blocking
Chris Liechti9d893052016-05-18 22:03:29 +0200619 :meth:`read` call is aborted immediately. :meth:`read` will not report
620 any error but return all data received up to that point (similar to a
621 timeout).
622
623 On Posix a call to `cancel_read()` may cancel a future :meth:`read` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200624
625 .. versionadded:: 3.1
626
627 .. method:: cancel_write()
628
Chris Liechti9d893052016-05-18 22:03:29 +0200629 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200630 :platform: Windows
631
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100632 Cancel a pending write operation from another thread. The
Chris Liechti9d893052016-05-18 22:03:29 +0200633 :meth:`write` method will return immediately (no error indicated).
634 However the OS may still be sending from the buffer, a separate call to
635 :meth:`reset_output_buffer` may be needed.
636
637 On Posix a call to `cancel_write()` may cancel a future :meth:`write` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200638
639 .. versionadded:: 3.1
Chris Liechti518b0d32015-08-30 02:20:39 +0200640
Chris Liechti9a99cb22015-08-30 22:16:50 +0200641 .. note:: The following members are deprecated and will be removed in a
Chris Liechti518b0d32015-08-30 02:20:39 +0200642 future release.
Chris Liechti256ea212015-08-29 23:57:00 +0200643
644 .. attribute:: portstr
645
Chris Liechti518b0d32015-08-30 02:20:39 +0200646 .. deprecated:: 2.5 use :attr:`name` instead
Chris Liechti256ea212015-08-29 23:57:00 +0200647
648 .. method:: inWaiting()
649
Chris Liechti518b0d32015-08-30 02:20:39 +0200650 .. deprecated:: 3.0 see :attr:`in_waiting`
651
Matthew Westfa58a972016-07-08 14:27:04 -0700652 .. method:: isOpen()
653
Chris Liechtifd70a552017-07-20 23:46:34 +0200654 .. deprecated:: 3.0 see :attr:`is_open`
Matthew Westfa58a972016-07-08 14:27:04 -0700655
Chris Liechti518b0d32015-08-30 02:20:39 +0200656 .. attribute:: writeTimeout
657
658 .. deprecated:: 3.0 see :attr:`write_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200659
660 .. attribute:: interCharTimeout
661
Chris Liechti518b0d32015-08-30 02:20:39 +0200662 .. deprecated:: 3.0 see :attr:`inter_byte_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200663
664 .. method:: sendBreak(duration=0.25)
665
Chris Liechti518b0d32015-08-30 02:20:39 +0200666 .. deprecated:: 3.0 see :meth:`send_break`
Chris Liechti256ea212015-08-29 23:57:00 +0200667
668 .. method:: flushInput()
669
Chris Liechti518b0d32015-08-30 02:20:39 +0200670 .. deprecated:: 3.0 see :meth:`reset_input_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200671
672 .. method:: flushOutput()
673
Chris Liechti518b0d32015-08-30 02:20:39 +0200674 .. deprecated:: 3.0 see :meth:`reset_output_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200675
676 .. method:: setBreak(level=True)
677
Chris Liechti518b0d32015-08-30 02:20:39 +0200678 .. deprecated:: 3.0 see :attr:`break_condition`
Chris Liechti256ea212015-08-29 23:57:00 +0200679
680 .. method:: setRTS(level=True)
681
Chris Liechti518b0d32015-08-30 02:20:39 +0200682 .. deprecated:: 3.0 see :attr:`rts`
Chris Liechti256ea212015-08-29 23:57:00 +0200683
684 .. method:: setDTR(level=True)
685
Chris Liechti518b0d32015-08-30 02:20:39 +0200686 .. deprecated:: 3.0 see :attr:`dtr`
Chris Liechti256ea212015-08-29 23:57:00 +0200687
688 .. method:: getCTS()
689
Chris Liechti518b0d32015-08-30 02:20:39 +0200690 .. deprecated:: 3.0 see :attr:`cts`
Chris Liechti256ea212015-08-29 23:57:00 +0200691
692 .. method:: getDSR()
693
Chris Liechti518b0d32015-08-30 02:20:39 +0200694 .. deprecated:: 3.0 see :attr:`dsr`
Chris Liechti256ea212015-08-29 23:57:00 +0200695
696 .. method:: getRI()
697
Chris Liechti518b0d32015-08-30 02:20:39 +0200698 .. deprecated:: 3.0 see :attr:`ri`
Chris Liechti256ea212015-08-29 23:57:00 +0200699
700 .. method:: getCD()
701
Chris Liechti518b0d32015-08-30 02:20:39 +0200702 .. deprecated:: 3.0 see :attr:`cd`
Chris Liechti256ea212015-08-29 23:57:00 +0200703
704 .. method:: getSettingsDict()
705
Chris Liechti518b0d32015-08-30 02:20:39 +0200706 .. deprecated:: 3.0 see :meth:`get_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200707
708 .. method:: applySettingsDict(d)
709
Chris Liechti518b0d32015-08-30 02:20:39 +0200710 .. deprecated:: 3.0 see :meth:`apply_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200711
712 .. method:: outWaiting()
713
Chris Liechti518b0d32015-08-30 02:20:39 +0200714 .. deprecated:: 3.0 see :attr:`out_waiting`
Chris Liechti256ea212015-08-29 23:57:00 +0200715
716 .. method:: setXON(level=True)
717
Chris Liechti518b0d32015-08-30 02:20:39 +0200718 .. deprecated:: 3.0 see :meth:`set_output_flow_control`
Chris Liechti256ea212015-08-29 23:57:00 +0200719
720 .. method:: flowControlOut(enable)
721
Chris Liechti518b0d32015-08-30 02:20:39 +0200722 .. deprecated:: 3.0 see :meth:`set_input_flow_control`
cliechtif4566342009-08-04 00:07:19 +0000723
cliechtic648da92011-12-29 01:17:51 +0000724 .. attribute:: rtsToggle
725
726 :platform: Windows
727
728 Attribute to configure RTS toggle control setting. When enabled and
729 supported by OS, RTS will be active when data is available and inactive
730 if no data is available.
731
732 .. versionadded:: 2.6
Chris Liechti518b0d32015-08-30 02:20:39 +0200733 .. versionchanged:: 3.0 (removed, see :attr:`rs485_mode` instead)
cliechtic648da92011-12-29 01:17:51 +0000734
735
cliechtic56e41d2011-08-25 22:06:38 +0000736Implementation detail: some attributes and functions are provided by the
Chris Liechti22d39002018-05-08 03:39:20 +0200737class :class:`serial.SerialBase` which inherits from :class:`io.RawIOBase`
738and some by the platform specific class and others by the base class
739mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000740
Chris Liechti4f988d42017-03-20 23:59:40 +0100741
Chris Liechtice621882015-08-06 19:29:31 +0200742RS485 support
743-------------
744The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to
745enable RS485 specific support on some platforms. Currently Windows and Linux
746(only a small number of devices) are supported.
cliechti4a567a02009-07-27 22:09:31 +0000747
Chris Liechtice621882015-08-06 19:29:31 +0200748:attr:`Serial.rs485_mode` needs to be set to an instance of
749:class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature.
cliechti4a567a02009-07-27 22:09:31 +0000750
Chris Liechtice621882015-08-06 19:29:31 +0200751Usage::
cliechti4a567a02009-07-27 22:09:31 +0000752
Chris Liechti11538242016-09-06 22:20:09 +0200753 import serial
754 import serial.rs485
Chris Liechtice621882015-08-06 19:29:31 +0200755 ser = serial.Serial(...)
756 ser.rs485_mode = serial.rs485.RS485Settings(...)
757 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000758
Chris Liechtice621882015-08-06 19:29:31 +0200759There is a subclass :class:`rs485.RS485` available to emulate the RS485 support
Chris Liechti11538242016-09-06 22:20:09 +0200760on regular serial ports (``serial.rs485`` needs to be imported).
cliechti4a567a02009-07-27 22:09:31 +0000761
cliechti4a567a02009-07-27 22:09:31 +0000762
Chris Liechtice621882015-08-06 19:29:31 +0200763.. class:: rs485.RS485Settings
cliechti4a567a02009-07-27 22:09:31 +0000764
Chris Liechtice621882015-08-06 19:29:31 +0200765 A class that holds RS485 specific settings which are supported on
766 some platforms.
cliechti4a567a02009-07-27 22:09:31 +0000767
Chris Liechtice621882015-08-06 19:29:31 +0200768 .. versionadded:: 3.0
cliechti4a567a02009-07-27 22:09:31 +0000769
Chris Liechtice621882015-08-06 19:29:31 +0200770 .. 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 +0000771
Chris Liechtice621882015-08-06 19:29:31 +0200772 :param bool rts_level_for_tx:
773 RTS level for transmission
cliechti4a567a02009-07-27 22:09:31 +0000774
Chris Liechtice621882015-08-06 19:29:31 +0200775 :param bool rts_level_for_rx:
776 RTS level for reception
cliechti4a567a02009-07-27 22:09:31 +0000777
Chris Liechtice621882015-08-06 19:29:31 +0200778 :param bool loopback:
779 When set to ``True`` transmitted data is also received.
cliechti4a567a02009-07-27 22:09:31 +0000780
Chris Liechtice621882015-08-06 19:29:31 +0200781 :param float delay_before_tx:
782 Delay after setting RTS but before transmission starts
783
784 :param float delay_before_rx:
785 Delay after transmission ends and resetting RTS
786
787 .. attribute:: rts_level_for_tx
788
789 RTS level for transmission.
790
791 .. attribute:: rts_level_for_rx
792
793 RTS level for reception.
794
795 .. attribute:: loopback
796
797 When set to ``True`` transmitted data is also received.
798
799 .. attribute:: delay_before_tx
800
801 Delay after setting RTS but before transmission starts (seconds as float).
802
803 .. attribute:: delay_before_rx
804
805 Delay after transmission ends and resetting RTS (seconds as float).
cliechti4a567a02009-07-27 22:09:31 +0000806
807
Chris Liechtice621882015-08-06 19:29:31 +0200808.. class:: rs485.RS485
cliechti4a567a02009-07-27 22:09:31 +0000809
Chris Liechtice621882015-08-06 19:29:31 +0200810 A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS
811 according to the RS485 settings.
cliechti4a567a02009-07-27 22:09:31 +0000812
Chris Liechti518b0d32015-08-30 02:20:39 +0200813 Usage::
814
815 ser = serial.rs485.RS485(...)
816 ser.rs485_mode = serial.rs485.RS485Settings(...)
817 ser.write(b'hello')
818
Chris Liechtice621882015-08-06 19:29:31 +0200819 .. warning:: This may work unreliably on some serial ports (control signals not
820 synchronized or delayed compared to data). Using delays may be unreliable
821 (varying times, larger than expected) as the OS may not support very fine
822 grained delays (no smaller than in the order of tens of milliseconds).
cliechti4a567a02009-07-27 22:09:31 +0000823
Chris Liechtice621882015-08-06 19:29:31 +0200824 .. note:: Some implementations support this natively in the class
825 :class:`Serial`. Better performance can be expected when the native version
826 is used.
cliechti4a567a02009-07-27 22:09:31 +0000827
Chris Liechtice621882015-08-06 19:29:31 +0200828 .. note:: The loopback property is ignored by this implementation. The actual
829 behavior depends on the used hardware.
cliechti4a567a02009-07-27 22:09:31 +0000830
cliechti4a567a02009-07-27 22:09:31 +0000831
cliechtie214ff82010-07-21 15:48:47 +0000832
cliechti7aed8332009-08-05 14:19:31 +0000833:rfc:`2217` Network ports
834-------------------------
835
836.. warning:: This implementation is currently in an experimental state. Use
837 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000838
cliechtiec4ac1b2009-08-02 00:47:21 +0000839.. class:: rfc2217.Serial
840
Chris Liechti2a1befc2015-10-21 17:37:08 +0200841 This implements a :rfc:`2217` compatible client. Port names are :ref:`URL
842 <URLs>` in the form: ``rfc2217://<host>:<port>[?<option>[&<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000843
cliechtiec4ac1b2009-08-02 00:47:21 +0000844 This class API is compatible to :class:`Serial` with a few exceptions:
845
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100846 - ``write_timeout`` is not implemented
cliechtiec4ac1b2009-08-02 00:47:21 +0000847 - The current implementation starts a thread that keeps reading from the
848 (internal) socket. The thread is managed automatically by the
849 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
850 However it may be a problem for user applications that like to use select
851 instead of threads.
852
853 Due to the nature of the network and protocol involved there are a few
854 extra points to keep in mind:
855
856 - All operations have an additional latency time.
857 - Setting control lines (RTS/CTS) needs more time.
858 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
859 cache is updated depends entirely on the server. The server itself may
860 implement a polling at a certain rate and quick changes may be invisible.
861 - The network layer also has buffers. This means that :meth:`flush`,
Chris Liechti518b0d32015-08-30 02:20:39 +0200862 :meth:`reset_input_buffer` and :meth:`reset_output_buffer` may work with
863 additional delay. Likewise :attr:`in_waiting` returns the size of the
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100864 data arrived at the objects internal buffer and excludes any bytes in the
Chris Liechti518b0d32015-08-30 02:20:39 +0200865 network buffers or any server side buffer.
cliechtiec4ac1b2009-08-02 00:47:21 +0000866 - Closing and immediately reopening the same port may fail due to time
867 needed by the server to get ready again.
868
869 Not implemented yet / Possible problems with the implementation:
870
cliechti8611bf42009-08-03 00:34:03 +0000871 - :rfc:`2217` flow control between client and server (objects internal
872 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000873 - No authentication support (servers may not prompt for a password etc.)
874 - No encryption.
875
876 Due to lack of authentication and encryption it is not suitable to use this
877 client for connections across the internet and should only be used in
878 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000879
cliechti7c640ed2009-08-02 00:54:51 +0000880 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000881
cliechti7aed8332009-08-05 14:19:31 +0000882
883.. class:: rfc2217.PortManager
884
885 This class provides helper functions for implementing :rfc:`2217`
886 compatible servers.
887
Chris Liechti518b0d32015-08-30 02:20:39 +0200888 Basically, it implements everything needed for the :rfc:`2217` protocol.
cliechti7aed8332009-08-05 14:19:31 +0000889 It just does not open sockets and read/write to serial ports (though it
890 changes other port settings). The user of this class must take care of the
891 data transmission itself. The reason for that is, that this way, this class
892 supports all programming models such as threads and select.
893
894 Usage examples can be found in the examples where two TCP/IP - serial
895 converters are shown, one using threads (the single port server) and an
896 other using select (the multi port server).
897
898 .. note:: Each new client connection must create a new instance as this
899 object (and the :rfc:`2217` protocol) has internal state.
900
901 .. method:: __init__(serial_port, connection, debug_output=False)
902
903 :param serial_port: a :class:`Serial` instance that is managed.
904 :param connection: an object implementing :meth:`write`, used to write
905 to the network.
cliechti86844e82009-08-12 00:05:33 +0000906 :param debug_output: enables debug messages: a :class:`logging.Logger`
907 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000908
909 Initializes the Manager and starts negotiating with client in Telnet
910 and :rfc:`2217` protocol. The negotiation starts immediately so that
911 the class should be instantiated in the moment the client connects.
912
913 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000914 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000915 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
916 the :meth:`filter` method.
917
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100918 The *connection* object must implement a :meth:`write` function.
cliechti7aed8332009-08-05 14:19:31 +0000919 This function must ensure that *data* is written at once (no user data
920 mixed in, i.e. it must be thread-safe). All data must be sent in its
921 raw form (:meth:`escape` must not be used) as it is used to send Telnet
922 and :rfc:`2217` control commands.
923
cliechti86844e82009-08-12 00:05:33 +0000924 For diagnostics of the connection or the implementation, *debug_output*
925 can be set to an instance of a :class:`logging.Logger` (e.g.
926 ``logging.getLogger('rfc2217.server')``). The caller should configure
927 the logger using ``setLevel`` for the desired detail level of the logs.
928
cliechti7aed8332009-08-05 14:19:31 +0000929 .. method:: escape(data)
930
931 :param data: data to be sent over the network.
932 :return: data, escaped for Telnet/:rfc:`2217`
933
934 A generator that escapes all data to be compatible with :rfc:`2217`.
935 Implementors of servers should use this function to process all data
936 sent over the network.
937
938 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000939 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000940
941 .. method:: filter(data)
942
943 :param data: data read from the network, including Telnet and
944 :rfc:`2217` controls.
945 :return: data, free from Telnet and :rfc:`2217` controls.
946
947 A generator that filters and processes all data related to :rfc:`2217`.
948 Implementors of servers should use this function to process all data
949 received from the network.
950
951 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000952 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000953
cliechti7cb78e82009-08-05 15:47:57 +0000954 .. method:: check_modem_lines(force_notification=False)
955
956 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000957
958 This function needs to be called periodically (e.g. every second) when
959 the server wants to send NOTIFY_MODEMSTATE messages. This is required
960 to support the client for reading CTS/DSR/RI/CD status lines.
961
962 The function reads the status line and issues the notifications
963 automatically.
964
965 .. versionadded:: 2.5
966
cliechtiec4ac1b2009-08-02 00:47:21 +0000967.. seealso::
968
969 :rfc:`2217` - Telnet Com Port Control Option
970
971
cliechtic1c37602009-07-21 01:34:57 +0000972Exceptions
973==========
974
975.. exception:: SerialException
976
977 Base class for serial port exceptions.
978
cliechti4a567a02009-07-27 22:09:31 +0000979 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000980 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000981
cliechtic1c37602009-07-21 01:34:57 +0000982.. exception:: SerialTimeoutException
983
984 Exception that is raised on write timeouts.
985
986
987Constants
988=========
989
cliechti87686242009-08-18 00:58:31 +0000990*Parity*
991
cliechtic1c37602009-07-21 01:34:57 +0000992.. data:: PARITY_NONE
993.. data:: PARITY_EVEN
994.. data:: PARITY_ODD
995.. data:: PARITY_MARK
996.. data:: PARITY_SPACE
997
cliechti87686242009-08-18 00:58:31 +0000998*Stop bits*
999
cliechtic1c37602009-07-21 01:34:57 +00001000.. data:: STOPBITS_ONE
1001.. data:: STOPBITS_ONE_POINT_FIVE
1002.. data:: STOPBITS_TWO
1003
cliechti06281be2011-08-25 23:08:29 +00001004Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +00001005bits.
1006
cliechti87686242009-08-18 00:58:31 +00001007*Byte size*
1008
cliechtic1c37602009-07-21 01:34:57 +00001009.. data:: FIVEBITS
1010.. data:: SIXBITS
1011.. data:: SEVENBITS
1012.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +00001013
cliechti87686242009-08-18 00:58:31 +00001014
1015*Others*
1016
cliechti8611bf42009-08-03 00:34:03 +00001017Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +00001018software flow control:
cliechti6066f842009-07-24 00:05:45 +00001019
cliechti5b7d16a2009-07-21 21:53:59 +00001020.. data:: XON
1021.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +00001022
cliechti4a567a02009-07-27 22:09:31 +00001023Module version:
cliechtif81362e2009-07-25 03:44:33 +00001024
1025.. data:: VERSION
1026
Chris Liechti518b0d32015-08-30 02:20:39 +02001027 A string indicating the pySerial version, such as ``3.0``.
cliechtif81362e2009-07-25 03:44:33 +00001028
cliechti87686242009-08-18 00:58:31 +00001029 .. versionadded:: 2.3
1030
cliechti44eb98f2011-03-21 21:41:10 +00001031
cliechtie542b362011-03-18 00:49:16 +00001032Module functions and attributes
1033===============================
cliechtif81362e2009-07-25 03:44:33 +00001034
1035.. function:: device(number)
1036
Chris Liechti518b0d32015-08-30 02:20:39 +02001037 .. versionchanged:: 3.0 removed, use ``serial.tools.list_ports`` instead
1038
cliechti7c640ed2009-08-02 00:54:51 +00001039
cliechtie3ab3532009-08-05 12:40:38 +00001040.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +00001041
cliechti86844e82009-08-12 00:05:33 +00001042 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +00001043 :param do_not_open: When set to true, the serial port is not opened.
1044 :return: an instance of :class:`Serial` or a compatible object.
1045
1046 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +00001047 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +00001048 to support both, local ports and remote ports. There is also support
Chris Liechti589c92a2015-09-04 23:04:34 +02001049 for other types, see :ref:`URL <URLs>` section.
cliechti7c640ed2009-08-02 00:54:51 +00001050
cliechtid7e7ce22009-08-03 02:01:07 +00001051 The port is not opened when a keyword parameter called *do_not_open* is
1052 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +00001053
1054 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +00001055
cliechti87686242009-08-18 00:58:31 +00001056
cliechtie542b362011-03-18 00:49:16 +00001057.. attribute:: protocol_handler_packages
1058
1059 This attribute is a list of package names (strings) that is searched for
1060 protocol handlers.
1061
1062 e.g. we want to support a URL ``foobar://``. A module
1063 ``my_handlers.protocol_foobar`` is provided by the user::
1064
1065 serial.protocol_handler_packages.append("my_handlers")
1066 s = serial.serial_for_url("foobar://")
1067
1068 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
1069 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
1070 ``PACKAGE`` from this list.
1071
1072 .. versionadded:: 2.6
1073
1074
cliechti25375b52010-07-21 23:27:13 +00001075.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +00001076
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001077 :param sequence: bytes, bytearray or memoryview
cliechti25375b52010-07-21 23:27:13 +00001078 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +00001079
cliechtie542b362011-03-18 00:49:16 +00001080 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +00001081 compatible to Python 2.x and 3.x.
1082
cliechtie542b362011-03-18 00:49:16 +00001083 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +00001084 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +00001085 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +00001086
1087 This function is used internally and in the unit tests.
1088
cliechtie542b362011-03-18 00:49:16 +00001089 .. versionadded:: 2.5
1090
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001091.. function:: iterbytes(sequence)
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001092
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001093 :param sequence: bytes, bytearray or memoryview
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001094 :returns: a generator that yields bytes
1095
1096 Some versions of Python (3.x) would return integers instead of bytes when
Chris Liechti4f988d42017-03-20 23:59:40 +01001097 looping over an instance of ``bytes``. This helper function ensures that
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001098 bytes are returned.
1099
1100 .. versionadded:: 3.0
1101
cliechtie214ff82010-07-21 15:48:47 +00001102
Chris Liechti811bf382015-10-10 00:54:47 +02001103Threading
1104=========
1105
1106.. module:: serial.threaded
1107.. versionadded:: 3.0
1108
1109.. warning:: This implementation is currently in an experimental state. Use
1110 at your own risk.
1111
1112This module provides classes to simplify working with threads and protocols.
1113
1114.. class:: Protocol
1115
Chris Liechti5665d572015-10-13 23:50:01 +02001116 Protocol as used by the :class:`ReaderThread`. This base class provides empty
Chris Liechti811bf382015-10-10 00:54:47 +02001117 implementations of all methods.
1118
1119
1120 .. method:: connection_made(transport)
1121
1122 :param transport: instance used to write to serial port.
1123
1124 Called when reader thread is started.
1125
1126 .. method:: data_received(data)
1127
Chris Liechtif8a09192015-12-20 23:36:38 +01001128 :param bytes data: received bytes
Chris Liechti811bf382015-10-10 00:54:47 +02001129
1130 Called with snippets received from the serial port.
1131
1132 .. method:: connection_lost(exc)
1133
1134 :param exc: Exception if connection was terminated by error else ``None``
1135
1136 Called when the serial port is closed or the reader loop terminated
1137 otherwise.
1138
1139.. class:: Packetizer(Protocol)
1140
1141 Read binary packets from serial port. Packets are expected to be terminated
1142 with a ``TERMINATOR`` byte (null byte by default).
1143
1144 The class also keeps track of the transport.
1145
1146 .. attribute:: TERMINATOR = b'\\0'
1147
1148 .. method:: __init__()
1149
1150 .. method:: connection_made(transport)
1151
Chris Liechtif8a09192015-12-20 23:36:38 +01001152 Stores transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001153
1154 .. method:: connection_lost(exc)
1155
Chris Liechtif8a09192015-12-20 23:36:38 +01001156 Forgets transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001157
1158 .. method:: data_received(data)
1159
Chris Liechtif8a09192015-12-20 23:36:38 +01001160 :param bytes data: partial received data
1161
1162 Buffer received data and search for :attr:`TERMINATOR`, when found,
Chris Liechti811bf382015-10-10 00:54:47 +02001163 call :meth:`handle_packet`.
1164
1165 .. method:: handle_packet(packet)
1166
Chris Liechti1c4c5992016-01-18 20:46:00 +01001167 :param bytes packet: a packet as defined by ``TERMINATOR``
1168
Chris Liechti811bf382015-10-10 00:54:47 +02001169 Process packets - to be overridden by subclassing.
1170
1171
1172.. class:: LineReader(Packetizer)
1173
1174 Read and write (Unicode) lines from/to serial port.
1175 The encoding is applied.
1176
1177
1178 .. attribute:: TERMINATOR = b'\\r\\n'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001179
1180 Line ending.
1181
Chris Liechti811bf382015-10-10 00:54:47 +02001182 .. attribute:: ENCODING = 'utf-8'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001183
1184 Encoding of the send and received data.
1185
Chris Liechti811bf382015-10-10 00:54:47 +02001186 .. attribute:: UNICODE_HANDLING = 'replace'
1187
Chris Liechti1c4c5992016-01-18 20:46:00 +01001188 Unicode error handly policy.
1189
Chris Liechti811bf382015-10-10 00:54:47 +02001190 .. method:: handle_packet(packet)
1191
Chris Liechti1c4c5992016-01-18 20:46:00 +01001192 :param bytes packet: a packet as defined by ``TERMINATOR``
1193
1194 In this case it will be a line, calls :meth:`handle_line` after applying
1195 the :attr:`ENCODING`.
1196
Chris Liechti811bf382015-10-10 00:54:47 +02001197 .. method:: handle_line(line)
1198
Chris Liechtif8a09192015-12-20 23:36:38 +01001199 :param str line: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001200
1201 Process one line - to be overridden by subclassing.
1202
1203 .. method:: write_line(text)
1204
Chris Liechtif8a09192015-12-20 23:36:38 +01001205 :param str text: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001206
Chris Liechtif8a09192015-12-20 23:36:38 +01001207 Write *text* to the transport. *text* is expected to be a Unicode
1208 string and the encoding is applied before sending and also the
1209 :attr:`TERMINATOR` (new line) is appended.
Chris Liechti811bf382015-10-10 00:54:47 +02001210
1211
Chris Liechti5665d572015-10-13 23:50:01 +02001212.. class:: ReaderThread(threading.Thread)
Chris Liechti811bf382015-10-10 00:54:47 +02001213
1214 Implement a serial port read loop and dispatch to a Protocol instance (like
1215 the :class:`asyncio.Protocol`) but do it with threads.
1216
Chris Liechtif8a09192015-12-20 23:36:38 +01001217 Calls to :meth:`close` will close the serial port but it is also possible
1218 to just :meth:`stop` this thread and continue to use the serial port
1219 instance otherwise.
Chris Liechti811bf382015-10-10 00:54:47 +02001220
1221 .. method:: __init__(serial_instance, protocol_factory)
1222
1223 :param serial_instance: serial port instance (opened) to be used.
1224 :param protocol_factory: a callable that returns a Protocol instance
1225
1226 Initialize thread.
1227
Chris Liechtif8a09192015-12-20 23:36:38 +01001228 Note that the ``serial_instance`` 's timeout is set to one second!
Chris Liechti811bf382015-10-10 00:54:47 +02001229 Other settings are not changed.
1230
1231 .. method:: stop()
1232
1233 Stop the reader thread.
1234
1235 .. method:: run()
1236
1237 The actual reader loop driven by the thread. It calls
1238 :meth:`Protocol.connection_made`, reads from the serial port calling
Chris Liechtif8a09192015-12-20 23:36:38 +01001239 :meth:`Protocol.data_received` and finally calls :meth:`Protocol.connection_lost`
Chris Liechti811bf382015-10-10 00:54:47 +02001240 when :meth:`close` is called or an error occurs.
1241
1242 .. method:: write(data)
1243
Chris Liechtif8a09192015-12-20 23:36:38 +01001244 :param bytes data: data to write
1245
Chris Liechti811bf382015-10-10 00:54:47 +02001246 Thread safe writing (uses lock).
1247
1248 .. method:: close()
1249
1250 Close the serial port and exit reader thread, calls :meth:`stop` (uses lock).
1251
1252 .. method:: connect()
1253
1254 Wait until connection is set up and return the transport and protocol
1255 instances.
1256
1257
1258 This class can be used as context manager, in this case it starts
1259 the thread and connects automatically. The serial port is closed
1260 when the context is left.
1261
1262 .. method:: __enter__()
1263
1264 :returns: protocol
1265
1266 Connect and return protocol instance.
1267
1268 .. method:: __exit__(exc_type, exc_val, exc_tb)
1269
1270 Closes serial port.
1271
Chris Liechtia1436b12015-10-12 23:19:04 +02001272Example::
1273
1274 class PrintLines(LineReader):
1275 def connection_made(self, transport):
1276 super(PrintLines, self).connection_made(transport)
1277 sys.stdout.write('port opened\n')
1278 self.write_line('hello world')
1279
1280 def handle_line(self, data):
1281 sys.stdout.write('line received: {}\n'.format(repr(data)))
1282
1283 def connection_lost(self, exc):
1284 if exc:
1285 traceback.print_exc(exc)
1286 sys.stdout.write('port closed\n')
1287
1288 ser = serial.serial_for_url('loop://', baudrate=115200, timeout=1)
Chris Liechti5665d572015-10-13 23:50:01 +02001289 with ReaderThread(ser, PrintLines) as protocol:
Chris Liechtia1436b12015-10-12 23:19:04 +02001290 protocol.write_line('hello')
1291 time.sleep(2)
1292
Chris Liechti811bf382015-10-10 00:54:47 +02001293
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001294asyncio
1295=======
1296
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001297``asyncio`` was introduced with Python 3.4. Experimental support for pySerial
1298is provided via a separate distribution `pyserial-asyncio`_.
Chris Liechti589c92a2015-09-04 23:04:34 +02001299
Jakub Wilk772a6fa2016-12-20 21:59:27 +01001300It is currently under development, see:
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001301
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001302- http://pyserial-asyncio.readthedocs.io/
1303- https://github.com/pyserial/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001304
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001305.. _`pyserial-asyncio`: https://pypi.python.org/pypi/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001306