blob: 545f948132ea6f77b85ebcbaabfce74e7128c5c0 [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 Liechti32ccf2e2015-12-19 23:42:40 +0100468 The port settings can be read and written as dictionary. The following
469 keys are supported: ``write_timeout``, ``inter_byte_timeout``,
470 ``dsrdtr``, ``baudrate``, ``timeout``, ``parity``, ``bytesize``,
471 ``rtscts``, ``stopbits``, ``xonxoff``
cliechti25375b52010-07-21 23:27:13 +0000472
Chris Liechti256ea212015-08-29 23:57:00 +0200473 .. method:: get_settings()
cliechti4065dce2009-08-10 00:55:46 +0000474
475 :return: a dictionary with current port settings.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100476 :rtype: dict
cliechti4065dce2009-08-10 00:55:46 +0000477
478 Get a dictionary with port settings. This is useful to backup the
479 current settings so that a later point in time they can be restored
Chris Liechti518b0d32015-08-30 02:20:39 +0200480 using :meth:`apply_settings`.
cliechti4065dce2009-08-10 00:55:46 +0000481
Chris Liechti19688bf2016-05-06 23:48:36 +0200482 Note that the state of control lines (RTS/DTR) are not part of the
483 settings.
cliechti4065dce2009-08-10 00:55:46 +0000484
485 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100486 .. versionchanged:: 3.0 renamed from ``getSettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000487
Chris Liechti256ea212015-08-29 23:57:00 +0200488 .. method:: apply_settings(d)
cliechti4065dce2009-08-10 00:55:46 +0000489
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100490 :param dict d: a dictionary with port settings.
cliechti4065dce2009-08-10 00:55:46 +0000491
Chris Liechti518b0d32015-08-30 02:20:39 +0200492 Applies a dictionary that was created by :meth:`get_settings`. Only
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100493 changes are applied and when a key is missing, it means that the
494 setting stays unchanged.
cliechti4065dce2009-08-10 00:55:46 +0000495
496 Note that control lines (RTS/DTR) are not changed.
497
498 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100499 .. versionchanged:: 3.0 renamed from ``applySettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000500
Chris Liechtic9f89962016-08-12 21:04:37 +0200501
Chris Liechti9e205742016-10-31 22:37:13 +0100502 .. _context-manager:
503
Chris Liechtic9f89962016-08-12 21:04:37 +0200504 This class can be used as context manager. The serial port is closed when
505 the context is left.
506
507 .. method:: __enter__()
508
509 :returns: Serial instance
510
511 Returns the instance that was used in the ``with`` statement.
512
513 Example:
514
515 >>> with serial.serial_for_url(port) as s:
516 ... s.write(b'hello')
517
Chris Liechtifd70a552017-07-20 23:46:34 +0200518 The port is opened automatically:
Chris Liechtic9f89962016-08-12 21:04:37 +0200519
Chris Liechtifd70a552017-07-20 23:46:34 +0200520 >>> port = serial.Serial()
521 >>> port.port = '...'
522 >>> with port as s:
Chris Liechtic9f89962016-08-12 21:04:37 +0200523 ... s.write(b'hello')
524
Chris Liechtifd70a552017-07-20 23:46:34 +0200525 Which also means that ``with`` statements can be used repeatedly,
526 each time opening and closing the port.
527
528 .. versionchanged:: 3.4 the port is automatically opened
529
Chris Liechtic9f89962016-08-12 21:04:37 +0200530
531 .. method:: __exit__(exc_type, exc_val, exc_tb)
532
Chris Liechti4f988d42017-03-20 23:59:40 +0100533 Closes serial port (exceptions are not handled by ``__exit__``).
Chris Liechtic9f89962016-08-12 21:04:37 +0200534
535
cliechti25375b52010-07-21 23:27:13 +0000536 Platform specific methods.
537
cliechtic648da92011-12-29 01:17:51 +0000538 .. warning:: Programs using the following methods and attributes are not
539 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000540
541 .. method:: nonblocking()
542
Chris Liechtice621882015-08-06 19:29:31 +0200543 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000544
Chris Liechti59848422016-06-04 22:28:14 +0200545 .. deprecated:: 3.2
546 The serial port is already opened in this mode. This method is not
547 needed and going away.
548
cliechti25375b52010-07-21 23:27:13 +0000549
550 .. method:: fileno()
551
Chris Liechtice621882015-08-06 19:29:31 +0200552 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000553 :return: File descriptor.
554
555 Return file descriptor number for the port that is opened by this object.
556 It is useful when serial ports are used with :mod:`select`.
557
Chris Liechti518b0d32015-08-30 02:20:39 +0200558 .. method:: set_input_flow_control(enable)
cliechti25375b52010-07-21 23:27:13 +0000559
cliechti2f0f8a32011-12-28 22:10:00 +0000560 :platform: Posix
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100561 :param bool enable: Set flow control state.
cliechti25375b52010-07-21 23:27:13 +0000562
cliechti2f0f8a32011-12-28 22:10:00 +0000563 Manually control flow - when software flow control is enabled.
564
565 This will send XON (true) and XOFF (false) to the other device.
566
Chris Liechti518b0d32015-08-30 02:20:39 +0200567 .. versionadded:: 2.7 (Posix support added)
568 .. versionchanged:: 3.0 renamed from ``flowControlOut``
cliechti2f0f8a32011-12-28 22:10:00 +0000569
Chris Liechti518b0d32015-08-30 02:20:39 +0200570 .. method:: set_output_flow_control(enable)
cliechti2f0f8a32011-12-28 22:10:00 +0000571
Chris Liechti518b0d32015-08-30 02:20:39 +0200572 :platform: Posix (HW and SW flow control)
573 :platform: Windows (SW flow control only)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100574 :param bool enable: Set flow control state.
cliechti2f0f8a32011-12-28 22:10:00 +0000575
576 Manually control flow of outgoing data - when hardware or software flow
577 control is enabled.
578
579 Sending will be suspended when called with ``False`` and enabled when
580 called with ``True``.
581
Chris Liechti518b0d32015-08-30 02:20:39 +0200582 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
583 .. versionchanged:: 3.0 renamed from ``setXON``
Chris Liechti256ea212015-08-29 23:57:00 +0200584
Chris Liechti19688bf2016-05-06 23:48:36 +0200585 .. method:: cancel_read()
Chris Liechti256ea212015-08-29 23:57:00 +0200586
Chris Liechti9d893052016-05-18 22:03:29 +0200587 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200588 :platform: Windows
589
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100590 Cancel a pending read operation from another thread. A blocking
Chris Liechti9d893052016-05-18 22:03:29 +0200591 :meth:`read` call is aborted immediately. :meth:`read` will not report
592 any error but return all data received up to that point (similar to a
593 timeout).
594
595 On Posix a call to `cancel_read()` may cancel a future :meth:`read` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200596
597 .. versionadded:: 3.1
598
599 .. method:: cancel_write()
600
Chris Liechti9d893052016-05-18 22:03:29 +0200601 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200602 :platform: Windows
603
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100604 Cancel a pending write operation from another thread. The
Chris Liechti9d893052016-05-18 22:03:29 +0200605 :meth:`write` method will return immediately (no error indicated).
606 However the OS may still be sending from the buffer, a separate call to
607 :meth:`reset_output_buffer` may be needed.
608
609 On Posix a call to `cancel_write()` may cancel a future :meth:`write` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200610
611 .. versionadded:: 3.1
Chris Liechti518b0d32015-08-30 02:20:39 +0200612
Chris Liechti9a99cb22015-08-30 22:16:50 +0200613 .. note:: The following members are deprecated and will be removed in a
Chris Liechti518b0d32015-08-30 02:20:39 +0200614 future release.
Chris Liechti256ea212015-08-29 23:57:00 +0200615
616 .. attribute:: portstr
617
Chris Liechti518b0d32015-08-30 02:20:39 +0200618 .. deprecated:: 2.5 use :attr:`name` instead
Chris Liechti256ea212015-08-29 23:57:00 +0200619
620 .. method:: inWaiting()
621
Chris Liechti518b0d32015-08-30 02:20:39 +0200622 .. deprecated:: 3.0 see :attr:`in_waiting`
623
Matthew Westfa58a972016-07-08 14:27:04 -0700624 .. method:: isOpen()
625
Chris Liechtifd70a552017-07-20 23:46:34 +0200626 .. deprecated:: 3.0 see :attr:`is_open`
Matthew Westfa58a972016-07-08 14:27:04 -0700627
Chris Liechti518b0d32015-08-30 02:20:39 +0200628 .. attribute:: writeTimeout
629
630 .. deprecated:: 3.0 see :attr:`write_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200631
632 .. attribute:: interCharTimeout
633
Chris Liechti518b0d32015-08-30 02:20:39 +0200634 .. deprecated:: 3.0 see :attr:`inter_byte_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200635
636 .. method:: sendBreak(duration=0.25)
637
Chris Liechti518b0d32015-08-30 02:20:39 +0200638 .. deprecated:: 3.0 see :meth:`send_break`
Chris Liechti256ea212015-08-29 23:57:00 +0200639
640 .. method:: flushInput()
641
Chris Liechti518b0d32015-08-30 02:20:39 +0200642 .. deprecated:: 3.0 see :meth:`reset_input_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200643
644 .. method:: flushOutput()
645
Chris Liechti518b0d32015-08-30 02:20:39 +0200646 .. deprecated:: 3.0 see :meth:`reset_output_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200647
648 .. method:: setBreak(level=True)
649
Chris Liechti518b0d32015-08-30 02:20:39 +0200650 .. deprecated:: 3.0 see :attr:`break_condition`
Chris Liechti256ea212015-08-29 23:57:00 +0200651
652 .. method:: setRTS(level=True)
653
Chris Liechti518b0d32015-08-30 02:20:39 +0200654 .. deprecated:: 3.0 see :attr:`rts`
Chris Liechti256ea212015-08-29 23:57:00 +0200655
656 .. method:: setDTR(level=True)
657
Chris Liechti518b0d32015-08-30 02:20:39 +0200658 .. deprecated:: 3.0 see :attr:`dtr`
Chris Liechti256ea212015-08-29 23:57:00 +0200659
660 .. method:: getCTS()
661
Chris Liechti518b0d32015-08-30 02:20:39 +0200662 .. deprecated:: 3.0 see :attr:`cts`
Chris Liechti256ea212015-08-29 23:57:00 +0200663
664 .. method:: getDSR()
665
Chris Liechti518b0d32015-08-30 02:20:39 +0200666 .. deprecated:: 3.0 see :attr:`dsr`
Chris Liechti256ea212015-08-29 23:57:00 +0200667
668 .. method:: getRI()
669
Chris Liechti518b0d32015-08-30 02:20:39 +0200670 .. deprecated:: 3.0 see :attr:`ri`
Chris Liechti256ea212015-08-29 23:57:00 +0200671
672 .. method:: getCD()
673
Chris Liechti518b0d32015-08-30 02:20:39 +0200674 .. deprecated:: 3.0 see :attr:`cd`
Chris Liechti256ea212015-08-29 23:57:00 +0200675
676 .. method:: getSettingsDict()
677
Chris Liechti518b0d32015-08-30 02:20:39 +0200678 .. deprecated:: 3.0 see :meth:`get_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200679
680 .. method:: applySettingsDict(d)
681
Chris Liechti518b0d32015-08-30 02:20:39 +0200682 .. deprecated:: 3.0 see :meth:`apply_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200683
684 .. method:: outWaiting()
685
Chris Liechti518b0d32015-08-30 02:20:39 +0200686 .. deprecated:: 3.0 see :attr:`out_waiting`
Chris Liechti256ea212015-08-29 23:57:00 +0200687
688 .. method:: setXON(level=True)
689
Chris Liechti518b0d32015-08-30 02:20:39 +0200690 .. deprecated:: 3.0 see :meth:`set_output_flow_control`
Chris Liechti256ea212015-08-29 23:57:00 +0200691
692 .. method:: flowControlOut(enable)
693
Chris Liechti518b0d32015-08-30 02:20:39 +0200694 .. deprecated:: 3.0 see :meth:`set_input_flow_control`
cliechtif4566342009-08-04 00:07:19 +0000695
cliechtic648da92011-12-29 01:17:51 +0000696 .. attribute:: rtsToggle
697
698 :platform: Windows
699
700 Attribute to configure RTS toggle control setting. When enabled and
701 supported by OS, RTS will be active when data is available and inactive
702 if no data is available.
703
704 .. versionadded:: 2.6
Chris Liechti518b0d32015-08-30 02:20:39 +0200705 .. versionchanged:: 3.0 (removed, see :attr:`rs485_mode` instead)
cliechtic648da92011-12-29 01:17:51 +0000706
707
cliechtic56e41d2011-08-25 22:06:38 +0000708Implementation detail: some attributes and functions are provided by the
709class :class:`SerialBase` and some by the platform specific class and
710others by the base class mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000711
Chris Liechti4f988d42017-03-20 23:59:40 +0100712
Chris Liechtice621882015-08-06 19:29:31 +0200713RS485 support
714-------------
715The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to
716enable RS485 specific support on some platforms. Currently Windows and Linux
717(only a small number of devices) are supported.
cliechti4a567a02009-07-27 22:09:31 +0000718
Chris Liechtice621882015-08-06 19:29:31 +0200719:attr:`Serial.rs485_mode` needs to be set to an instance of
720:class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature.
cliechti4a567a02009-07-27 22:09:31 +0000721
Chris Liechtice621882015-08-06 19:29:31 +0200722Usage::
cliechti4a567a02009-07-27 22:09:31 +0000723
Chris Liechti11538242016-09-06 22:20:09 +0200724 import serial
725 import serial.rs485
Chris Liechtice621882015-08-06 19:29:31 +0200726 ser = serial.Serial(...)
727 ser.rs485_mode = serial.rs485.RS485Settings(...)
728 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000729
Chris Liechtice621882015-08-06 19:29:31 +0200730There is a subclass :class:`rs485.RS485` available to emulate the RS485 support
Chris Liechti11538242016-09-06 22:20:09 +0200731on regular serial ports (``serial.rs485`` needs to be imported).
cliechti4a567a02009-07-27 22:09:31 +0000732
cliechti4a567a02009-07-27 22:09:31 +0000733
Chris Liechtice621882015-08-06 19:29:31 +0200734.. class:: rs485.RS485Settings
cliechti4a567a02009-07-27 22:09:31 +0000735
Chris Liechtice621882015-08-06 19:29:31 +0200736 A class that holds RS485 specific settings which are supported on
737 some platforms.
cliechti4a567a02009-07-27 22:09:31 +0000738
Chris Liechtice621882015-08-06 19:29:31 +0200739 .. versionadded:: 3.0
cliechti4a567a02009-07-27 22:09:31 +0000740
Chris Liechtice621882015-08-06 19:29:31 +0200741 .. 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 +0000742
Chris Liechtice621882015-08-06 19:29:31 +0200743 :param bool rts_level_for_tx:
744 RTS level for transmission
cliechti4a567a02009-07-27 22:09:31 +0000745
Chris Liechtice621882015-08-06 19:29:31 +0200746 :param bool rts_level_for_rx:
747 RTS level for reception
cliechti4a567a02009-07-27 22:09:31 +0000748
Chris Liechtice621882015-08-06 19:29:31 +0200749 :param bool loopback:
750 When set to ``True`` transmitted data is also received.
cliechti4a567a02009-07-27 22:09:31 +0000751
Chris Liechtice621882015-08-06 19:29:31 +0200752 :param float delay_before_tx:
753 Delay after setting RTS but before transmission starts
754
755 :param float delay_before_rx:
756 Delay after transmission ends and resetting RTS
757
758 .. attribute:: rts_level_for_tx
759
760 RTS level for transmission.
761
762 .. attribute:: rts_level_for_rx
763
764 RTS level for reception.
765
766 .. attribute:: loopback
767
768 When set to ``True`` transmitted data is also received.
769
770 .. attribute:: delay_before_tx
771
772 Delay after setting RTS but before transmission starts (seconds as float).
773
774 .. attribute:: delay_before_rx
775
776 Delay after transmission ends and resetting RTS (seconds as float).
cliechti4a567a02009-07-27 22:09:31 +0000777
778
Chris Liechtice621882015-08-06 19:29:31 +0200779.. class:: rs485.RS485
cliechti4a567a02009-07-27 22:09:31 +0000780
Chris Liechtice621882015-08-06 19:29:31 +0200781 A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS
782 according to the RS485 settings.
cliechti4a567a02009-07-27 22:09:31 +0000783
Chris Liechti518b0d32015-08-30 02:20:39 +0200784 Usage::
785
786 ser = serial.rs485.RS485(...)
787 ser.rs485_mode = serial.rs485.RS485Settings(...)
788 ser.write(b'hello')
789
Chris Liechtice621882015-08-06 19:29:31 +0200790 .. warning:: This may work unreliably on some serial ports (control signals not
791 synchronized or delayed compared to data). Using delays may be unreliable
792 (varying times, larger than expected) as the OS may not support very fine
793 grained delays (no smaller than in the order of tens of milliseconds).
cliechti4a567a02009-07-27 22:09:31 +0000794
Chris Liechtice621882015-08-06 19:29:31 +0200795 .. note:: Some implementations support this natively in the class
796 :class:`Serial`. Better performance can be expected when the native version
797 is used.
cliechti4a567a02009-07-27 22:09:31 +0000798
Chris Liechtice621882015-08-06 19:29:31 +0200799 .. note:: The loopback property is ignored by this implementation. The actual
800 behavior depends on the used hardware.
cliechti4a567a02009-07-27 22:09:31 +0000801
cliechti4a567a02009-07-27 22:09:31 +0000802
cliechtie214ff82010-07-21 15:48:47 +0000803
cliechti7aed8332009-08-05 14:19:31 +0000804:rfc:`2217` Network ports
805-------------------------
806
807.. warning:: This implementation is currently in an experimental state. Use
808 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000809
cliechtiec4ac1b2009-08-02 00:47:21 +0000810.. class:: rfc2217.Serial
811
Chris Liechti2a1befc2015-10-21 17:37:08 +0200812 This implements a :rfc:`2217` compatible client. Port names are :ref:`URL
813 <URLs>` in the form: ``rfc2217://<host>:<port>[?<option>[&<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000814
cliechtiec4ac1b2009-08-02 00:47:21 +0000815 This class API is compatible to :class:`Serial` with a few exceptions:
816
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100817 - ``write_timeout`` is not implemented
cliechtiec4ac1b2009-08-02 00:47:21 +0000818 - The current implementation starts a thread that keeps reading from the
819 (internal) socket. The thread is managed automatically by the
820 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
821 However it may be a problem for user applications that like to use select
822 instead of threads.
823
824 Due to the nature of the network and protocol involved there are a few
825 extra points to keep in mind:
826
827 - All operations have an additional latency time.
828 - Setting control lines (RTS/CTS) needs more time.
829 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
830 cache is updated depends entirely on the server. The server itself may
831 implement a polling at a certain rate and quick changes may be invisible.
832 - The network layer also has buffers. This means that :meth:`flush`,
Chris Liechti518b0d32015-08-30 02:20:39 +0200833 :meth:`reset_input_buffer` and :meth:`reset_output_buffer` may work with
834 additional delay. Likewise :attr:`in_waiting` returns the size of the
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100835 data arrived at the objects internal buffer and excludes any bytes in the
Chris Liechti518b0d32015-08-30 02:20:39 +0200836 network buffers or any server side buffer.
cliechtiec4ac1b2009-08-02 00:47:21 +0000837 - Closing and immediately reopening the same port may fail due to time
838 needed by the server to get ready again.
839
840 Not implemented yet / Possible problems with the implementation:
841
cliechti8611bf42009-08-03 00:34:03 +0000842 - :rfc:`2217` flow control between client and server (objects internal
843 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000844 - No authentication support (servers may not prompt for a password etc.)
845 - No encryption.
846
847 Due to lack of authentication and encryption it is not suitable to use this
848 client for connections across the internet and should only be used in
849 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000850
cliechti7c640ed2009-08-02 00:54:51 +0000851 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000852
cliechti7aed8332009-08-05 14:19:31 +0000853
854.. class:: rfc2217.PortManager
855
856 This class provides helper functions for implementing :rfc:`2217`
857 compatible servers.
858
Chris Liechti518b0d32015-08-30 02:20:39 +0200859 Basically, it implements everything needed for the :rfc:`2217` protocol.
cliechti7aed8332009-08-05 14:19:31 +0000860 It just does not open sockets and read/write to serial ports (though it
861 changes other port settings). The user of this class must take care of the
862 data transmission itself. The reason for that is, that this way, this class
863 supports all programming models such as threads and select.
864
865 Usage examples can be found in the examples where two TCP/IP - serial
866 converters are shown, one using threads (the single port server) and an
867 other using select (the multi port server).
868
869 .. note:: Each new client connection must create a new instance as this
870 object (and the :rfc:`2217` protocol) has internal state.
871
872 .. method:: __init__(serial_port, connection, debug_output=False)
873
874 :param serial_port: a :class:`Serial` instance that is managed.
875 :param connection: an object implementing :meth:`write`, used to write
876 to the network.
cliechti86844e82009-08-12 00:05:33 +0000877 :param debug_output: enables debug messages: a :class:`logging.Logger`
878 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000879
880 Initializes the Manager and starts negotiating with client in Telnet
881 and :rfc:`2217` protocol. The negotiation starts immediately so that
882 the class should be instantiated in the moment the client connects.
883
884 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000885 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000886 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
887 the :meth:`filter` method.
888
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100889 The *connection* object must implement a :meth:`write` function.
cliechti7aed8332009-08-05 14:19:31 +0000890 This function must ensure that *data* is written at once (no user data
891 mixed in, i.e. it must be thread-safe). All data must be sent in its
892 raw form (:meth:`escape` must not be used) as it is used to send Telnet
893 and :rfc:`2217` control commands.
894
cliechti86844e82009-08-12 00:05:33 +0000895 For diagnostics of the connection or the implementation, *debug_output*
896 can be set to an instance of a :class:`logging.Logger` (e.g.
897 ``logging.getLogger('rfc2217.server')``). The caller should configure
898 the logger using ``setLevel`` for the desired detail level of the logs.
899
cliechti7aed8332009-08-05 14:19:31 +0000900 .. method:: escape(data)
901
902 :param data: data to be sent over the network.
903 :return: data, escaped for Telnet/:rfc:`2217`
904
905 A generator that escapes all data to be compatible with :rfc:`2217`.
906 Implementors of servers should use this function to process all data
907 sent over the network.
908
909 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000910 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000911
912 .. method:: filter(data)
913
914 :param data: data read from the network, including Telnet and
915 :rfc:`2217` controls.
916 :return: data, free from Telnet and :rfc:`2217` controls.
917
918 A generator that filters and processes all data related to :rfc:`2217`.
919 Implementors of servers should use this function to process all data
920 received from 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
cliechti7cb78e82009-08-05 15:47:57 +0000925 .. method:: check_modem_lines(force_notification=False)
926
927 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000928
929 This function needs to be called periodically (e.g. every second) when
930 the server wants to send NOTIFY_MODEMSTATE messages. This is required
931 to support the client for reading CTS/DSR/RI/CD status lines.
932
933 The function reads the status line and issues the notifications
934 automatically.
935
936 .. versionadded:: 2.5
937
cliechtiec4ac1b2009-08-02 00:47:21 +0000938.. seealso::
939
940 :rfc:`2217` - Telnet Com Port Control Option
941
942
cliechtic1c37602009-07-21 01:34:57 +0000943Exceptions
944==========
945
946.. exception:: SerialException
947
948 Base class for serial port exceptions.
949
cliechti4a567a02009-07-27 22:09:31 +0000950 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000951 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000952
cliechtic1c37602009-07-21 01:34:57 +0000953.. exception:: SerialTimeoutException
954
955 Exception that is raised on write timeouts.
956
957
958Constants
959=========
960
cliechti87686242009-08-18 00:58:31 +0000961*Parity*
962
cliechtic1c37602009-07-21 01:34:57 +0000963.. data:: PARITY_NONE
964.. data:: PARITY_EVEN
965.. data:: PARITY_ODD
966.. data:: PARITY_MARK
967.. data:: PARITY_SPACE
968
cliechti87686242009-08-18 00:58:31 +0000969*Stop bits*
970
cliechtic1c37602009-07-21 01:34:57 +0000971.. data:: STOPBITS_ONE
972.. data:: STOPBITS_ONE_POINT_FIVE
973.. data:: STOPBITS_TWO
974
cliechti06281be2011-08-25 23:08:29 +0000975Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +0000976bits.
977
cliechti87686242009-08-18 00:58:31 +0000978*Byte size*
979
cliechtic1c37602009-07-21 01:34:57 +0000980.. data:: FIVEBITS
981.. data:: SIXBITS
982.. data:: SEVENBITS
983.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000984
cliechti87686242009-08-18 00:58:31 +0000985
986*Others*
987
cliechti8611bf42009-08-03 00:34:03 +0000988Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000989software flow control:
cliechti6066f842009-07-24 00:05:45 +0000990
cliechti5b7d16a2009-07-21 21:53:59 +0000991.. data:: XON
992.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000993
cliechti4a567a02009-07-27 22:09:31 +0000994Module version:
cliechtif81362e2009-07-25 03:44:33 +0000995
996.. data:: VERSION
997
Chris Liechti518b0d32015-08-30 02:20:39 +0200998 A string indicating the pySerial version, such as ``3.0``.
cliechtif81362e2009-07-25 03:44:33 +0000999
cliechti87686242009-08-18 00:58:31 +00001000 .. versionadded:: 2.3
1001
cliechti44eb98f2011-03-21 21:41:10 +00001002
cliechtie542b362011-03-18 00:49:16 +00001003Module functions and attributes
1004===============================
cliechtif81362e2009-07-25 03:44:33 +00001005
1006.. function:: device(number)
1007
Chris Liechti518b0d32015-08-30 02:20:39 +02001008 .. versionchanged:: 3.0 removed, use ``serial.tools.list_ports`` instead
1009
cliechti7c640ed2009-08-02 00:54:51 +00001010
cliechtie3ab3532009-08-05 12:40:38 +00001011.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +00001012
cliechti86844e82009-08-12 00:05:33 +00001013 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +00001014 :param do_not_open: When set to true, the serial port is not opened.
1015 :return: an instance of :class:`Serial` or a compatible object.
1016
1017 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +00001018 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +00001019 to support both, local ports and remote ports. There is also support
Chris Liechti589c92a2015-09-04 23:04:34 +02001020 for other types, see :ref:`URL <URLs>` section.
cliechti7c640ed2009-08-02 00:54:51 +00001021
cliechtid7e7ce22009-08-03 02:01:07 +00001022 The port is not opened when a keyword parameter called *do_not_open* is
1023 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +00001024
1025 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +00001026
cliechti87686242009-08-18 00:58:31 +00001027
cliechtie542b362011-03-18 00:49:16 +00001028.. attribute:: protocol_handler_packages
1029
1030 This attribute is a list of package names (strings) that is searched for
1031 protocol handlers.
1032
1033 e.g. we want to support a URL ``foobar://``. A module
1034 ``my_handlers.protocol_foobar`` is provided by the user::
1035
1036 serial.protocol_handler_packages.append("my_handlers")
1037 s = serial.serial_for_url("foobar://")
1038
1039 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
1040 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
1041 ``PACKAGE`` from this list.
1042
1043 .. versionadded:: 2.6
1044
1045
cliechti25375b52010-07-21 23:27:13 +00001046.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +00001047
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001048 :param sequence: bytes, bytearray or memoryview
cliechti25375b52010-07-21 23:27:13 +00001049 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +00001050
cliechtie542b362011-03-18 00:49:16 +00001051 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +00001052 compatible to Python 2.x and 3.x.
1053
cliechtie542b362011-03-18 00:49:16 +00001054 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +00001055 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +00001056 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +00001057
1058 This function is used internally and in the unit tests.
1059
cliechtie542b362011-03-18 00:49:16 +00001060 .. versionadded:: 2.5
1061
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001062.. function:: iterbytes(sequence)
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001063
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001064 :param sequence: bytes, bytearray or memoryview
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001065 :returns: a generator that yields bytes
1066
1067 Some versions of Python (3.x) would return integers instead of bytes when
Chris Liechti4f988d42017-03-20 23:59:40 +01001068 looping over an instance of ``bytes``. This helper function ensures that
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001069 bytes are returned.
1070
1071 .. versionadded:: 3.0
1072
cliechtie214ff82010-07-21 15:48:47 +00001073
Chris Liechti811bf382015-10-10 00:54:47 +02001074Threading
1075=========
1076
1077.. module:: serial.threaded
1078.. versionadded:: 3.0
1079
1080.. warning:: This implementation is currently in an experimental state. Use
1081 at your own risk.
1082
1083This module provides classes to simplify working with threads and protocols.
1084
1085.. class:: Protocol
1086
Chris Liechti5665d572015-10-13 23:50:01 +02001087 Protocol as used by the :class:`ReaderThread`. This base class provides empty
Chris Liechti811bf382015-10-10 00:54:47 +02001088 implementations of all methods.
1089
1090
1091 .. method:: connection_made(transport)
1092
1093 :param transport: instance used to write to serial port.
1094
1095 Called when reader thread is started.
1096
1097 .. method:: data_received(data)
1098
Chris Liechtif8a09192015-12-20 23:36:38 +01001099 :param bytes data: received bytes
Chris Liechti811bf382015-10-10 00:54:47 +02001100
1101 Called with snippets received from the serial port.
1102
1103 .. method:: connection_lost(exc)
1104
1105 :param exc: Exception if connection was terminated by error else ``None``
1106
1107 Called when the serial port is closed or the reader loop terminated
1108 otherwise.
1109
1110.. class:: Packetizer(Protocol)
1111
1112 Read binary packets from serial port. Packets are expected to be terminated
1113 with a ``TERMINATOR`` byte (null byte by default).
1114
1115 The class also keeps track of the transport.
1116
1117 .. attribute:: TERMINATOR = b'\\0'
1118
1119 .. method:: __init__()
1120
1121 .. method:: connection_made(transport)
1122
Chris Liechtif8a09192015-12-20 23:36:38 +01001123 Stores transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001124
1125 .. method:: connection_lost(exc)
1126
Chris Liechtif8a09192015-12-20 23:36:38 +01001127 Forgets transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001128
1129 .. method:: data_received(data)
1130
Chris Liechtif8a09192015-12-20 23:36:38 +01001131 :param bytes data: partial received data
1132
1133 Buffer received data and search for :attr:`TERMINATOR`, when found,
Chris Liechti811bf382015-10-10 00:54:47 +02001134 call :meth:`handle_packet`.
1135
1136 .. method:: handle_packet(packet)
1137
Chris Liechti1c4c5992016-01-18 20:46:00 +01001138 :param bytes packet: a packet as defined by ``TERMINATOR``
1139
Chris Liechti811bf382015-10-10 00:54:47 +02001140 Process packets - to be overridden by subclassing.
1141
1142
1143.. class:: LineReader(Packetizer)
1144
1145 Read and write (Unicode) lines from/to serial port.
1146 The encoding is applied.
1147
1148
1149 .. attribute:: TERMINATOR = b'\\r\\n'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001150
1151 Line ending.
1152
Chris Liechti811bf382015-10-10 00:54:47 +02001153 .. attribute:: ENCODING = 'utf-8'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001154
1155 Encoding of the send and received data.
1156
Chris Liechti811bf382015-10-10 00:54:47 +02001157 .. attribute:: UNICODE_HANDLING = 'replace'
1158
Chris Liechti1c4c5992016-01-18 20:46:00 +01001159 Unicode error handly policy.
1160
Chris Liechti811bf382015-10-10 00:54:47 +02001161 .. method:: handle_packet(packet)
1162
Chris Liechti1c4c5992016-01-18 20:46:00 +01001163 :param bytes packet: a packet as defined by ``TERMINATOR``
1164
1165 In this case it will be a line, calls :meth:`handle_line` after applying
1166 the :attr:`ENCODING`.
1167
Chris Liechti811bf382015-10-10 00:54:47 +02001168 .. method:: handle_line(line)
1169
Chris Liechtif8a09192015-12-20 23:36:38 +01001170 :param str line: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001171
1172 Process one line - to be overridden by subclassing.
1173
1174 .. method:: write_line(text)
1175
Chris Liechtif8a09192015-12-20 23:36:38 +01001176 :param str text: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001177
Chris Liechtif8a09192015-12-20 23:36:38 +01001178 Write *text* to the transport. *text* is expected to be a Unicode
1179 string and the encoding is applied before sending and also the
1180 :attr:`TERMINATOR` (new line) is appended.
Chris Liechti811bf382015-10-10 00:54:47 +02001181
1182
Chris Liechti5665d572015-10-13 23:50:01 +02001183.. class:: ReaderThread(threading.Thread)
Chris Liechti811bf382015-10-10 00:54:47 +02001184
1185 Implement a serial port read loop and dispatch to a Protocol instance (like
1186 the :class:`asyncio.Protocol`) but do it with threads.
1187
Chris Liechtif8a09192015-12-20 23:36:38 +01001188 Calls to :meth:`close` will close the serial port but it is also possible
1189 to just :meth:`stop` this thread and continue to use the serial port
1190 instance otherwise.
Chris Liechti811bf382015-10-10 00:54:47 +02001191
1192 .. method:: __init__(serial_instance, protocol_factory)
1193
1194 :param serial_instance: serial port instance (opened) to be used.
1195 :param protocol_factory: a callable that returns a Protocol instance
1196
1197 Initialize thread.
1198
Chris Liechtif8a09192015-12-20 23:36:38 +01001199 Note that the ``serial_instance`` 's timeout is set to one second!
Chris Liechti811bf382015-10-10 00:54:47 +02001200 Other settings are not changed.
1201
1202 .. method:: stop()
1203
1204 Stop the reader thread.
1205
1206 .. method:: run()
1207
1208 The actual reader loop driven by the thread. It calls
1209 :meth:`Protocol.connection_made`, reads from the serial port calling
Chris Liechtif8a09192015-12-20 23:36:38 +01001210 :meth:`Protocol.data_received` and finally calls :meth:`Protocol.connection_lost`
Chris Liechti811bf382015-10-10 00:54:47 +02001211 when :meth:`close` is called or an error occurs.
1212
1213 .. method:: write(data)
1214
Chris Liechtif8a09192015-12-20 23:36:38 +01001215 :param bytes data: data to write
1216
Chris Liechti811bf382015-10-10 00:54:47 +02001217 Thread safe writing (uses lock).
1218
1219 .. method:: close()
1220
1221 Close the serial port and exit reader thread, calls :meth:`stop` (uses lock).
1222
1223 .. method:: connect()
1224
1225 Wait until connection is set up and return the transport and protocol
1226 instances.
1227
1228
1229 This class can be used as context manager, in this case it starts
1230 the thread and connects automatically. The serial port is closed
1231 when the context is left.
1232
1233 .. method:: __enter__()
1234
1235 :returns: protocol
1236
1237 Connect and return protocol instance.
1238
1239 .. method:: __exit__(exc_type, exc_val, exc_tb)
1240
1241 Closes serial port.
1242
Chris Liechtia1436b12015-10-12 23:19:04 +02001243Example::
1244
1245 class PrintLines(LineReader):
1246 def connection_made(self, transport):
1247 super(PrintLines, self).connection_made(transport)
1248 sys.stdout.write('port opened\n')
1249 self.write_line('hello world')
1250
1251 def handle_line(self, data):
1252 sys.stdout.write('line received: {}\n'.format(repr(data)))
1253
1254 def connection_lost(self, exc):
1255 if exc:
1256 traceback.print_exc(exc)
1257 sys.stdout.write('port closed\n')
1258
1259 ser = serial.serial_for_url('loop://', baudrate=115200, timeout=1)
Chris Liechti5665d572015-10-13 23:50:01 +02001260 with ReaderThread(ser, PrintLines) as protocol:
Chris Liechtia1436b12015-10-12 23:19:04 +02001261 protocol.write_line('hello')
1262 time.sleep(2)
1263
Chris Liechti811bf382015-10-10 00:54:47 +02001264
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001265asyncio
1266=======
1267
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001268``asyncio`` was introduced with Python 3.4. Experimental support for pySerial
1269is provided via a separate distribution `pyserial-asyncio`_.
Chris Liechti589c92a2015-09-04 23:04:34 +02001270
Jakub Wilk772a6fa2016-12-20 21:59:27 +01001271It is currently under development, see:
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001272
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001273- http://pyserial-asyncio.readthedocs.io/
1274- https://github.com/pyserial/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001275
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001276.. _`pyserial-asyncio`: https://pypi.python.org/pypi/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001277