blob: 54d2ff3ebdb41a5f6b23a05325fdbca9ca6371bb [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).
Rob Gaddi636cc642017-02-24 11:39:46 -080055
56 :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
cliechti5c72e4d2010-07-21 14:04:54 +0000115
cliechtic1c37602009-07-21 01:34:57 +0000116 .. method:: open()
117
Chris Liechtiaefbf032016-09-30 23:57:22 +0200118 Open port. The state of :attr:`rts` and :attr:`dtr` is applied.
119
Chris Liechti5e953c32016-09-28 18:18:25 +0200120 .. note::
Chris Liechtiaefbf032016-09-30 23:57:22 +0200121
Chris Liechti5e953c32016-09-28 18:18:25 +0200122 Some OS and/or drivers may activate RTS and or DTR automatically,
123 as soon as the port is opened. There may be a glitch on RTS/DTR
124 when :attr:`rts`` or :attr:`dtr` are set differently from their
125 default value (``True`` / active).
cliechtic1c37602009-07-21 01:34:57 +0000126
Chris Liechtiaefbf032016-09-30 23:57:22 +0200127 .. note::
128
129 For compatibility reasons, no error is reported when applying
130 :attr:`rts` or :attr:`dtr` fails on POSIX due to EINVAL (22) or
131 ENOTTY (25).
132
cliechtic1c37602009-07-21 01:34:57 +0000133 .. method:: close()
134
135 Close port immediately.
136
cliechti25375b52010-07-21 23:27:13 +0000137 .. method:: __del__()
138
139 Destructor, close port when serial port instance is freed.
140
cliechtic1c37602009-07-21 01:34:57 +0000141
Chris Liechti256ea212015-08-29 23:57:00 +0200142 The following methods may raise :exc:`SerialException` when applied to a closed
cliechti4a567a02009-07-27 22:09:31 +0000143 port.
cliechtic1c37602009-07-21 01:34:57 +0000144
145 .. method:: read(size=1)
146
cliechtibb5c3c52009-07-23 02:29:27 +0000147 :param size: Number of bytes to read.
148 :return: Bytes read from the port.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100149 :rtype: bytes
cliechtic1c37602009-07-21 01:34:57 +0000150
cliechti4a567a02009-07-27 22:09:31 +0000151 Read *size* bytes from the serial port. If a timeout is set it may
cliechtibb5c3c52009-07-23 02:29:27 +0000152 return less characters as requested. With no timeout it will block
153 until the requested number of bytes is read.
cliechtic1c37602009-07-21 01:34:57 +0000154
cliechti4a567a02009-07-27 22:09:31 +0000155 .. versionchanged:: 2.5
156 Returns an instance of :class:`bytes` when available (Python 2.6
cliechti8611bf42009-08-03 00:34:03 +0000157 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000158
cliechtibb5c3c52009-07-23 02:29:27 +0000159 .. method:: write(data)
160
161 :param data: Data to send.
cliechti4a567a02009-07-27 22:09:31 +0000162 :return: Number of bytes written.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100163 :rtype: int
cliechti6066f842009-07-24 00:05:45 +0000164 :exception SerialTimeoutException:
165 In case a write timeout is configured for the port and the time is
166 exceeded.
167
Chris Liechti9ad044a2015-12-17 19:43:59 +0100168 Write the bytes *data* to the port. This should be of type ``bytes``
169 (or compatible such as ``bytearray`` or ``memoryview``). Unicode
170 strings must be encoded (e.g. ``'hello'.encode('utf-8'``).
cliechtic1c37602009-07-21 01:34:57 +0000171
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100172 .. versionchanged:: 2.5
cliechtiddd78132009-07-28 01:13:28 +0000173 Accepts instances of :class:`bytes` and :class:`bytearray` when
cliechti8611bf42009-08-03 00:34:03 +0000174 available (Python 2.6 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000175
cliechti9a1c6952009-10-20 22:18:28 +0000176 .. versionchanged:: 2.5
177 Write returned ``None`` in previous versions.
178
Chris Liechti518b0d32015-08-30 02:20:39 +0200179 .. method:: flush()
180
181 Flush of file like objects. In this case, wait until all data is
182 written.
183
Chris Liechti256ea212015-08-29 23:57:00 +0200184 .. attribute:: in_waiting
cliechti4a567a02009-07-27 22:09:31 +0000185
Chris Liechti256ea212015-08-29 23:57:00 +0200186 :getter: Get the number of bytes in the input buffer
187 :type: int
188
189 Return the number of bytes in the receive buffer.
cliechti4a567a02009-07-27 22:09:31 +0000190
Chris Liechti518b0d32015-08-30 02:20:39 +0200191 .. versionchanged:: 3.0 changed to property from ``inWaiting()``
cliechtic1c37602009-07-21 01:34:57 +0000192
Chris Liechti518b0d32015-08-30 02:20:39 +0200193 .. attribute:: out_waiting
194
195 :getter: Get the number of bytes in the output buffer
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100196 :type: int
Chris Liechti518b0d32015-08-30 02:20:39 +0200197 :platform: Posix
198 :platform: Windows
199
200 Return the number of bytes in the output buffer.
201
202 .. versionchanged:: 2.7 (Posix support added)
203 .. versionchanged:: 3.0 changed to property from ``outWaiting()``
cliechtic1c37602009-07-21 01:34:57 +0000204
Chris Liechti256ea212015-08-29 23:57:00 +0200205 .. method:: reset_input_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000206
207 Flush input buffer, discarding all it's contents.
208
Chris Liechti518b0d32015-08-30 02:20:39 +0200209 .. versionchanged:: 3.0 renamed from ``flushInput()``
210
Chris Liechti256ea212015-08-29 23:57:00 +0200211 .. method:: reset_output_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000212
213 Clear output buffer, aborting the current output and
214 discarding all that is in the buffer.
215
Chris Liechti48e40e92016-05-24 00:09:12 +0200216 Note, for some USB serial adapters, this may only flush the buffer of
217 the OS and not all the data that may be present in the USB part.
218
Chris Liechti518b0d32015-08-30 02:20:39 +0200219 .. versionchanged:: 3.0 renamed from ``flushOutput()``
220
Chris Liechti256ea212015-08-29 23:57:00 +0200221 .. method:: send_break(duration=0.25)
cliechtic1c37602009-07-21 01:34:57 +0000222
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100223 :param float duration: Time to activate the BREAK condition.
cliechtibb5c3c52009-07-23 02:29:27 +0000224
cliechtic1c37602009-07-21 01:34:57 +0000225 Send break condition. Timed, returns to idle state after given
226 duration.
227
cliechtic1c37602009-07-21 01:34:57 +0000228
Chris Liechti256ea212015-08-29 23:57:00 +0200229 .. attribute:: break_condition
cliechtibb5c3c52009-07-23 02:29:27 +0000230
Chris Liechti256ea212015-08-29 23:57:00 +0200231 :getter: Get the current BREAK state
232 :setter: Control the BREAK state
233 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000234
Chris Liechti256ea212015-08-29 23:57:00 +0200235 When set to ``True`` activate BREAK condition, else disable.
236 Controls TXD. When active, no transmitting is possible.
cliechtic1c37602009-07-21 01:34:57 +0000237
Chris Liechti256ea212015-08-29 23:57:00 +0200238 .. attribute:: rts
cliechtibb5c3c52009-07-23 02:29:27 +0000239
Chris Liechti256ea212015-08-29 23:57:00 +0200240 :setter: Set the state of the RTS line
241 :getter: Return the state of the RTS line
242 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000243
Chris Liechti256ea212015-08-29 23:57:00 +0200244 Set RTS line to specified logic level. It is possible to assign this
245 value before opening the serial port, then the value is applied uppon
Chris Liechti5e953c32016-09-28 18:18:25 +0200246 :meth:`open` (with restrictions, see :meth:`open`).
cliechtic1c37602009-07-21 01:34:57 +0000247
Chris Liechti256ea212015-08-29 23:57:00 +0200248 .. attribute:: dtr
cliechtibb5c3c52009-07-23 02:29:27 +0000249
Chris Liechti256ea212015-08-29 23:57:00 +0200250 :setter: Set the state of the DTR line
251 :getter: Return the state of the DTR line
252 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000253
Chris Liechti256ea212015-08-29 23:57:00 +0200254 Set DTR line to specified logic level. It is possible to assign this
255 value before opening the serial port, then the value is applied uppon
Chris Liechti5e953c32016-09-28 18:18:25 +0200256 :meth:`open` (with restrictions, see :meth:`open`).
cliechtic1c37602009-07-21 01:34:57 +0000257
Chris Liechti518b0d32015-08-30 02:20:39 +0200258 Read-only attributes:
259
260 .. attribute:: name
261
262 :getter: Device name.
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100263 :type: str
Chris Liechti518b0d32015-08-30 02:20:39 +0200264
265 .. versionadded:: 2.5
266
Chris Liechti256ea212015-08-29 23:57:00 +0200267 .. attribute:: cts
268
269 :getter: Get the state of the CTS line
270 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000271
cliechtic1c37602009-07-21 01:34:57 +0000272 Return the state of the CTS line.
273
Chris Liechti256ea212015-08-29 23:57:00 +0200274 .. attribute:: dsr
cliechtic1c37602009-07-21 01:34:57 +0000275
Chris Liechti256ea212015-08-29 23:57:00 +0200276 :getter: Get the state of the DSR line
277 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000278
cliechtic1c37602009-07-21 01:34:57 +0000279 Return the state of the DSR line.
280
Chris Liechti256ea212015-08-29 23:57:00 +0200281 .. attribute:: ri
cliechtic1c37602009-07-21 01:34:57 +0000282
Chris Liechti256ea212015-08-29 23:57:00 +0200283 :getter: Get the state of the RI line
284 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000285
cliechtic1c37602009-07-21 01:34:57 +0000286 Return the state of the RI line.
287
Chris Liechti256ea212015-08-29 23:57:00 +0200288 .. attribute:: cd
cliechtic1c37602009-07-21 01:34:57 +0000289
Chris Liechti256ea212015-08-29 23:57:00 +0200290 :getter: Get the state of the CD line
291 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000292
cliechtic1c37602009-07-21 01:34:57 +0000293 Return the state of the CD line
294
Matthew Westfa58a972016-07-08 14:27:04 -0700295 .. attribute:: is_open
Chris Liechtiaefbf032016-09-30 23:57:22 +0200296
Chris Liechti5e953c32016-09-28 18:18:25 +0200297 :getter: Get the state of the serial port, whether it's open.
298 :type: bool
cliechtif81362e2009-07-25 03:44:33 +0000299
cliechti25375b52010-07-21 23:27:13 +0000300 New values can be assigned to the following attributes (properties), the
301 port will be reconfigured, even if it's opened at that time:
cliechtic1c37602009-07-21 01:34:57 +0000302
cliechtiedcdbe42009-07-22 00:48:34 +0000303
304 .. attribute:: port
305
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100306 :type: str
307
cliechtiedcdbe42009-07-22 00:48:34 +0000308 Read or write port. When the port is already open, it will be closed
309 and reopened with the new setting.
310
311 .. attribute:: baudrate
312
Chris Liechti518b0d32015-08-30 02:20:39 +0200313 :getter: Get current baud rate
314 :setter: Set new baud rate
315 :type: int
316
cliechti6066f842009-07-24 00:05:45 +0000317 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000318
319 .. attribute:: bytesize
320
Chris Liechti518b0d32015-08-30 02:20:39 +0200321 :getter: Get current byte size
322 :setter: Set new byte size. Possible values:
323 :const:`FIVEBITS`, :const:`SIXBITS`, :const:`SEVENBITS`,
324 :const:`EIGHTBITS`
325 :type: int
326
cliechti6066f842009-07-24 00:05:45 +0000327 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000328
329 .. attribute:: parity
330
Chris Liechti518b0d32015-08-30 02:20:39 +0200331 :getter: Get current parity setting
332 :setter: Set new parity mode. Possible values:
333 :const:`PARITY_NONE`, :const:`PARITY_EVEN`, :const:`PARITY_ODD`
334 :const:`PARITY_MARK`, :const:`PARITY_SPACE`
335
cliechti6066f842009-07-24 00:05:45 +0000336 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000337
338 .. attribute:: stopbits
339
Chris Liechti518b0d32015-08-30 02:20:39 +0200340 :getter: Get current stop bit setting
341 :setter: Set new stop bit setting. Possible values:
342 :const:`STOPBITS_ONE`, :const:`STOPBITS_ONE_POINT_FIVE`,
343 :const:`STOPBITS_TWO`
344
cliechti6066f842009-07-24 00:05:45 +0000345 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000346
347 .. attribute:: timeout
348
Chris Liechti518b0d32015-08-30 02:20:39 +0200349 :getter: Get current read timeout setting
350 :setter: Set read timeout
351 :type: float (seconds)
352
cliechti6066f842009-07-24 00:05:45 +0000353 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000354
Chris Liechti518b0d32015-08-30 02:20:39 +0200355 .. attribute:: write_timeout
356
357 :getter: Get current write timeout setting
358 :setter: Set write timeout
359 :type: float (seconds)
cliechtiedcdbe42009-07-22 00:48:34 +0000360
cliechti6066f842009-07-24 00:05:45 +0000361 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000362
Chris Liechti518b0d32015-08-30 02:20:39 +0200363 .. versionchanged:: 3.0 renamed from ``writeTimeout``
364
365 .. attribute:: inter_byte_timeout
366
367 :getter: Get current inter byte timeout setting
368 :setter: Disable (``None``) or enable the inter byte timeout
369 :type: float or None
370
371 Read or write current inter byte timeout setting.
372
373 .. versionchanged:: 3.0 renamed from ``interCharTimeout``
374
cliechtiedcdbe42009-07-22 00:48:34 +0000375 .. attribute:: xonxoff
376
Chris Liechti518b0d32015-08-30 02:20:39 +0200377 :getter: Get current software flow control setting
378 :setter: Enable or disable software flow control
379 :type: bool
380
cliechti6066f842009-07-24 00:05:45 +0000381 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000382
383 .. attribute:: rtscts
384
Chris Liechti518b0d32015-08-30 02:20:39 +0200385 :getter: Get current hardware flow control setting
386 :setter: Enable or disable hardware flow control
387 :type: bool
388
cliechti6066f842009-07-24 00:05:45 +0000389 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000390
391 .. attribute:: dsrdtr
392
Chris Liechti518b0d32015-08-30 02:20:39 +0200393 :getter: Get current hardware flow control setting
394 :setter: Enable or disable hardware flow control
395 :type: bool
396
cliechti6066f842009-07-24 00:05:45 +0000397 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000398
Chris Liechtice621882015-08-06 19:29:31 +0200399 .. attribute:: rs485_mode
400
Chris Liechti518b0d32015-08-30 02:20:39 +0200401 :getter: Get the current RS485 settings
402 :setter: Disable (``None``) or enable the RS485 settings
403 :type: :class:`rs485.RS485Settings` or ``None``
404 :platform: Posix (Linux, limited set of hardware)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100405 :platform: Windows (only RTS on TX possible)
Chris Liechtice621882015-08-06 19:29:31 +0200406
407 Attribute to configure RS485 support. When set to an instance of
408 :class:`rs485.RS485Settings` and supported by OS, RTS will be active
409 when data is sent and inactive otherwise (for reception). The
410 :class:`rs485.RS485Settings` class provides additional settings
411 supported on some platforms.
412
413 .. versionadded:: 3.0
414
415
cliechtiedcdbe42009-07-22 00:48:34 +0000416 The following constants are also provided:
417
418 .. attribute:: BAUDRATES
419
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100420 A list of valid baud rates. The list may be incomplete, such that higher
421 and/or intermediate baud rates may also be supported by the device
422 (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000423
424 .. attribute:: BYTESIZES
425
cliechti25375b52010-07-21 23:27:13 +0000426 A list of valid byte sizes for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000427
428 .. attribute:: PARITIES
429
cliechti25375b52010-07-21 23:27:13 +0000430 A list of valid parities for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000431
432 .. attribute:: STOPBITS
433
cliechti25375b52010-07-21 23:27:13 +0000434 A list of valid stop bit widths for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000435
cliechti4a567a02009-07-27 22:09:31 +0000436
cliechti25375b52010-07-21 23:27:13 +0000437 The following methods are for compatibility with the :mod:`io` library.
cliechti4a567a02009-07-27 22:09:31 +0000438
439 .. method:: readable()
440
441 :return: True
cliechtif4566342009-08-04 00:07:19 +0000442
cliechti4a567a02009-07-27 22:09:31 +0000443 .. versionadded:: 2.5
444
445 .. method:: writable()
446
447 :return: True
cliechtif4566342009-08-04 00:07:19 +0000448
cliechti4a567a02009-07-27 22:09:31 +0000449 .. versionadded:: 2.5
450
451 .. method:: seekable()
452
453 :return: False
cliechtif4566342009-08-04 00:07:19 +0000454
cliechti4a567a02009-07-27 22:09:31 +0000455 .. versionadded:: 2.5
456
457 .. method:: readinto(b)
458
cliechti8611bf42009-08-03 00:34:03 +0000459 :param b: bytearray or array instance
cliechti4a567a02009-07-27 22:09:31 +0000460 :return: Number of byte read
461
cliechtid7e7ce22009-08-03 02:01:07 +0000462 Read up to len(b) bytes into :class:`bytearray` *b* and return the
463 number of bytes read.
cliechti4a567a02009-07-27 22:09:31 +0000464
465 .. versionadded:: 2.5
466
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100467 The port settings can be read and written as dictionary. The following
468 keys are supported: ``write_timeout``, ``inter_byte_timeout``,
469 ``dsrdtr``, ``baudrate``, ``timeout``, ``parity``, ``bytesize``,
470 ``rtscts``, ``stopbits``, ``xonxoff``
cliechti25375b52010-07-21 23:27:13 +0000471
Chris Liechti256ea212015-08-29 23:57:00 +0200472 .. method:: get_settings()
cliechti4065dce2009-08-10 00:55:46 +0000473
474 :return: a dictionary with current port settings.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100475 :rtype: dict
cliechti4065dce2009-08-10 00:55:46 +0000476
477 Get a dictionary with port settings. This is useful to backup the
478 current settings so that a later point in time they can be restored
Chris Liechti518b0d32015-08-30 02:20:39 +0200479 using :meth:`apply_settings`.
cliechti4065dce2009-08-10 00:55:46 +0000480
Chris Liechti19688bf2016-05-06 23:48:36 +0200481 Note that the state of control lines (RTS/DTR) are not part of the
482 settings.
cliechti4065dce2009-08-10 00:55:46 +0000483
484 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100485 .. versionchanged:: 3.0 renamed from ``getSettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000486
Chris Liechti256ea212015-08-29 23:57:00 +0200487 .. method:: apply_settings(d)
cliechti4065dce2009-08-10 00:55:46 +0000488
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100489 :param dict d: a dictionary with port settings.
cliechti4065dce2009-08-10 00:55:46 +0000490
Chris Liechti518b0d32015-08-30 02:20:39 +0200491 Applies a dictionary that was created by :meth:`get_settings`. Only
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100492 changes are applied and when a key is missing, it means that the
493 setting stays unchanged.
cliechti4065dce2009-08-10 00:55:46 +0000494
495 Note that control lines (RTS/DTR) are not changed.
496
497 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100498 .. versionchanged:: 3.0 renamed from ``applySettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000499
Chris Liechtic9f89962016-08-12 21:04:37 +0200500
Chris Liechti9e205742016-10-31 22:37:13 +0100501 .. _context-manager:
502
Chris Liechtic9f89962016-08-12 21:04:37 +0200503 This class can be used as context manager. The serial port is closed when
504 the context is left.
505
506 .. method:: __enter__()
507
508 :returns: Serial instance
509
510 Returns the instance that was used in the ``with`` statement.
511
512 Example:
513
514 >>> with serial.serial_for_url(port) as s:
515 ... s.write(b'hello')
516
517 Here no port argument is given, so it is not opened automatically:
518
519 >>> with serial.Serial() as s:
520 ... s.port = ...
521 ... s.open()
522 ... s.write(b'hello')
523
524
525 .. method:: __exit__(exc_type, exc_val, exc_tb)
526
527 Closes serial port.
528
529
cliechti25375b52010-07-21 23:27:13 +0000530 Platform specific methods.
531
cliechtic648da92011-12-29 01:17:51 +0000532 .. warning:: Programs using the following methods and attributes are not
533 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000534
535 .. method:: nonblocking()
536
Chris Liechtice621882015-08-06 19:29:31 +0200537 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000538
Chris Liechti59848422016-06-04 22:28:14 +0200539 .. deprecated:: 3.2
540 The serial port is already opened in this mode. This method is not
541 needed and going away.
542
cliechti25375b52010-07-21 23:27:13 +0000543
544 .. method:: fileno()
545
Chris Liechtice621882015-08-06 19:29:31 +0200546 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000547 :return: File descriptor.
548
549 Return file descriptor number for the port that is opened by this object.
550 It is useful when serial ports are used with :mod:`select`.
551
Chris Liechti518b0d32015-08-30 02:20:39 +0200552 .. method:: set_input_flow_control(enable)
cliechti25375b52010-07-21 23:27:13 +0000553
cliechti2f0f8a32011-12-28 22:10:00 +0000554 :platform: Posix
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100555 :param bool enable: Set flow control state.
cliechti25375b52010-07-21 23:27:13 +0000556
cliechti2f0f8a32011-12-28 22:10:00 +0000557 Manually control flow - when software flow control is enabled.
558
559 This will send XON (true) and XOFF (false) to the other device.
560
Chris Liechti518b0d32015-08-30 02:20:39 +0200561 .. versionadded:: 2.7 (Posix support added)
562 .. versionchanged:: 3.0 renamed from ``flowControlOut``
cliechti2f0f8a32011-12-28 22:10:00 +0000563
Chris Liechti518b0d32015-08-30 02:20:39 +0200564 .. method:: set_output_flow_control(enable)
cliechti2f0f8a32011-12-28 22:10:00 +0000565
Chris Liechti518b0d32015-08-30 02:20:39 +0200566 :platform: Posix (HW and SW flow control)
567 :platform: Windows (SW flow control only)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100568 :param bool enable: Set flow control state.
cliechti2f0f8a32011-12-28 22:10:00 +0000569
570 Manually control flow of outgoing data - when hardware or software flow
571 control is enabled.
572
573 Sending will be suspended when called with ``False`` and enabled when
574 called with ``True``.
575
Chris Liechti518b0d32015-08-30 02:20:39 +0200576 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
577 .. versionchanged:: 3.0 renamed from ``setXON``
Chris Liechti256ea212015-08-29 23:57:00 +0200578
Chris Liechti19688bf2016-05-06 23:48:36 +0200579 .. method:: cancel_read()
Chris Liechti256ea212015-08-29 23:57:00 +0200580
Chris Liechti9d893052016-05-18 22:03:29 +0200581 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200582 :platform: Windows
583
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100584 Cancel a pending read operation from another thread. A blocking
Chris Liechti9d893052016-05-18 22:03:29 +0200585 :meth:`read` call is aborted immediately. :meth:`read` will not report
586 any error but return all data received up to that point (similar to a
587 timeout).
588
589 On Posix a call to `cancel_read()` may cancel a future :meth:`read` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200590
591 .. versionadded:: 3.1
592
593 .. method:: cancel_write()
594
Chris Liechti9d893052016-05-18 22:03:29 +0200595 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200596 :platform: Windows
597
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100598 Cancel a pending write operation from another thread. The
Chris Liechti9d893052016-05-18 22:03:29 +0200599 :meth:`write` method will return immediately (no error indicated).
600 However the OS may still be sending from the buffer, a separate call to
601 :meth:`reset_output_buffer` may be needed.
602
603 On Posix a call to `cancel_write()` may cancel a future :meth:`write` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200604
605 .. versionadded:: 3.1
Chris Liechti518b0d32015-08-30 02:20:39 +0200606
Chris Liechti9a99cb22015-08-30 22:16:50 +0200607 .. note:: The following members are deprecated and will be removed in a
Chris Liechti518b0d32015-08-30 02:20:39 +0200608 future release.
Chris Liechti256ea212015-08-29 23:57:00 +0200609
610 .. attribute:: portstr
611
Chris Liechti518b0d32015-08-30 02:20:39 +0200612 .. deprecated:: 2.5 use :attr:`name` instead
Chris Liechti256ea212015-08-29 23:57:00 +0200613
614 .. method:: inWaiting()
615
Chris Liechti518b0d32015-08-30 02:20:39 +0200616 .. deprecated:: 3.0 see :attr:`in_waiting`
617
Matthew Westfa58a972016-07-08 14:27:04 -0700618 .. method:: isOpen()
619
620 .. deprecated:: 3.0 see :attr:`is_open`
621
Chris Liechti518b0d32015-08-30 02:20:39 +0200622 .. attribute:: writeTimeout
623
624 .. deprecated:: 3.0 see :attr:`write_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200625
626 .. attribute:: interCharTimeout
627
Chris Liechti518b0d32015-08-30 02:20:39 +0200628 .. deprecated:: 3.0 see :attr:`inter_byte_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200629
630 .. method:: sendBreak(duration=0.25)
631
Chris Liechti518b0d32015-08-30 02:20:39 +0200632 .. deprecated:: 3.0 see :meth:`send_break`
Chris Liechti256ea212015-08-29 23:57:00 +0200633
634 .. method:: flushInput()
635
Chris Liechti518b0d32015-08-30 02:20:39 +0200636 .. deprecated:: 3.0 see :meth:`reset_input_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200637
638 .. method:: flushOutput()
639
Chris Liechti518b0d32015-08-30 02:20:39 +0200640 .. deprecated:: 3.0 see :meth:`reset_output_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200641
642 .. method:: setBreak(level=True)
643
Chris Liechti518b0d32015-08-30 02:20:39 +0200644 .. deprecated:: 3.0 see :attr:`break_condition`
Chris Liechti256ea212015-08-29 23:57:00 +0200645
646 .. method:: setRTS(level=True)
647
Chris Liechti518b0d32015-08-30 02:20:39 +0200648 .. deprecated:: 3.0 see :attr:`rts`
Chris Liechti256ea212015-08-29 23:57:00 +0200649
650 .. method:: setDTR(level=True)
651
Chris Liechti518b0d32015-08-30 02:20:39 +0200652 .. deprecated:: 3.0 see :attr:`dtr`
Chris Liechti256ea212015-08-29 23:57:00 +0200653
654 .. method:: getCTS()
655
Chris Liechti518b0d32015-08-30 02:20:39 +0200656 .. deprecated:: 3.0 see :attr:`cts`
Chris Liechti256ea212015-08-29 23:57:00 +0200657
658 .. method:: getDSR()
659
Chris Liechti518b0d32015-08-30 02:20:39 +0200660 .. deprecated:: 3.0 see :attr:`dsr`
Chris Liechti256ea212015-08-29 23:57:00 +0200661
662 .. method:: getRI()
663
Chris Liechti518b0d32015-08-30 02:20:39 +0200664 .. deprecated:: 3.0 see :attr:`ri`
Chris Liechti256ea212015-08-29 23:57:00 +0200665
666 .. method:: getCD()
667
Chris Liechti518b0d32015-08-30 02:20:39 +0200668 .. deprecated:: 3.0 see :attr:`cd`
Chris Liechti256ea212015-08-29 23:57:00 +0200669
670 .. method:: getSettingsDict()
671
Chris Liechti518b0d32015-08-30 02:20:39 +0200672 .. deprecated:: 3.0 see :meth:`get_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200673
674 .. method:: applySettingsDict(d)
675
Chris Liechti518b0d32015-08-30 02:20:39 +0200676 .. deprecated:: 3.0 see :meth:`apply_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200677
678 .. method:: outWaiting()
679
Chris Liechti518b0d32015-08-30 02:20:39 +0200680 .. deprecated:: 3.0 see :attr:`out_waiting`
Chris Liechti256ea212015-08-29 23:57:00 +0200681
682 .. method:: setXON(level=True)
683
Chris Liechti518b0d32015-08-30 02:20:39 +0200684 .. deprecated:: 3.0 see :meth:`set_output_flow_control`
Chris Liechti256ea212015-08-29 23:57:00 +0200685
686 .. method:: flowControlOut(enable)
687
Chris Liechti518b0d32015-08-30 02:20:39 +0200688 .. deprecated:: 3.0 see :meth:`set_input_flow_control`
cliechtif4566342009-08-04 00:07:19 +0000689
cliechtic648da92011-12-29 01:17:51 +0000690 .. attribute:: rtsToggle
691
692 :platform: Windows
693
694 Attribute to configure RTS toggle control setting. When enabled and
695 supported by OS, RTS will be active when data is available and inactive
696 if no data is available.
697
698 .. versionadded:: 2.6
Chris Liechti518b0d32015-08-30 02:20:39 +0200699 .. versionchanged:: 3.0 (removed, see :attr:`rs485_mode` instead)
cliechtic648da92011-12-29 01:17:51 +0000700
701
cliechtic56e41d2011-08-25 22:06:38 +0000702Implementation detail: some attributes and functions are provided by the
703class :class:`SerialBase` and some by the platform specific class and
704others by the base class mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000705
Chris Liechtice621882015-08-06 19:29:31 +0200706RS485 support
707-------------
708The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to
709enable RS485 specific support on some platforms. Currently Windows and Linux
710(only a small number of devices) are supported.
cliechti4a567a02009-07-27 22:09:31 +0000711
Chris Liechtice621882015-08-06 19:29:31 +0200712:attr:`Serial.rs485_mode` needs to be set to an instance of
713:class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature.
cliechti4a567a02009-07-27 22:09:31 +0000714
Chris Liechtice621882015-08-06 19:29:31 +0200715Usage::
cliechti4a567a02009-07-27 22:09:31 +0000716
Chris Liechti11538242016-09-06 22:20:09 +0200717 import serial
718 import serial.rs485
Chris Liechtice621882015-08-06 19:29:31 +0200719 ser = serial.Serial(...)
720 ser.rs485_mode = serial.rs485.RS485Settings(...)
721 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000722
Chris Liechtice621882015-08-06 19:29:31 +0200723There is a subclass :class:`rs485.RS485` available to emulate the RS485 support
Chris Liechti11538242016-09-06 22:20:09 +0200724on regular serial ports (``serial.rs485`` needs to be imported).
cliechti4a567a02009-07-27 22:09:31 +0000725
cliechti4a567a02009-07-27 22:09:31 +0000726
Chris Liechtice621882015-08-06 19:29:31 +0200727.. class:: rs485.RS485Settings
cliechti4a567a02009-07-27 22:09:31 +0000728
Chris Liechtice621882015-08-06 19:29:31 +0200729 A class that holds RS485 specific settings which are supported on
730 some platforms.
cliechti4a567a02009-07-27 22:09:31 +0000731
Chris Liechtice621882015-08-06 19:29:31 +0200732 .. versionadded:: 3.0
cliechti4a567a02009-07-27 22:09:31 +0000733
Chris Liechtice621882015-08-06 19:29:31 +0200734 .. 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 +0000735
Chris Liechtice621882015-08-06 19:29:31 +0200736 :param bool rts_level_for_tx:
737 RTS level for transmission
cliechti4a567a02009-07-27 22:09:31 +0000738
Chris Liechtice621882015-08-06 19:29:31 +0200739 :param bool rts_level_for_rx:
740 RTS level for reception
cliechti4a567a02009-07-27 22:09:31 +0000741
Chris Liechtice621882015-08-06 19:29:31 +0200742 :param bool loopback:
743 When set to ``True`` transmitted data is also received.
cliechti4a567a02009-07-27 22:09:31 +0000744
Chris Liechtice621882015-08-06 19:29:31 +0200745 :param float delay_before_tx:
746 Delay after setting RTS but before transmission starts
747
748 :param float delay_before_rx:
749 Delay after transmission ends and resetting RTS
750
751 .. attribute:: rts_level_for_tx
752
753 RTS level for transmission.
754
755 .. attribute:: rts_level_for_rx
756
757 RTS level for reception.
758
759 .. attribute:: loopback
760
761 When set to ``True`` transmitted data is also received.
762
763 .. attribute:: delay_before_tx
764
765 Delay after setting RTS but before transmission starts (seconds as float).
766
767 .. attribute:: delay_before_rx
768
769 Delay after transmission ends and resetting RTS (seconds as float).
cliechti4a567a02009-07-27 22:09:31 +0000770
771
Chris Liechtice621882015-08-06 19:29:31 +0200772.. class:: rs485.RS485
cliechti4a567a02009-07-27 22:09:31 +0000773
Chris Liechtice621882015-08-06 19:29:31 +0200774 A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS
775 according to the RS485 settings.
cliechti4a567a02009-07-27 22:09:31 +0000776
Chris Liechti518b0d32015-08-30 02:20:39 +0200777 Usage::
778
779 ser = serial.rs485.RS485(...)
780 ser.rs485_mode = serial.rs485.RS485Settings(...)
781 ser.write(b'hello')
782
Chris Liechtice621882015-08-06 19:29:31 +0200783 .. warning:: This may work unreliably on some serial ports (control signals not
784 synchronized or delayed compared to data). Using delays may be unreliable
785 (varying times, larger than expected) as the OS may not support very fine
786 grained delays (no smaller than in the order of tens of milliseconds).
cliechti4a567a02009-07-27 22:09:31 +0000787
Chris Liechtice621882015-08-06 19:29:31 +0200788 .. note:: Some implementations support this natively in the class
789 :class:`Serial`. Better performance can be expected when the native version
790 is used.
cliechti4a567a02009-07-27 22:09:31 +0000791
Chris Liechtice621882015-08-06 19:29:31 +0200792 .. note:: The loopback property is ignored by this implementation. The actual
793 behavior depends on the used hardware.
cliechti4a567a02009-07-27 22:09:31 +0000794
cliechti4a567a02009-07-27 22:09:31 +0000795
cliechtie214ff82010-07-21 15:48:47 +0000796
cliechti25375b52010-07-21 23:27:13 +0000797
cliechti7aed8332009-08-05 14:19:31 +0000798:rfc:`2217` Network ports
799-------------------------
800
801.. warning:: This implementation is currently in an experimental state. Use
802 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000803
cliechtiec4ac1b2009-08-02 00:47:21 +0000804.. class:: rfc2217.Serial
805
Chris Liechti2a1befc2015-10-21 17:37:08 +0200806 This implements a :rfc:`2217` compatible client. Port names are :ref:`URL
807 <URLs>` in the form: ``rfc2217://<host>:<port>[?<option>[&<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000808
cliechtiec4ac1b2009-08-02 00:47:21 +0000809 This class API is compatible to :class:`Serial` with a few exceptions:
810
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100811 - ``write_timeout`` is not implemented
cliechtiec4ac1b2009-08-02 00:47:21 +0000812 - The current implementation starts a thread that keeps reading from the
813 (internal) socket. The thread is managed automatically by the
814 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
815 However it may be a problem for user applications that like to use select
816 instead of threads.
817
818 Due to the nature of the network and protocol involved there are a few
819 extra points to keep in mind:
820
821 - All operations have an additional latency time.
822 - Setting control lines (RTS/CTS) needs more time.
823 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
824 cache is updated depends entirely on the server. The server itself may
825 implement a polling at a certain rate and quick changes may be invisible.
826 - The network layer also has buffers. This means that :meth:`flush`,
Chris Liechti518b0d32015-08-30 02:20:39 +0200827 :meth:`reset_input_buffer` and :meth:`reset_output_buffer` may work with
828 additional delay. Likewise :attr:`in_waiting` returns the size of the
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100829 data arrived at the objects internal buffer and excludes any bytes in the
Chris Liechti518b0d32015-08-30 02:20:39 +0200830 network buffers or any server side buffer.
cliechtiec4ac1b2009-08-02 00:47:21 +0000831 - Closing and immediately reopening the same port may fail due to time
832 needed by the server to get ready again.
833
834 Not implemented yet / Possible problems with the implementation:
835
cliechti8611bf42009-08-03 00:34:03 +0000836 - :rfc:`2217` flow control between client and server (objects internal
837 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000838 - No authentication support (servers may not prompt for a password etc.)
839 - No encryption.
840
841 Due to lack of authentication and encryption it is not suitable to use this
842 client for connections across the internet and should only be used in
843 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000844
cliechti7c640ed2009-08-02 00:54:51 +0000845 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000846
cliechti7aed8332009-08-05 14:19:31 +0000847
848.. class:: rfc2217.PortManager
849
850 This class provides helper functions for implementing :rfc:`2217`
851 compatible servers.
852
Chris Liechti518b0d32015-08-30 02:20:39 +0200853 Basically, it implements everything needed for the :rfc:`2217` protocol.
cliechti7aed8332009-08-05 14:19:31 +0000854 It just does not open sockets and read/write to serial ports (though it
855 changes other port settings). The user of this class must take care of the
856 data transmission itself. The reason for that is, that this way, this class
857 supports all programming models such as threads and select.
858
859 Usage examples can be found in the examples where two TCP/IP - serial
860 converters are shown, one using threads (the single port server) and an
861 other using select (the multi port server).
862
863 .. note:: Each new client connection must create a new instance as this
864 object (and the :rfc:`2217` protocol) has internal state.
865
866 .. method:: __init__(serial_port, connection, debug_output=False)
867
868 :param serial_port: a :class:`Serial` instance that is managed.
869 :param connection: an object implementing :meth:`write`, used to write
870 to the network.
cliechti86844e82009-08-12 00:05:33 +0000871 :param debug_output: enables debug messages: a :class:`logging.Logger`
872 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000873
874 Initializes the Manager and starts negotiating with client in Telnet
875 and :rfc:`2217` protocol. The negotiation starts immediately so that
876 the class should be instantiated in the moment the client connects.
877
878 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000879 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000880 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
881 the :meth:`filter` method.
882
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100883 The *connection* object must implement a :meth:`write` function.
cliechti7aed8332009-08-05 14:19:31 +0000884 This function must ensure that *data* is written at once (no user data
885 mixed in, i.e. it must be thread-safe). All data must be sent in its
886 raw form (:meth:`escape` must not be used) as it is used to send Telnet
887 and :rfc:`2217` control commands.
888
cliechti86844e82009-08-12 00:05:33 +0000889 For diagnostics of the connection or the implementation, *debug_output*
890 can be set to an instance of a :class:`logging.Logger` (e.g.
891 ``logging.getLogger('rfc2217.server')``). The caller should configure
892 the logger using ``setLevel`` for the desired detail level of the logs.
893
cliechti7aed8332009-08-05 14:19:31 +0000894 .. method:: escape(data)
895
896 :param data: data to be sent over the network.
897 :return: data, escaped for Telnet/:rfc:`2217`
898
899 A generator that escapes all data to be compatible with :rfc:`2217`.
900 Implementors of servers should use this function to process all data
901 sent over the network.
902
903 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000904 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000905
906 .. method:: filter(data)
907
908 :param data: data read from the network, including Telnet and
909 :rfc:`2217` controls.
910 :return: data, free from Telnet and :rfc:`2217` controls.
911
912 A generator that filters and processes all data related to :rfc:`2217`.
913 Implementors of servers should use this function to process all data
914 received from the network.
915
916 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000917 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000918
cliechti7cb78e82009-08-05 15:47:57 +0000919 .. method:: check_modem_lines(force_notification=False)
920
921 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000922
923 This function needs to be called periodically (e.g. every second) when
924 the server wants to send NOTIFY_MODEMSTATE messages. This is required
925 to support the client for reading CTS/DSR/RI/CD status lines.
926
927 The function reads the status line and issues the notifications
928 automatically.
929
930 .. versionadded:: 2.5
931
cliechtiec4ac1b2009-08-02 00:47:21 +0000932.. seealso::
933
934 :rfc:`2217` - Telnet Com Port Control Option
935
936
cliechtic1c37602009-07-21 01:34:57 +0000937Exceptions
938==========
939
940.. exception:: SerialException
941
942 Base class for serial port exceptions.
943
cliechti4a567a02009-07-27 22:09:31 +0000944 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000945 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000946
cliechtic1c37602009-07-21 01:34:57 +0000947.. exception:: SerialTimeoutException
948
949 Exception that is raised on write timeouts.
950
951
952Constants
953=========
954
cliechti87686242009-08-18 00:58:31 +0000955*Parity*
956
cliechtic1c37602009-07-21 01:34:57 +0000957.. data:: PARITY_NONE
958.. data:: PARITY_EVEN
959.. data:: PARITY_ODD
960.. data:: PARITY_MARK
961.. data:: PARITY_SPACE
962
cliechti87686242009-08-18 00:58:31 +0000963*Stop bits*
964
cliechtic1c37602009-07-21 01:34:57 +0000965.. data:: STOPBITS_ONE
966.. data:: STOPBITS_ONE_POINT_FIVE
967.. data:: STOPBITS_TWO
968
cliechti06281be2011-08-25 23:08:29 +0000969Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +0000970bits.
971
cliechti87686242009-08-18 00:58:31 +0000972*Byte size*
973
cliechtic1c37602009-07-21 01:34:57 +0000974.. data:: FIVEBITS
975.. data:: SIXBITS
976.. data:: SEVENBITS
977.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000978
cliechti87686242009-08-18 00:58:31 +0000979
980*Others*
981
cliechti8611bf42009-08-03 00:34:03 +0000982Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000983software flow control:
cliechti6066f842009-07-24 00:05:45 +0000984
cliechti5b7d16a2009-07-21 21:53:59 +0000985.. data:: XON
986.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000987
cliechti4a567a02009-07-27 22:09:31 +0000988Module version:
cliechtif81362e2009-07-25 03:44:33 +0000989
990.. data:: VERSION
991
Chris Liechti518b0d32015-08-30 02:20:39 +0200992 A string indicating the pySerial version, such as ``3.0``.
cliechtif81362e2009-07-25 03:44:33 +0000993
cliechti87686242009-08-18 00:58:31 +0000994 .. versionadded:: 2.3
995
cliechti44eb98f2011-03-21 21:41:10 +0000996
cliechtie542b362011-03-18 00:49:16 +0000997Module functions and attributes
998===============================
cliechtif81362e2009-07-25 03:44:33 +0000999
1000.. function:: device(number)
1001
Chris Liechti518b0d32015-08-30 02:20:39 +02001002 .. versionchanged:: 3.0 removed, use ``serial.tools.list_ports`` instead
1003
cliechti7c640ed2009-08-02 00:54:51 +00001004
cliechtie3ab3532009-08-05 12:40:38 +00001005.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +00001006
cliechti86844e82009-08-12 00:05:33 +00001007 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +00001008 :param do_not_open: When set to true, the serial port is not opened.
1009 :return: an instance of :class:`Serial` or a compatible object.
1010
1011 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +00001012 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +00001013 to support both, local ports and remote ports. There is also support
Chris Liechti589c92a2015-09-04 23:04:34 +02001014 for other types, see :ref:`URL <URLs>` section.
cliechti7c640ed2009-08-02 00:54:51 +00001015
cliechtid7e7ce22009-08-03 02:01:07 +00001016 The port is not opened when a keyword parameter called *do_not_open* is
1017 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +00001018
1019 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +00001020
cliechti87686242009-08-18 00:58:31 +00001021
cliechtie542b362011-03-18 00:49:16 +00001022.. attribute:: protocol_handler_packages
1023
1024 This attribute is a list of package names (strings) that is searched for
1025 protocol handlers.
1026
1027 e.g. we want to support a URL ``foobar://``. A module
1028 ``my_handlers.protocol_foobar`` is provided by the user::
1029
1030 serial.protocol_handler_packages.append("my_handlers")
1031 s = serial.serial_for_url("foobar://")
1032
1033 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
1034 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
1035 ``PACKAGE`` from this list.
1036
1037 .. versionadded:: 2.6
1038
1039
cliechti25375b52010-07-21 23:27:13 +00001040.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +00001041
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001042 :param sequence: bytes, bytearray or memoryview
cliechti25375b52010-07-21 23:27:13 +00001043 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +00001044
cliechtie542b362011-03-18 00:49:16 +00001045 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +00001046 compatible to Python 2.x and 3.x.
1047
cliechtie542b362011-03-18 00:49:16 +00001048 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +00001049 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +00001050 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +00001051
1052 This function is used internally and in the unit tests.
1053
cliechtie542b362011-03-18 00:49:16 +00001054 .. versionadded:: 2.5
1055
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001056.. function:: iterbytes(sequence)
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001057
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001058 :param sequence: bytes, bytearray or memoryview
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001059 :returns: a generator that yields bytes
1060
1061 Some versions of Python (3.x) would return integers instead of bytes when
1062 looping over an instance of ``bytes``. This helper function ensures that
1063 bytes are returned.
1064
1065 .. versionadded:: 3.0
1066
cliechtie214ff82010-07-21 15:48:47 +00001067
Chris Liechti811bf382015-10-10 00:54:47 +02001068Threading
1069=========
1070
1071.. module:: serial.threaded
1072.. versionadded:: 3.0
1073
1074.. warning:: This implementation is currently in an experimental state. Use
1075 at your own risk.
1076
1077This module provides classes to simplify working with threads and protocols.
1078
1079.. class:: Protocol
1080
Chris Liechti5665d572015-10-13 23:50:01 +02001081 Protocol as used by the :class:`ReaderThread`. This base class provides empty
Chris Liechti811bf382015-10-10 00:54:47 +02001082 implementations of all methods.
1083
1084
1085 .. method:: connection_made(transport)
1086
1087 :param transport: instance used to write to serial port.
1088
1089 Called when reader thread is started.
1090
1091 .. method:: data_received(data)
1092
Chris Liechtif8a09192015-12-20 23:36:38 +01001093 :param bytes data: received bytes
Chris Liechti811bf382015-10-10 00:54:47 +02001094
1095 Called with snippets received from the serial port.
1096
1097 .. method:: connection_lost(exc)
1098
1099 :param exc: Exception if connection was terminated by error else ``None``
1100
1101 Called when the serial port is closed or the reader loop terminated
1102 otherwise.
1103
1104.. class:: Packetizer(Protocol)
1105
1106 Read binary packets from serial port. Packets are expected to be terminated
1107 with a ``TERMINATOR`` byte (null byte by default).
1108
1109 The class also keeps track of the transport.
1110
1111 .. attribute:: TERMINATOR = b'\\0'
1112
1113 .. method:: __init__()
1114
1115 .. method:: connection_made(transport)
1116
Chris Liechtif8a09192015-12-20 23:36:38 +01001117 Stores transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001118
1119 .. method:: connection_lost(exc)
1120
Chris Liechtif8a09192015-12-20 23:36:38 +01001121 Forgets transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001122
1123 .. method:: data_received(data)
1124
Chris Liechtif8a09192015-12-20 23:36:38 +01001125 :param bytes data: partial received data
1126
1127 Buffer received data and search for :attr:`TERMINATOR`, when found,
Chris Liechti811bf382015-10-10 00:54:47 +02001128 call :meth:`handle_packet`.
1129
1130 .. method:: handle_packet(packet)
1131
Chris Liechti1c4c5992016-01-18 20:46:00 +01001132 :param bytes packet: a packet as defined by ``TERMINATOR``
1133
Chris Liechti811bf382015-10-10 00:54:47 +02001134 Process packets - to be overridden by subclassing.
1135
1136
1137.. class:: LineReader(Packetizer)
1138
1139 Read and write (Unicode) lines from/to serial port.
1140 The encoding is applied.
1141
1142
1143 .. attribute:: TERMINATOR = b'\\r\\n'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001144
1145 Line ending.
1146
Chris Liechti811bf382015-10-10 00:54:47 +02001147 .. attribute:: ENCODING = 'utf-8'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001148
1149 Encoding of the send and received data.
1150
Chris Liechti811bf382015-10-10 00:54:47 +02001151 .. attribute:: UNICODE_HANDLING = 'replace'
1152
Chris Liechti1c4c5992016-01-18 20:46:00 +01001153 Unicode error handly policy.
1154
Chris Liechti811bf382015-10-10 00:54:47 +02001155 .. method:: handle_packet(packet)
1156
Chris Liechti1c4c5992016-01-18 20:46:00 +01001157 :param bytes packet: a packet as defined by ``TERMINATOR``
1158
1159 In this case it will be a line, calls :meth:`handle_line` after applying
1160 the :attr:`ENCODING`.
1161
Chris Liechti811bf382015-10-10 00:54:47 +02001162 .. method:: handle_line(line)
1163
Chris Liechtif8a09192015-12-20 23:36:38 +01001164 :param str line: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001165
1166 Process one line - to be overridden by subclassing.
1167
1168 .. method:: write_line(text)
1169
Chris Liechtif8a09192015-12-20 23:36:38 +01001170 :param str text: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001171
Chris Liechtif8a09192015-12-20 23:36:38 +01001172 Write *text* to the transport. *text* is expected to be a Unicode
1173 string and the encoding is applied before sending and also the
1174 :attr:`TERMINATOR` (new line) is appended.
Chris Liechti811bf382015-10-10 00:54:47 +02001175
1176
Chris Liechti5665d572015-10-13 23:50:01 +02001177.. class:: ReaderThread(threading.Thread)
Chris Liechti811bf382015-10-10 00:54:47 +02001178
1179 Implement a serial port read loop and dispatch to a Protocol instance (like
1180 the :class:`asyncio.Protocol`) but do it with threads.
1181
Chris Liechtif8a09192015-12-20 23:36:38 +01001182 Calls to :meth:`close` will close the serial port but it is also possible
1183 to just :meth:`stop` this thread and continue to use the serial port
1184 instance otherwise.
Chris Liechti811bf382015-10-10 00:54:47 +02001185
1186 .. method:: __init__(serial_instance, protocol_factory)
1187
1188 :param serial_instance: serial port instance (opened) to be used.
1189 :param protocol_factory: a callable that returns a Protocol instance
1190
1191 Initialize thread.
1192
Chris Liechtif8a09192015-12-20 23:36:38 +01001193 Note that the ``serial_instance`` 's timeout is set to one second!
Chris Liechti811bf382015-10-10 00:54:47 +02001194 Other settings are not changed.
1195
1196 .. method:: stop()
1197
1198 Stop the reader thread.
1199
1200 .. method:: run()
1201
1202 The actual reader loop driven by the thread. It calls
1203 :meth:`Protocol.connection_made`, reads from the serial port calling
Chris Liechtif8a09192015-12-20 23:36:38 +01001204 :meth:`Protocol.data_received` and finally calls :meth:`Protocol.connection_lost`
Chris Liechti811bf382015-10-10 00:54:47 +02001205 when :meth:`close` is called or an error occurs.
1206
1207 .. method:: write(data)
1208
Chris Liechtif8a09192015-12-20 23:36:38 +01001209 :param bytes data: data to write
1210
Chris Liechti811bf382015-10-10 00:54:47 +02001211 Thread safe writing (uses lock).
1212
1213 .. method:: close()
1214
1215 Close the serial port and exit reader thread, calls :meth:`stop` (uses lock).
1216
1217 .. method:: connect()
1218
1219 Wait until connection is set up and return the transport and protocol
1220 instances.
1221
1222
1223 This class can be used as context manager, in this case it starts
1224 the thread and connects automatically. The serial port is closed
1225 when the context is left.
1226
1227 .. method:: __enter__()
1228
1229 :returns: protocol
1230
1231 Connect and return protocol instance.
1232
1233 .. method:: __exit__(exc_type, exc_val, exc_tb)
1234
1235 Closes serial port.
1236
Chris Liechtia1436b12015-10-12 23:19:04 +02001237Example::
1238
1239 class PrintLines(LineReader):
1240 def connection_made(self, transport):
1241 super(PrintLines, self).connection_made(transport)
1242 sys.stdout.write('port opened\n')
1243 self.write_line('hello world')
1244
1245 def handle_line(self, data):
1246 sys.stdout.write('line received: {}\n'.format(repr(data)))
1247
1248 def connection_lost(self, exc):
1249 if exc:
1250 traceback.print_exc(exc)
1251 sys.stdout.write('port closed\n')
1252
1253 ser = serial.serial_for_url('loop://', baudrate=115200, timeout=1)
Chris Liechti5665d572015-10-13 23:50:01 +02001254 with ReaderThread(ser, PrintLines) as protocol:
Chris Liechtia1436b12015-10-12 23:19:04 +02001255 protocol.write_line('hello')
1256 time.sleep(2)
1257
Chris Liechti811bf382015-10-10 00:54:47 +02001258
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001259asyncio
1260=======
1261
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001262``asyncio`` was introduced with Python 3.4. Experimental support for pySerial
1263is provided via a separate distribution `pyserial-asyncio`_.
Chris Liechti589c92a2015-09-04 23:04:34 +02001264
Jakub Wilk772a6fa2016-12-20 21:59:27 +01001265It is currently under development, see:
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001266
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001267- http://pyserial-asyncio.readthedocs.io/
1268- https://github.com/pyserial/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001269
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001270.. _`pyserial-asyncio`: https://pypi.python.org/pypi/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001271