blob: ebae06045f15a3cfbaead0d45fc2a28908f0f8f8 [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
125 when :attr:`rts`` or :attr:`dtr` are set differently from their
126 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
246 value before opening the serial port, then the value is applied uppon
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
256 value before opening the serial port, then the value is applied uppon
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
518 Here no port argument is given, so it is not opened automatically:
519
520 >>> with serial.Serial() as s:
521 ... s.port = ...
522 ... s.open()
523 ... s.write(b'hello')
524
525
526 .. method:: __exit__(exc_type, exc_val, exc_tb)
527
Chris Liechti4f988d42017-03-20 23:59:40 +0100528 Closes serial port (exceptions are not handled by ``__exit__``).
Chris Liechtic9f89962016-08-12 21:04:37 +0200529
530
cliechti25375b52010-07-21 23:27:13 +0000531 Platform specific methods.
532
cliechtic648da92011-12-29 01:17:51 +0000533 .. warning:: Programs using the following methods and attributes are not
534 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000535
536 .. method:: nonblocking()
537
Chris Liechtice621882015-08-06 19:29:31 +0200538 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000539
Chris Liechti59848422016-06-04 22:28:14 +0200540 .. deprecated:: 3.2
541 The serial port is already opened in this mode. This method is not
542 needed and going away.
543
cliechti25375b52010-07-21 23:27:13 +0000544
545 .. method:: fileno()
546
Chris Liechtice621882015-08-06 19:29:31 +0200547 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000548 :return: File descriptor.
549
550 Return file descriptor number for the port that is opened by this object.
551 It is useful when serial ports are used with :mod:`select`.
552
Chris Liechti518b0d32015-08-30 02:20:39 +0200553 .. method:: set_input_flow_control(enable)
cliechti25375b52010-07-21 23:27:13 +0000554
cliechti2f0f8a32011-12-28 22:10:00 +0000555 :platform: Posix
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100556 :param bool enable: Set flow control state.
cliechti25375b52010-07-21 23:27:13 +0000557
cliechti2f0f8a32011-12-28 22:10:00 +0000558 Manually control flow - when software flow control is enabled.
559
560 This will send XON (true) and XOFF (false) to the other device.
561
Chris Liechti518b0d32015-08-30 02:20:39 +0200562 .. versionadded:: 2.7 (Posix support added)
563 .. versionchanged:: 3.0 renamed from ``flowControlOut``
cliechti2f0f8a32011-12-28 22:10:00 +0000564
Chris Liechti518b0d32015-08-30 02:20:39 +0200565 .. method:: set_output_flow_control(enable)
cliechti2f0f8a32011-12-28 22:10:00 +0000566
Chris Liechti518b0d32015-08-30 02:20:39 +0200567 :platform: Posix (HW and SW flow control)
568 :platform: Windows (SW flow control only)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100569 :param bool enable: Set flow control state.
cliechti2f0f8a32011-12-28 22:10:00 +0000570
571 Manually control flow of outgoing data - when hardware or software flow
572 control is enabled.
573
574 Sending will be suspended when called with ``False`` and enabled when
575 called with ``True``.
576
Chris Liechti518b0d32015-08-30 02:20:39 +0200577 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
578 .. versionchanged:: 3.0 renamed from ``setXON``
Chris Liechti256ea212015-08-29 23:57:00 +0200579
Chris Liechti19688bf2016-05-06 23:48:36 +0200580 .. method:: cancel_read()
Chris Liechti256ea212015-08-29 23:57:00 +0200581
Chris Liechti9d893052016-05-18 22:03:29 +0200582 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200583 :platform: Windows
584
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100585 Cancel a pending read operation from another thread. A blocking
Chris Liechti9d893052016-05-18 22:03:29 +0200586 :meth:`read` call is aborted immediately. :meth:`read` will not report
587 any error but return all data received up to that point (similar to a
588 timeout).
589
590 On Posix a call to `cancel_read()` may cancel a future :meth:`read` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200591
592 .. versionadded:: 3.1
593
594 .. method:: cancel_write()
595
Chris Liechti9d893052016-05-18 22:03:29 +0200596 :platform: Posix
Chris Liechti19688bf2016-05-06 23:48:36 +0200597 :platform: Windows
598
Jakub Wilk772a6fa2016-12-20 21:59:27 +0100599 Cancel a pending write operation from another thread. The
Chris Liechti9d893052016-05-18 22:03:29 +0200600 :meth:`write` method will return immediately (no error indicated).
601 However the OS may still be sending from the buffer, a separate call to
602 :meth:`reset_output_buffer` may be needed.
603
604 On Posix a call to `cancel_write()` may cancel a future :meth:`write` call.
Chris Liechti19688bf2016-05-06 23:48:36 +0200605
606 .. versionadded:: 3.1
Chris Liechti518b0d32015-08-30 02:20:39 +0200607
Chris Liechti9a99cb22015-08-30 22:16:50 +0200608 .. note:: The following members are deprecated and will be removed in a
Chris Liechti518b0d32015-08-30 02:20:39 +0200609 future release.
Chris Liechti256ea212015-08-29 23:57:00 +0200610
611 .. attribute:: portstr
612
Chris Liechti518b0d32015-08-30 02:20:39 +0200613 .. deprecated:: 2.5 use :attr:`name` instead
Chris Liechti256ea212015-08-29 23:57:00 +0200614
615 .. method:: inWaiting()
616
Chris Liechti518b0d32015-08-30 02:20:39 +0200617 .. deprecated:: 3.0 see :attr:`in_waiting`
618
Matthew Westfa58a972016-07-08 14:27:04 -0700619 .. method:: isOpen()
620
621 .. deprecated:: 3.0 see :attr:`is_open`
622
Chris Liechti518b0d32015-08-30 02:20:39 +0200623 .. attribute:: writeTimeout
624
625 .. deprecated:: 3.0 see :attr:`write_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200626
627 .. attribute:: interCharTimeout
628
Chris Liechti518b0d32015-08-30 02:20:39 +0200629 .. deprecated:: 3.0 see :attr:`inter_byte_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200630
631 .. method:: sendBreak(duration=0.25)
632
Chris Liechti518b0d32015-08-30 02:20:39 +0200633 .. deprecated:: 3.0 see :meth:`send_break`
Chris Liechti256ea212015-08-29 23:57:00 +0200634
635 .. method:: flushInput()
636
Chris Liechti518b0d32015-08-30 02:20:39 +0200637 .. deprecated:: 3.0 see :meth:`reset_input_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200638
639 .. method:: flushOutput()
640
Chris Liechti518b0d32015-08-30 02:20:39 +0200641 .. deprecated:: 3.0 see :meth:`reset_output_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200642
643 .. method:: setBreak(level=True)
644
Chris Liechti518b0d32015-08-30 02:20:39 +0200645 .. deprecated:: 3.0 see :attr:`break_condition`
Chris Liechti256ea212015-08-29 23:57:00 +0200646
647 .. method:: setRTS(level=True)
648
Chris Liechti518b0d32015-08-30 02:20:39 +0200649 .. deprecated:: 3.0 see :attr:`rts`
Chris Liechti256ea212015-08-29 23:57:00 +0200650
651 .. method:: setDTR(level=True)
652
Chris Liechti518b0d32015-08-30 02:20:39 +0200653 .. deprecated:: 3.0 see :attr:`dtr`
Chris Liechti256ea212015-08-29 23:57:00 +0200654
655 .. method:: getCTS()
656
Chris Liechti518b0d32015-08-30 02:20:39 +0200657 .. deprecated:: 3.0 see :attr:`cts`
Chris Liechti256ea212015-08-29 23:57:00 +0200658
659 .. method:: getDSR()
660
Chris Liechti518b0d32015-08-30 02:20:39 +0200661 .. deprecated:: 3.0 see :attr:`dsr`
Chris Liechti256ea212015-08-29 23:57:00 +0200662
663 .. method:: getRI()
664
Chris Liechti518b0d32015-08-30 02:20:39 +0200665 .. deprecated:: 3.0 see :attr:`ri`
Chris Liechti256ea212015-08-29 23:57:00 +0200666
667 .. method:: getCD()
668
Chris Liechti518b0d32015-08-30 02:20:39 +0200669 .. deprecated:: 3.0 see :attr:`cd`
Chris Liechti256ea212015-08-29 23:57:00 +0200670
671 .. method:: getSettingsDict()
672
Chris Liechti518b0d32015-08-30 02:20:39 +0200673 .. deprecated:: 3.0 see :meth:`get_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200674
675 .. method:: applySettingsDict(d)
676
Chris Liechti518b0d32015-08-30 02:20:39 +0200677 .. deprecated:: 3.0 see :meth:`apply_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200678
679 .. method:: outWaiting()
680
Chris Liechti518b0d32015-08-30 02:20:39 +0200681 .. deprecated:: 3.0 see :attr:`out_waiting`
Chris Liechti256ea212015-08-29 23:57:00 +0200682
683 .. method:: setXON(level=True)
684
Chris Liechti518b0d32015-08-30 02:20:39 +0200685 .. deprecated:: 3.0 see :meth:`set_output_flow_control`
Chris Liechti256ea212015-08-29 23:57:00 +0200686
687 .. method:: flowControlOut(enable)
688
Chris Liechti518b0d32015-08-30 02:20:39 +0200689 .. deprecated:: 3.0 see :meth:`set_input_flow_control`
cliechtif4566342009-08-04 00:07:19 +0000690
cliechtic648da92011-12-29 01:17:51 +0000691 .. attribute:: rtsToggle
692
693 :platform: Windows
694
695 Attribute to configure RTS toggle control setting. When enabled and
696 supported by OS, RTS will be active when data is available and inactive
697 if no data is available.
698
699 .. versionadded:: 2.6
Chris Liechti518b0d32015-08-30 02:20:39 +0200700 .. versionchanged:: 3.0 (removed, see :attr:`rs485_mode` instead)
cliechtic648da92011-12-29 01:17:51 +0000701
702
cliechtic56e41d2011-08-25 22:06:38 +0000703Implementation detail: some attributes and functions are provided by the
704class :class:`SerialBase` and some by the platform specific class and
705others by the base class mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000706
Chris Liechti4f988d42017-03-20 23:59:40 +0100707
Chris Liechtice621882015-08-06 19:29:31 +0200708RS485 support
709-------------
710The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to
711enable RS485 specific support on some platforms. Currently Windows and Linux
712(only a small number of devices) are supported.
cliechti4a567a02009-07-27 22:09:31 +0000713
Chris Liechtice621882015-08-06 19:29:31 +0200714:attr:`Serial.rs485_mode` needs to be set to an instance of
715:class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature.
cliechti4a567a02009-07-27 22:09:31 +0000716
Chris Liechtice621882015-08-06 19:29:31 +0200717Usage::
cliechti4a567a02009-07-27 22:09:31 +0000718
Chris Liechti11538242016-09-06 22:20:09 +0200719 import serial
720 import serial.rs485
Chris Liechtice621882015-08-06 19:29:31 +0200721 ser = serial.Serial(...)
722 ser.rs485_mode = serial.rs485.RS485Settings(...)
723 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000724
Chris Liechtice621882015-08-06 19:29:31 +0200725There is a subclass :class:`rs485.RS485` available to emulate the RS485 support
Chris Liechti11538242016-09-06 22:20:09 +0200726on regular serial ports (``serial.rs485`` needs to be imported).
cliechti4a567a02009-07-27 22:09:31 +0000727
cliechti4a567a02009-07-27 22:09:31 +0000728
Chris Liechtice621882015-08-06 19:29:31 +0200729.. class:: rs485.RS485Settings
cliechti4a567a02009-07-27 22:09:31 +0000730
Chris Liechtice621882015-08-06 19:29:31 +0200731 A class that holds RS485 specific settings which are supported on
732 some platforms.
cliechti4a567a02009-07-27 22:09:31 +0000733
Chris Liechtice621882015-08-06 19:29:31 +0200734 .. versionadded:: 3.0
cliechti4a567a02009-07-27 22:09:31 +0000735
Chris Liechtice621882015-08-06 19:29:31 +0200736 .. 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 +0000737
Chris Liechtice621882015-08-06 19:29:31 +0200738 :param bool rts_level_for_tx:
739 RTS level for transmission
cliechti4a567a02009-07-27 22:09:31 +0000740
Chris Liechtice621882015-08-06 19:29:31 +0200741 :param bool rts_level_for_rx:
742 RTS level for reception
cliechti4a567a02009-07-27 22:09:31 +0000743
Chris Liechtice621882015-08-06 19:29:31 +0200744 :param bool loopback:
745 When set to ``True`` transmitted data is also received.
cliechti4a567a02009-07-27 22:09:31 +0000746
Chris Liechtice621882015-08-06 19:29:31 +0200747 :param float delay_before_tx:
748 Delay after setting RTS but before transmission starts
749
750 :param float delay_before_rx:
751 Delay after transmission ends and resetting RTS
752
753 .. attribute:: rts_level_for_tx
754
755 RTS level for transmission.
756
757 .. attribute:: rts_level_for_rx
758
759 RTS level for reception.
760
761 .. attribute:: loopback
762
763 When set to ``True`` transmitted data is also received.
764
765 .. attribute:: delay_before_tx
766
767 Delay after setting RTS but before transmission starts (seconds as float).
768
769 .. attribute:: delay_before_rx
770
771 Delay after transmission ends and resetting RTS (seconds as float).
cliechti4a567a02009-07-27 22:09:31 +0000772
773
Chris Liechtice621882015-08-06 19:29:31 +0200774.. class:: rs485.RS485
cliechti4a567a02009-07-27 22:09:31 +0000775
Chris Liechtice621882015-08-06 19:29:31 +0200776 A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS
777 according to the RS485 settings.
cliechti4a567a02009-07-27 22:09:31 +0000778
Chris Liechti518b0d32015-08-30 02:20:39 +0200779 Usage::
780
781 ser = serial.rs485.RS485(...)
782 ser.rs485_mode = serial.rs485.RS485Settings(...)
783 ser.write(b'hello')
784
Chris Liechtice621882015-08-06 19:29:31 +0200785 .. warning:: This may work unreliably on some serial ports (control signals not
786 synchronized or delayed compared to data). Using delays may be unreliable
787 (varying times, larger than expected) as the OS may not support very fine
788 grained delays (no smaller than in the order of tens of milliseconds).
cliechti4a567a02009-07-27 22:09:31 +0000789
Chris Liechtice621882015-08-06 19:29:31 +0200790 .. note:: Some implementations support this natively in the class
791 :class:`Serial`. Better performance can be expected when the native version
792 is used.
cliechti4a567a02009-07-27 22:09:31 +0000793
Chris Liechtice621882015-08-06 19:29:31 +0200794 .. note:: The loopback property is ignored by this implementation. The actual
795 behavior depends on the used hardware.
cliechti4a567a02009-07-27 22:09:31 +0000796
cliechti4a567a02009-07-27 22:09:31 +0000797
cliechtie214ff82010-07-21 15:48:47 +0000798
cliechti7aed8332009-08-05 14:19:31 +0000799:rfc:`2217` Network ports
800-------------------------
801
802.. warning:: This implementation is currently in an experimental state. Use
803 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000804
cliechtiec4ac1b2009-08-02 00:47:21 +0000805.. class:: rfc2217.Serial
806
Chris Liechti2a1befc2015-10-21 17:37:08 +0200807 This implements a :rfc:`2217` compatible client. Port names are :ref:`URL
808 <URLs>` in the form: ``rfc2217://<host>:<port>[?<option>[&<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000809
cliechtiec4ac1b2009-08-02 00:47:21 +0000810 This class API is compatible to :class:`Serial` with a few exceptions:
811
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100812 - ``write_timeout`` is not implemented
cliechtiec4ac1b2009-08-02 00:47:21 +0000813 - The current implementation starts a thread that keeps reading from the
814 (internal) socket. The thread is managed automatically by the
815 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
816 However it may be a problem for user applications that like to use select
817 instead of threads.
818
819 Due to the nature of the network and protocol involved there are a few
820 extra points to keep in mind:
821
822 - All operations have an additional latency time.
823 - Setting control lines (RTS/CTS) needs more time.
824 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
825 cache is updated depends entirely on the server. The server itself may
826 implement a polling at a certain rate and quick changes may be invisible.
827 - The network layer also has buffers. This means that :meth:`flush`,
Chris Liechti518b0d32015-08-30 02:20:39 +0200828 :meth:`reset_input_buffer` and :meth:`reset_output_buffer` may work with
829 additional delay. Likewise :attr:`in_waiting` returns the size of the
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100830 data arrived at the objects internal buffer and excludes any bytes in the
Chris Liechti518b0d32015-08-30 02:20:39 +0200831 network buffers or any server side buffer.
cliechtiec4ac1b2009-08-02 00:47:21 +0000832 - Closing and immediately reopening the same port may fail due to time
833 needed by the server to get ready again.
834
835 Not implemented yet / Possible problems with the implementation:
836
cliechti8611bf42009-08-03 00:34:03 +0000837 - :rfc:`2217` flow control between client and server (objects internal
838 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000839 - No authentication support (servers may not prompt for a password etc.)
840 - No encryption.
841
842 Due to lack of authentication and encryption it is not suitable to use this
843 client for connections across the internet and should only be used in
844 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000845
cliechti7c640ed2009-08-02 00:54:51 +0000846 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000847
cliechti7aed8332009-08-05 14:19:31 +0000848
849.. class:: rfc2217.PortManager
850
851 This class provides helper functions for implementing :rfc:`2217`
852 compatible servers.
853
Chris Liechti518b0d32015-08-30 02:20:39 +0200854 Basically, it implements everything needed for the :rfc:`2217` protocol.
cliechti7aed8332009-08-05 14:19:31 +0000855 It just does not open sockets and read/write to serial ports (though it
856 changes other port settings). The user of this class must take care of the
857 data transmission itself. The reason for that is, that this way, this class
858 supports all programming models such as threads and select.
859
860 Usage examples can be found in the examples where two TCP/IP - serial
861 converters are shown, one using threads (the single port server) and an
862 other using select (the multi port server).
863
864 .. note:: Each new client connection must create a new instance as this
865 object (and the :rfc:`2217` protocol) has internal state.
866
867 .. method:: __init__(serial_port, connection, debug_output=False)
868
869 :param serial_port: a :class:`Serial` instance that is managed.
870 :param connection: an object implementing :meth:`write`, used to write
871 to the network.
cliechti86844e82009-08-12 00:05:33 +0000872 :param debug_output: enables debug messages: a :class:`logging.Logger`
873 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000874
875 Initializes the Manager and starts negotiating with client in Telnet
876 and :rfc:`2217` protocol. The negotiation starts immediately so that
877 the class should be instantiated in the moment the client connects.
878
879 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000880 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000881 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
882 the :meth:`filter` method.
883
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100884 The *connection* object must implement a :meth:`write` function.
cliechti7aed8332009-08-05 14:19:31 +0000885 This function must ensure that *data* is written at once (no user data
886 mixed in, i.e. it must be thread-safe). All data must be sent in its
887 raw form (:meth:`escape` must not be used) as it is used to send Telnet
888 and :rfc:`2217` control commands.
889
cliechti86844e82009-08-12 00:05:33 +0000890 For diagnostics of the connection or the implementation, *debug_output*
891 can be set to an instance of a :class:`logging.Logger` (e.g.
892 ``logging.getLogger('rfc2217.server')``). The caller should configure
893 the logger using ``setLevel`` for the desired detail level of the logs.
894
cliechti7aed8332009-08-05 14:19:31 +0000895 .. method:: escape(data)
896
897 :param data: data to be sent over the network.
898 :return: data, escaped for Telnet/:rfc:`2217`
899
900 A generator that escapes all data to be compatible with :rfc:`2217`.
901 Implementors of servers should use this function to process all data
902 sent over the network.
903
904 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000905 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000906
907 .. method:: filter(data)
908
909 :param data: data read from the network, including Telnet and
910 :rfc:`2217` controls.
911 :return: data, free from Telnet and :rfc:`2217` controls.
912
913 A generator that filters and processes all data related to :rfc:`2217`.
914 Implementors of servers should use this function to process all data
915 received from the network.
916
917 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000918 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000919
cliechti7cb78e82009-08-05 15:47:57 +0000920 .. method:: check_modem_lines(force_notification=False)
921
922 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000923
924 This function needs to be called periodically (e.g. every second) when
925 the server wants to send NOTIFY_MODEMSTATE messages. This is required
926 to support the client for reading CTS/DSR/RI/CD status lines.
927
928 The function reads the status line and issues the notifications
929 automatically.
930
931 .. versionadded:: 2.5
932
cliechtiec4ac1b2009-08-02 00:47:21 +0000933.. seealso::
934
935 :rfc:`2217` - Telnet Com Port Control Option
936
937
cliechtic1c37602009-07-21 01:34:57 +0000938Exceptions
939==========
940
941.. exception:: SerialException
942
943 Base class for serial port exceptions.
944
cliechti4a567a02009-07-27 22:09:31 +0000945 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000946 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000947
cliechtic1c37602009-07-21 01:34:57 +0000948.. exception:: SerialTimeoutException
949
950 Exception that is raised on write timeouts.
951
952
953Constants
954=========
955
cliechti87686242009-08-18 00:58:31 +0000956*Parity*
957
cliechtic1c37602009-07-21 01:34:57 +0000958.. data:: PARITY_NONE
959.. data:: PARITY_EVEN
960.. data:: PARITY_ODD
961.. data:: PARITY_MARK
962.. data:: PARITY_SPACE
963
cliechti87686242009-08-18 00:58:31 +0000964*Stop bits*
965
cliechtic1c37602009-07-21 01:34:57 +0000966.. data:: STOPBITS_ONE
967.. data:: STOPBITS_ONE_POINT_FIVE
968.. data:: STOPBITS_TWO
969
cliechti06281be2011-08-25 23:08:29 +0000970Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +0000971bits.
972
cliechti87686242009-08-18 00:58:31 +0000973*Byte size*
974
cliechtic1c37602009-07-21 01:34:57 +0000975.. data:: FIVEBITS
976.. data:: SIXBITS
977.. data:: SEVENBITS
978.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000979
cliechti87686242009-08-18 00:58:31 +0000980
981*Others*
982
cliechti8611bf42009-08-03 00:34:03 +0000983Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000984software flow control:
cliechti6066f842009-07-24 00:05:45 +0000985
cliechti5b7d16a2009-07-21 21:53:59 +0000986.. data:: XON
987.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000988
cliechti4a567a02009-07-27 22:09:31 +0000989Module version:
cliechtif81362e2009-07-25 03:44:33 +0000990
991.. data:: VERSION
992
Chris Liechti518b0d32015-08-30 02:20:39 +0200993 A string indicating the pySerial version, such as ``3.0``.
cliechtif81362e2009-07-25 03:44:33 +0000994
cliechti87686242009-08-18 00:58:31 +0000995 .. versionadded:: 2.3
996
cliechti44eb98f2011-03-21 21:41:10 +0000997
cliechtie542b362011-03-18 00:49:16 +0000998Module functions and attributes
999===============================
cliechtif81362e2009-07-25 03:44:33 +00001000
1001.. function:: device(number)
1002
Chris Liechti518b0d32015-08-30 02:20:39 +02001003 .. versionchanged:: 3.0 removed, use ``serial.tools.list_ports`` instead
1004
cliechti7c640ed2009-08-02 00:54:51 +00001005
cliechtie3ab3532009-08-05 12:40:38 +00001006.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +00001007
cliechti86844e82009-08-12 00:05:33 +00001008 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +00001009 :param do_not_open: When set to true, the serial port is not opened.
1010 :return: an instance of :class:`Serial` or a compatible object.
1011
1012 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +00001013 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +00001014 to support both, local ports and remote ports. There is also support
Chris Liechti589c92a2015-09-04 23:04:34 +02001015 for other types, see :ref:`URL <URLs>` section.
cliechti7c640ed2009-08-02 00:54:51 +00001016
cliechtid7e7ce22009-08-03 02:01:07 +00001017 The port is not opened when a keyword parameter called *do_not_open* is
1018 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +00001019
1020 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +00001021
cliechti87686242009-08-18 00:58:31 +00001022
cliechtie542b362011-03-18 00:49:16 +00001023.. attribute:: protocol_handler_packages
1024
1025 This attribute is a list of package names (strings) that is searched for
1026 protocol handlers.
1027
1028 e.g. we want to support a URL ``foobar://``. A module
1029 ``my_handlers.protocol_foobar`` is provided by the user::
1030
1031 serial.protocol_handler_packages.append("my_handlers")
1032 s = serial.serial_for_url("foobar://")
1033
1034 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
1035 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
1036 ``PACKAGE`` from this list.
1037
1038 .. versionadded:: 2.6
1039
1040
cliechti25375b52010-07-21 23:27:13 +00001041.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +00001042
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001043 :param sequence: bytes, bytearray or memoryview
cliechti25375b52010-07-21 23:27:13 +00001044 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +00001045
cliechtie542b362011-03-18 00:49:16 +00001046 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +00001047 compatible to Python 2.x and 3.x.
1048
cliechtie542b362011-03-18 00:49:16 +00001049 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +00001050 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +00001051 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +00001052
1053 This function is used internally and in the unit tests.
1054
cliechtie542b362011-03-18 00:49:16 +00001055 .. versionadded:: 2.5
1056
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001057.. function:: iterbytes(sequence)
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001058
Chris Liechti32ccf2e2015-12-19 23:42:40 +01001059 :param sequence: bytes, bytearray or memoryview
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001060 :returns: a generator that yields bytes
1061
1062 Some versions of Python (3.x) would return integers instead of bytes when
Chris Liechti4f988d42017-03-20 23:59:40 +01001063 looping over an instance of ``bytes``. This helper function ensures that
Chris Liechtid14b1ab2015-08-21 00:28:53 +02001064 bytes are returned.
1065
1066 .. versionadded:: 3.0
1067
cliechtie214ff82010-07-21 15:48:47 +00001068
Chris Liechti811bf382015-10-10 00:54:47 +02001069Threading
1070=========
1071
1072.. module:: serial.threaded
1073.. versionadded:: 3.0
1074
1075.. warning:: This implementation is currently in an experimental state. Use
1076 at your own risk.
1077
1078This module provides classes to simplify working with threads and protocols.
1079
1080.. class:: Protocol
1081
Chris Liechti5665d572015-10-13 23:50:01 +02001082 Protocol as used by the :class:`ReaderThread`. This base class provides empty
Chris Liechti811bf382015-10-10 00:54:47 +02001083 implementations of all methods.
1084
1085
1086 .. method:: connection_made(transport)
1087
1088 :param transport: instance used to write to serial port.
1089
1090 Called when reader thread is started.
1091
1092 .. method:: data_received(data)
1093
Chris Liechtif8a09192015-12-20 23:36:38 +01001094 :param bytes data: received bytes
Chris Liechti811bf382015-10-10 00:54:47 +02001095
1096 Called with snippets received from the serial port.
1097
1098 .. method:: connection_lost(exc)
1099
1100 :param exc: Exception if connection was terminated by error else ``None``
1101
1102 Called when the serial port is closed or the reader loop terminated
1103 otherwise.
1104
1105.. class:: Packetizer(Protocol)
1106
1107 Read binary packets from serial port. Packets are expected to be terminated
1108 with a ``TERMINATOR`` byte (null byte by default).
1109
1110 The class also keeps track of the transport.
1111
1112 .. attribute:: TERMINATOR = b'\\0'
1113
1114 .. method:: __init__()
1115
1116 .. method:: connection_made(transport)
1117
Chris Liechtif8a09192015-12-20 23:36:38 +01001118 Stores transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001119
1120 .. method:: connection_lost(exc)
1121
Chris Liechtif8a09192015-12-20 23:36:38 +01001122 Forgets transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001123
1124 .. method:: data_received(data)
1125
Chris Liechtif8a09192015-12-20 23:36:38 +01001126 :param bytes data: partial received data
1127
1128 Buffer received data and search for :attr:`TERMINATOR`, when found,
Chris Liechti811bf382015-10-10 00:54:47 +02001129 call :meth:`handle_packet`.
1130
1131 .. method:: handle_packet(packet)
1132
Chris Liechti1c4c5992016-01-18 20:46:00 +01001133 :param bytes packet: a packet as defined by ``TERMINATOR``
1134
Chris Liechti811bf382015-10-10 00:54:47 +02001135 Process packets - to be overridden by subclassing.
1136
1137
1138.. class:: LineReader(Packetizer)
1139
1140 Read and write (Unicode) lines from/to serial port.
1141 The encoding is applied.
1142
1143
1144 .. attribute:: TERMINATOR = b'\\r\\n'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001145
1146 Line ending.
1147
Chris Liechti811bf382015-10-10 00:54:47 +02001148 .. attribute:: ENCODING = 'utf-8'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001149
1150 Encoding of the send and received data.
1151
Chris Liechti811bf382015-10-10 00:54:47 +02001152 .. attribute:: UNICODE_HANDLING = 'replace'
1153
Chris Liechti1c4c5992016-01-18 20:46:00 +01001154 Unicode error handly policy.
1155
Chris Liechti811bf382015-10-10 00:54:47 +02001156 .. method:: handle_packet(packet)
1157
Chris Liechti1c4c5992016-01-18 20:46:00 +01001158 :param bytes packet: a packet as defined by ``TERMINATOR``
1159
1160 In this case it will be a line, calls :meth:`handle_line` after applying
1161 the :attr:`ENCODING`.
1162
Chris Liechti811bf382015-10-10 00:54:47 +02001163 .. method:: handle_line(line)
1164
Chris Liechtif8a09192015-12-20 23:36:38 +01001165 :param str line: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001166
1167 Process one line - to be overridden by subclassing.
1168
1169 .. method:: write_line(text)
1170
Chris Liechtif8a09192015-12-20 23:36:38 +01001171 :param str text: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001172
Chris Liechtif8a09192015-12-20 23:36:38 +01001173 Write *text* to the transport. *text* is expected to be a Unicode
1174 string and the encoding is applied before sending and also the
1175 :attr:`TERMINATOR` (new line) is appended.
Chris Liechti811bf382015-10-10 00:54:47 +02001176
1177
Chris Liechti5665d572015-10-13 23:50:01 +02001178.. class:: ReaderThread(threading.Thread)
Chris Liechti811bf382015-10-10 00:54:47 +02001179
1180 Implement a serial port read loop and dispatch to a Protocol instance (like
1181 the :class:`asyncio.Protocol`) but do it with threads.
1182
Chris Liechtif8a09192015-12-20 23:36:38 +01001183 Calls to :meth:`close` will close the serial port but it is also possible
1184 to just :meth:`stop` this thread and continue to use the serial port
1185 instance otherwise.
Chris Liechti811bf382015-10-10 00:54:47 +02001186
1187 .. method:: __init__(serial_instance, protocol_factory)
1188
1189 :param serial_instance: serial port instance (opened) to be used.
1190 :param protocol_factory: a callable that returns a Protocol instance
1191
1192 Initialize thread.
1193
Chris Liechtif8a09192015-12-20 23:36:38 +01001194 Note that the ``serial_instance`` 's timeout is set to one second!
Chris Liechti811bf382015-10-10 00:54:47 +02001195 Other settings are not changed.
1196
1197 .. method:: stop()
1198
1199 Stop the reader thread.
1200
1201 .. method:: run()
1202
1203 The actual reader loop driven by the thread. It calls
1204 :meth:`Protocol.connection_made`, reads from the serial port calling
Chris Liechtif8a09192015-12-20 23:36:38 +01001205 :meth:`Protocol.data_received` and finally calls :meth:`Protocol.connection_lost`
Chris Liechti811bf382015-10-10 00:54:47 +02001206 when :meth:`close` is called or an error occurs.
1207
1208 .. method:: write(data)
1209
Chris Liechtif8a09192015-12-20 23:36:38 +01001210 :param bytes data: data to write
1211
Chris Liechti811bf382015-10-10 00:54:47 +02001212 Thread safe writing (uses lock).
1213
1214 .. method:: close()
1215
1216 Close the serial port and exit reader thread, calls :meth:`stop` (uses lock).
1217
1218 .. method:: connect()
1219
1220 Wait until connection is set up and return the transport and protocol
1221 instances.
1222
1223
1224 This class can be used as context manager, in this case it starts
1225 the thread and connects automatically. The serial port is closed
1226 when the context is left.
1227
1228 .. method:: __enter__()
1229
1230 :returns: protocol
1231
1232 Connect and return protocol instance.
1233
1234 .. method:: __exit__(exc_type, exc_val, exc_tb)
1235
1236 Closes serial port.
1237
Chris Liechtia1436b12015-10-12 23:19:04 +02001238Example::
1239
1240 class PrintLines(LineReader):
1241 def connection_made(self, transport):
1242 super(PrintLines, self).connection_made(transport)
1243 sys.stdout.write('port opened\n')
1244 self.write_line('hello world')
1245
1246 def handle_line(self, data):
1247 sys.stdout.write('line received: {}\n'.format(repr(data)))
1248
1249 def connection_lost(self, exc):
1250 if exc:
1251 traceback.print_exc(exc)
1252 sys.stdout.write('port closed\n')
1253
1254 ser = serial.serial_for_url('loop://', baudrate=115200, timeout=1)
Chris Liechti5665d572015-10-13 23:50:01 +02001255 with ReaderThread(ser, PrintLines) as protocol:
Chris Liechtia1436b12015-10-12 23:19:04 +02001256 protocol.write_line('hello')
1257 time.sleep(2)
1258
Chris Liechti811bf382015-10-10 00:54:47 +02001259
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001260asyncio
1261=======
1262
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001263``asyncio`` was introduced with Python 3.4. Experimental support for pySerial
1264is provided via a separate distribution `pyserial-asyncio`_.
Chris Liechti589c92a2015-09-04 23:04:34 +02001265
Jakub Wilk772a6fa2016-12-20 21:59:27 +01001266It is currently under development, see:
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001267
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001268- http://pyserial-asyncio.readthedocs.io/
1269- https://github.com/pyserial/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001270
Chris Liechti2c5a31b2016-06-18 22:57:24 +02001271.. _`pyserial-asyncio`: https://pypi.python.org/pypi/pyserial-asyncio
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001272