blob: e0101d17d50a829198594428100499d44368ae68 [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
Chris Liechti518b0d32015-08-30 02:20:39 +020015 .. 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)
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).
cliechtic1c37602009-07-21 01:34:57 +000055
cliechti6066f842009-07-24 00:05:45 +000056 :exception ValueError:
cliechti4a567a02009-07-27 22:09:31 +000057 Will be raised when parameter are out of range, e.g. baud rate, data bits.
cliechti6066f842009-07-24 00:05:45 +000058
59 :exception SerialException:
60 In case the device can not be found or can not be configured.
61
62
cliechti4a567a02009-07-27 22:09:31 +000063 The port is immediately opened on object creation, when a *port* is
64 given. It is not opened when *port* is :const:`None` and a successive call
Chris Liechti32ccf2e2015-12-19 23:42:40 +010065 to :meth:`open` is required.
cliechti4a567a02009-07-27 22:09:31 +000066
Chris Liechti518b0d32015-08-30 02:20:39 +020067 *port* is a device name: depending on operating system. e.g.
68 ``/dev/ttyUSB0`` on GNU/Linux or ``COM3`` on Windows.
cliechtic1c37602009-07-21 01:34:57 +000069
cliechti25375b52010-07-21 23:27:13 +000070 The parameter *baudrate* can be one of the standard values:
71 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
72 9600, 19200, 38400, 57600, 115200.
cliechti28b8fd02011-12-28 21:39:42 +000073 These are well supported on all platforms.
74
75 Standard values above 115200, such as: 230400, 460800, 500000, 576000,
76 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000,
77 4000000 also work on many platforms and devices.
cliechti25375b52010-07-21 23:27:13 +000078
cliechti06281be2011-08-25 23:08:29 +000079 Non-standard values are also supported on some platforms (GNU/Linux, MAC
cliechti25375b52010-07-21 23:27:13 +000080 OSX >= Tiger, Windows). Though, even on these platforms some serial
81 ports may reject non-standard values.
82
Chris Liechti3e0dcc72015-12-18 23:23:26 +010083 Possible values for the parameter *timeout* which controls the behavior
84 of :meth:`read`:
cliechti5134aab2009-07-21 19:47:59 +000085
Chris Liechti3e0dcc72015-12-18 23:23:26 +010086 - ``timeout = None``: wait forever / until requested number of bytes
87 are received
88 - ``timeout = 0``: non-blocking mode, return immediately in any case,
89 returning zero or more, up to the requested number of bytes
cliechtif81362e2009-07-25 03:44:33 +000090 - ``timeout = x``: set timeout to ``x`` seconds (float allowed)
Chris Liechti3e0dcc72015-12-18 23:23:26 +010091 returns immediately when the requested number of bytes are available,
92 otherwise wait until the timeout expires and return all bytes that
93 were received until then.
cliechtic1c37602009-07-21 01:34:57 +000094
Chris Liechti3e0dcc72015-12-18 23:23:26 +010095 :meth:`write` is blocking by default, unless *write_timeout* is set.
96 For possible values refer to the list for *timeout* above.
cliechti07709e42010-05-21 00:30:09 +000097
cliechti8611bf42009-08-03 00:34:03 +000098 Note that enabling both flow control methods (*xonxoff* and *rtscts*)
99 together may not be supported. It is common to use one of the methods
100 at once, not both.
cliechtic1c37602009-07-21 01:34:57 +0000101
cliechti07709e42010-05-21 00:30:09 +0000102 *dsrdtr* is not supported by all platforms (silently ignored). Setting
103 it to ``None`` has the effect that its state follows *rtscts*.
104
cliechti25375b52010-07-21 23:27:13 +0000105 Also consider using the function :func:`serial_for_url` instead of
106 creating Serial instances directly.
107
cliechti5c72e4d2010-07-21 14:04:54 +0000108 .. versionchanged:: 2.5
cliechti06281be2011-08-25 23:08:29 +0000109 *dsrdtr* now defaults to ``False`` (instead of *None*)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100110 .. versionchanged:: 3.0 numbers as *port* argument are no longer supported
cliechti5c72e4d2010-07-21 14:04:54 +0000111
cliechtic1c37602009-07-21 01:34:57 +0000112 .. method:: open()
113
114 Open port.
115
116 .. method:: close()
117
118 Close port immediately.
119
cliechti25375b52010-07-21 23:27:13 +0000120 .. method:: __del__()
121
122 Destructor, close port when serial port instance is freed.
123
cliechtic1c37602009-07-21 01:34:57 +0000124
Chris Liechti256ea212015-08-29 23:57:00 +0200125 The following methods may raise :exc:`SerialException` when applied to a closed
cliechti4a567a02009-07-27 22:09:31 +0000126 port.
cliechtic1c37602009-07-21 01:34:57 +0000127
128 .. method:: read(size=1)
129
cliechtibb5c3c52009-07-23 02:29:27 +0000130 :param size: Number of bytes to read.
131 :return: Bytes read from the port.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100132 :rtype: bytes
cliechtic1c37602009-07-21 01:34:57 +0000133
cliechti4a567a02009-07-27 22:09:31 +0000134 Read *size* bytes from the serial port. If a timeout is set it may
cliechtibb5c3c52009-07-23 02:29:27 +0000135 return less characters as requested. With no timeout it will block
136 until the requested number of bytes is read.
cliechtic1c37602009-07-21 01:34:57 +0000137
cliechti4a567a02009-07-27 22:09:31 +0000138 .. versionchanged:: 2.5
139 Returns an instance of :class:`bytes` when available (Python 2.6
cliechti8611bf42009-08-03 00:34:03 +0000140 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000141
cliechtibb5c3c52009-07-23 02:29:27 +0000142 .. method:: write(data)
143
144 :param data: Data to send.
cliechti4a567a02009-07-27 22:09:31 +0000145 :return: Number of bytes written.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100146 :rtype: int
cliechti6066f842009-07-24 00:05:45 +0000147 :exception SerialTimeoutException:
148 In case a write timeout is configured for the port and the time is
149 exceeded.
150
Chris Liechti9ad044a2015-12-17 19:43:59 +0100151 Write the bytes *data* to the port. This should be of type ``bytes``
152 (or compatible such as ``bytearray`` or ``memoryview``). Unicode
153 strings must be encoded (e.g. ``'hello'.encode('utf-8'``).
cliechtic1c37602009-07-21 01:34:57 +0000154
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100155 .. versionchanged:: 2.5
cliechtiddd78132009-07-28 01:13:28 +0000156 Accepts instances of :class:`bytes` and :class:`bytearray` when
cliechti8611bf42009-08-03 00:34:03 +0000157 available (Python 2.6 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000158
cliechti9a1c6952009-10-20 22:18:28 +0000159 .. versionchanged:: 2.5
160 Write returned ``None`` in previous versions.
161
Chris Liechti518b0d32015-08-30 02:20:39 +0200162 .. method:: flush()
163
164 Flush of file like objects. In this case, wait until all data is
165 written.
166
Chris Liechti256ea212015-08-29 23:57:00 +0200167 .. attribute:: in_waiting
cliechti4a567a02009-07-27 22:09:31 +0000168
Chris Liechti256ea212015-08-29 23:57:00 +0200169 :getter: Get the number of bytes in the input buffer
170 :type: int
171
172 Return the number of bytes in the receive buffer.
cliechti4a567a02009-07-27 22:09:31 +0000173
Chris Liechti518b0d32015-08-30 02:20:39 +0200174 .. versionchanged:: 3.0 changed to property from ``inWaiting()``
cliechtic1c37602009-07-21 01:34:57 +0000175
Chris Liechti518b0d32015-08-30 02:20:39 +0200176 .. attribute:: out_waiting
177
178 :getter: Get the number of bytes in the output buffer
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100179 :type: int
Chris Liechti518b0d32015-08-30 02:20:39 +0200180 :platform: Posix
181 :platform: Windows
182
183 Return the number of bytes in the output buffer.
184
185 .. versionchanged:: 2.7 (Posix support added)
186 .. versionchanged:: 3.0 changed to property from ``outWaiting()``
cliechtic1c37602009-07-21 01:34:57 +0000187
Chris Liechti256ea212015-08-29 23:57:00 +0200188 .. method:: reset_input_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000189
190 Flush input buffer, discarding all it's contents.
191
Chris Liechti518b0d32015-08-30 02:20:39 +0200192 .. versionchanged:: 3.0 renamed from ``flushInput()``
193
Chris Liechti256ea212015-08-29 23:57:00 +0200194 .. method:: reset_output_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000195
196 Clear output buffer, aborting the current output and
197 discarding all that is in the buffer.
198
Chris Liechti518b0d32015-08-30 02:20:39 +0200199 .. versionchanged:: 3.0 renamed from ``flushOutput()``
200
Chris Liechti256ea212015-08-29 23:57:00 +0200201 .. method:: send_break(duration=0.25)
cliechtic1c37602009-07-21 01:34:57 +0000202
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100203 :param float duration: Time to activate the BREAK condition.
cliechtibb5c3c52009-07-23 02:29:27 +0000204
cliechtic1c37602009-07-21 01:34:57 +0000205 Send break condition. Timed, returns to idle state after given
206 duration.
207
cliechtic1c37602009-07-21 01:34:57 +0000208
Chris Liechti256ea212015-08-29 23:57:00 +0200209 .. attribute:: break_condition
cliechtibb5c3c52009-07-23 02:29:27 +0000210
Chris Liechti256ea212015-08-29 23:57:00 +0200211 :getter: Get the current BREAK state
212 :setter: Control the BREAK state
213 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000214
Chris Liechti256ea212015-08-29 23:57:00 +0200215 When set to ``True`` activate BREAK condition, else disable.
216 Controls TXD. When active, no transmitting is possible.
cliechtic1c37602009-07-21 01:34:57 +0000217
Chris Liechti256ea212015-08-29 23:57:00 +0200218 .. attribute:: rts
cliechtibb5c3c52009-07-23 02:29:27 +0000219
Chris Liechti256ea212015-08-29 23:57:00 +0200220 :setter: Set the state of the RTS line
221 :getter: Return the state of the RTS line
222 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000223
Chris Liechti256ea212015-08-29 23:57:00 +0200224 Set RTS line to specified logic level. It is possible to assign this
225 value before opening the serial port, then the value is applied uppon
226 :meth:`open`.
cliechtic1c37602009-07-21 01:34:57 +0000227
Chris Liechti256ea212015-08-29 23:57:00 +0200228 .. attribute:: dtr
cliechtibb5c3c52009-07-23 02:29:27 +0000229
Chris Liechti256ea212015-08-29 23:57:00 +0200230 :setter: Set the state of the DTR line
231 :getter: Return the state of the DTR line
232 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000233
Chris Liechti256ea212015-08-29 23:57:00 +0200234 Set DTR line to specified logic level. It is possible to assign this
235 value before opening the serial port, then the value is applied uppon
236 :meth:`open`.
cliechtic1c37602009-07-21 01:34:57 +0000237
Chris Liechti518b0d32015-08-30 02:20:39 +0200238 Read-only attributes:
239
240 .. attribute:: name
241
242 :getter: Device name.
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100243 :type: str
Chris Liechti518b0d32015-08-30 02:20:39 +0200244
245 .. versionadded:: 2.5
246
Chris Liechti256ea212015-08-29 23:57:00 +0200247 .. attribute:: cts
248
249 :getter: Get the state of the CTS line
250 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000251
cliechtic1c37602009-07-21 01:34:57 +0000252 Return the state of the CTS line.
253
Chris Liechti256ea212015-08-29 23:57:00 +0200254 .. attribute:: dsr
cliechtic1c37602009-07-21 01:34:57 +0000255
Chris Liechti256ea212015-08-29 23:57:00 +0200256 :getter: Get the state of the DSR line
257 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000258
cliechtic1c37602009-07-21 01:34:57 +0000259 Return the state of the DSR line.
260
Chris Liechti256ea212015-08-29 23:57:00 +0200261 .. attribute:: ri
cliechtic1c37602009-07-21 01:34:57 +0000262
Chris Liechti256ea212015-08-29 23:57:00 +0200263 :getter: Get the state of the RI line
264 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000265
cliechtic1c37602009-07-21 01:34:57 +0000266 Return the state of the RI line.
267
Chris Liechti256ea212015-08-29 23:57:00 +0200268 .. attribute:: cd
cliechtic1c37602009-07-21 01:34:57 +0000269
Chris Liechti256ea212015-08-29 23:57:00 +0200270 :getter: Get the state of the CD line
271 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000272
cliechtic1c37602009-07-21 01:34:57 +0000273 Return the state of the CD line
274
cliechtif81362e2009-07-25 03:44:33 +0000275
cliechti25375b52010-07-21 23:27:13 +0000276 New values can be assigned to the following attributes (properties), the
277 port will be reconfigured, even if it's opened at that time:
cliechtic1c37602009-07-21 01:34:57 +0000278
cliechtiedcdbe42009-07-22 00:48:34 +0000279
280 .. attribute:: port
281
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100282 :type: str
283
cliechtiedcdbe42009-07-22 00:48:34 +0000284 Read or write port. When the port is already open, it will be closed
285 and reopened with the new setting.
286
287 .. attribute:: baudrate
288
Chris Liechti518b0d32015-08-30 02:20:39 +0200289 :getter: Get current baud rate
290 :setter: Set new baud rate
291 :type: int
292
cliechti6066f842009-07-24 00:05:45 +0000293 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000294
295 .. attribute:: bytesize
296
Chris Liechti518b0d32015-08-30 02:20:39 +0200297 :getter: Get current byte size
298 :setter: Set new byte size. Possible values:
299 :const:`FIVEBITS`, :const:`SIXBITS`, :const:`SEVENBITS`,
300 :const:`EIGHTBITS`
301 :type: int
302
cliechti6066f842009-07-24 00:05:45 +0000303 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000304
305 .. attribute:: parity
306
Chris Liechti518b0d32015-08-30 02:20:39 +0200307 :getter: Get current parity setting
308 :setter: Set new parity mode. Possible values:
309 :const:`PARITY_NONE`, :const:`PARITY_EVEN`, :const:`PARITY_ODD`
310 :const:`PARITY_MARK`, :const:`PARITY_SPACE`
311
cliechti6066f842009-07-24 00:05:45 +0000312 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000313
314 .. attribute:: stopbits
315
Chris Liechti518b0d32015-08-30 02:20:39 +0200316 :getter: Get current stop bit setting
317 :setter: Set new stop bit setting. Possible values:
318 :const:`STOPBITS_ONE`, :const:`STOPBITS_ONE_POINT_FIVE`,
319 :const:`STOPBITS_TWO`
320
cliechti6066f842009-07-24 00:05:45 +0000321 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000322
323 .. attribute:: timeout
324
Chris Liechti518b0d32015-08-30 02:20:39 +0200325 :getter: Get current read timeout setting
326 :setter: Set read timeout
327 :type: float (seconds)
328
cliechti6066f842009-07-24 00:05:45 +0000329 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000330
Chris Liechti518b0d32015-08-30 02:20:39 +0200331 .. attribute:: write_timeout
332
333 :getter: Get current write timeout setting
334 :setter: Set write timeout
335 :type: float (seconds)
cliechtiedcdbe42009-07-22 00:48:34 +0000336
cliechti6066f842009-07-24 00:05:45 +0000337 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000338
Chris Liechti518b0d32015-08-30 02:20:39 +0200339 .. versionchanged:: 3.0 renamed from ``writeTimeout``
340
341 .. attribute:: inter_byte_timeout
342
343 :getter: Get current inter byte timeout setting
344 :setter: Disable (``None``) or enable the inter byte timeout
345 :type: float or None
346
347 Read or write current inter byte timeout setting.
348
349 .. versionchanged:: 3.0 renamed from ``interCharTimeout``
350
cliechtiedcdbe42009-07-22 00:48:34 +0000351 .. attribute:: xonxoff
352
Chris Liechti518b0d32015-08-30 02:20:39 +0200353 :getter: Get current software flow control setting
354 :setter: Enable or disable software flow control
355 :type: bool
356
cliechti6066f842009-07-24 00:05:45 +0000357 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000358
359 .. attribute:: rtscts
360
Chris Liechti518b0d32015-08-30 02:20:39 +0200361 :getter: Get current hardware flow control setting
362 :setter: Enable or disable hardware flow control
363 :type: bool
364
cliechti6066f842009-07-24 00:05:45 +0000365 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000366
367 .. attribute:: dsrdtr
368
Chris Liechti518b0d32015-08-30 02:20:39 +0200369 :getter: Get current hardware flow control setting
370 :setter: Enable or disable hardware flow control
371 :type: bool
372
cliechti6066f842009-07-24 00:05:45 +0000373 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000374
Chris Liechtice621882015-08-06 19:29:31 +0200375 .. attribute:: rs485_mode
376
Chris Liechti518b0d32015-08-30 02:20:39 +0200377 :getter: Get the current RS485 settings
378 :setter: Disable (``None``) or enable the RS485 settings
379 :type: :class:`rs485.RS485Settings` or ``None``
380 :platform: Posix (Linux, limited set of hardware)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100381 :platform: Windows (only RTS on TX possible)
Chris Liechtice621882015-08-06 19:29:31 +0200382
383 Attribute to configure RS485 support. When set to an instance of
384 :class:`rs485.RS485Settings` and supported by OS, RTS will be active
385 when data is sent and inactive otherwise (for reception). The
386 :class:`rs485.RS485Settings` class provides additional settings
387 supported on some platforms.
388
389 .. versionadded:: 3.0
390
391
cliechtiedcdbe42009-07-22 00:48:34 +0000392 The following constants are also provided:
393
394 .. attribute:: BAUDRATES
395
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100396 A list of valid baud rates. The list may be incomplete, such that higher
397 and/or intermediate baud rates may also be supported by the device
398 (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000399
400 .. attribute:: BYTESIZES
401
cliechti25375b52010-07-21 23:27:13 +0000402 A list of valid byte sizes for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000403
404 .. attribute:: PARITIES
405
cliechti25375b52010-07-21 23:27:13 +0000406 A list of valid parities for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000407
408 .. attribute:: STOPBITS
409
cliechti25375b52010-07-21 23:27:13 +0000410 A list of valid stop bit widths for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000411
cliechti4a567a02009-07-27 22:09:31 +0000412
cliechti25375b52010-07-21 23:27:13 +0000413 The following methods are for compatibility with the :mod:`io` library.
cliechti4a567a02009-07-27 22:09:31 +0000414
415 .. method:: readable()
416
417 :return: True
cliechtif4566342009-08-04 00:07:19 +0000418
cliechti4a567a02009-07-27 22:09:31 +0000419 .. versionadded:: 2.5
420
421 .. method:: writable()
422
423 :return: True
cliechtif4566342009-08-04 00:07:19 +0000424
cliechti4a567a02009-07-27 22:09:31 +0000425 .. versionadded:: 2.5
426
427 .. method:: seekable()
428
429 :return: False
cliechtif4566342009-08-04 00:07:19 +0000430
cliechti4a567a02009-07-27 22:09:31 +0000431 .. versionadded:: 2.5
432
433 .. method:: readinto(b)
434
cliechti8611bf42009-08-03 00:34:03 +0000435 :param b: bytearray or array instance
cliechti4a567a02009-07-27 22:09:31 +0000436 :return: Number of byte read
437
cliechtid7e7ce22009-08-03 02:01:07 +0000438 Read up to len(b) bytes into :class:`bytearray` *b* and return the
439 number of bytes read.
cliechti4a567a02009-07-27 22:09:31 +0000440
441 .. versionadded:: 2.5
442
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100443 The port settings can be read and written as dictionary. The following
444 keys are supported: ``write_timeout``, ``inter_byte_timeout``,
445 ``dsrdtr``, ``baudrate``, ``timeout``, ``parity``, ``bytesize``,
446 ``rtscts``, ``stopbits``, ``xonxoff``
cliechti25375b52010-07-21 23:27:13 +0000447
Chris Liechti256ea212015-08-29 23:57:00 +0200448 .. method:: get_settings()
cliechti4065dce2009-08-10 00:55:46 +0000449
450 :return: a dictionary with current port settings.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100451 :rtype: dict
cliechti4065dce2009-08-10 00:55:46 +0000452
453 Get a dictionary with port settings. This is useful to backup the
454 current settings so that a later point in time they can be restored
Chris Liechti518b0d32015-08-30 02:20:39 +0200455 using :meth:`apply_settings`.
cliechti4065dce2009-08-10 00:55:46 +0000456
Chris Liechti19688bf2016-05-06 23:48:36 +0200457 Note that the state of control lines (RTS/DTR) are not part of the
458 settings.
cliechti4065dce2009-08-10 00:55:46 +0000459
460 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100461 .. versionchanged:: 3.0 renamed from ``getSettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000462
Chris Liechti256ea212015-08-29 23:57:00 +0200463 .. method:: apply_settings(d)
cliechti4065dce2009-08-10 00:55:46 +0000464
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100465 :param dict d: a dictionary with port settings.
cliechti4065dce2009-08-10 00:55:46 +0000466
Chris Liechti518b0d32015-08-30 02:20:39 +0200467 Applies a dictionary that was created by :meth:`get_settings`. Only
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100468 changes are applied and when a key is missing, it means that the
469 setting stays unchanged.
cliechti4065dce2009-08-10 00:55:46 +0000470
471 Note that control lines (RTS/DTR) are not changed.
472
473 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100474 .. versionchanged:: 3.0 renamed from ``applySettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000475
cliechti25375b52010-07-21 23:27:13 +0000476 Platform specific methods.
477
cliechtic648da92011-12-29 01:17:51 +0000478 .. warning:: Programs using the following methods and attributes are not
479 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000480
481 .. method:: nonblocking()
482
Chris Liechtice621882015-08-06 19:29:31 +0200483 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000484
485 Configure the device for nonblocking operation. This can be useful if
Chris Liechti518b0d32015-08-30 02:20:39 +0200486 the port is used with :mod:`select`. Note that :attr:`timeout` must
487 also be set to ``0``
cliechti25375b52010-07-21 23:27:13 +0000488
489 .. method:: fileno()
490
Chris Liechtice621882015-08-06 19:29:31 +0200491 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000492 :return: File descriptor.
493
494 Return file descriptor number for the port that is opened by this object.
495 It is useful when serial ports are used with :mod:`select`.
496
Chris Liechti518b0d32015-08-30 02:20:39 +0200497 .. method:: set_input_flow_control(enable)
cliechti25375b52010-07-21 23:27:13 +0000498
cliechti2f0f8a32011-12-28 22:10:00 +0000499 :platform: Posix
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100500 :param bool enable: Set flow control state.
cliechti25375b52010-07-21 23:27:13 +0000501
cliechti2f0f8a32011-12-28 22:10:00 +0000502 Manually control flow - when software flow control is enabled.
503
504 This will send XON (true) and XOFF (false) to the other device.
505
Chris Liechti518b0d32015-08-30 02:20:39 +0200506 .. versionadded:: 2.7 (Posix support added)
507 .. versionchanged:: 3.0 renamed from ``flowControlOut``
cliechti2f0f8a32011-12-28 22:10:00 +0000508
Chris Liechti518b0d32015-08-30 02:20:39 +0200509 .. method:: set_output_flow_control(enable)
cliechti2f0f8a32011-12-28 22:10:00 +0000510
Chris Liechti518b0d32015-08-30 02:20:39 +0200511 :platform: Posix (HW and SW flow control)
512 :platform: Windows (SW flow control only)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100513 :param bool enable: Set flow control state.
cliechti2f0f8a32011-12-28 22:10:00 +0000514
515 Manually control flow of outgoing data - when hardware or software flow
516 control is enabled.
517
518 Sending will be suspended when called with ``False`` and enabled when
519 called with ``True``.
520
Chris Liechti518b0d32015-08-30 02:20:39 +0200521 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
522 .. versionchanged:: 3.0 renamed from ``setXON``
Chris Liechti256ea212015-08-29 23:57:00 +0200523
Chris Liechti19688bf2016-05-06 23:48:36 +0200524 .. method:: cancel_read()
Chris Liechti256ea212015-08-29 23:57:00 +0200525
Chris Liechti19688bf2016-05-06 23:48:36 +0200526 :platform: Windows
527
528 Cancel a pending read operation from an other thread.
529
530 .. versionadded:: 3.1
531
532 .. method:: cancel_write()
533
534 :platform: Windows
535
536 Cancel a pending write operation from an other thread.
537
538 .. versionadded:: 3.1
Chris Liechti518b0d32015-08-30 02:20:39 +0200539
Chris Liechti9a99cb22015-08-30 22:16:50 +0200540 .. note:: The following members are deprecated and will be removed in a
Chris Liechti518b0d32015-08-30 02:20:39 +0200541 future release.
Chris Liechti256ea212015-08-29 23:57:00 +0200542
543 .. attribute:: portstr
544
Chris Liechti518b0d32015-08-30 02:20:39 +0200545 .. deprecated:: 2.5 use :attr:`name` instead
Chris Liechti256ea212015-08-29 23:57:00 +0200546
547 .. method:: inWaiting()
548
Chris Liechti518b0d32015-08-30 02:20:39 +0200549 .. deprecated:: 3.0 see :attr:`in_waiting`
550
551 .. attribute:: writeTimeout
552
553 .. deprecated:: 3.0 see :attr:`write_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200554
555 .. attribute:: interCharTimeout
556
Chris Liechti518b0d32015-08-30 02:20:39 +0200557 .. deprecated:: 3.0 see :attr:`inter_byte_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200558
559 .. method:: sendBreak(duration=0.25)
560
Chris Liechti518b0d32015-08-30 02:20:39 +0200561 .. deprecated:: 3.0 see :meth:`send_break`
Chris Liechti256ea212015-08-29 23:57:00 +0200562
563 .. method:: flushInput()
564
Chris Liechti518b0d32015-08-30 02:20:39 +0200565 .. deprecated:: 3.0 see :meth:`reset_input_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200566
567 .. method:: flushOutput()
568
Chris Liechti518b0d32015-08-30 02:20:39 +0200569 .. deprecated:: 3.0 see :meth:`reset_output_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200570
571 .. method:: setBreak(level=True)
572
Chris Liechti518b0d32015-08-30 02:20:39 +0200573 .. deprecated:: 3.0 see :attr:`break_condition`
Chris Liechti256ea212015-08-29 23:57:00 +0200574
575 .. method:: setRTS(level=True)
576
Chris Liechti518b0d32015-08-30 02:20:39 +0200577 .. deprecated:: 3.0 see :attr:`rts`
Chris Liechti256ea212015-08-29 23:57:00 +0200578
579 .. method:: setDTR(level=True)
580
Chris Liechti518b0d32015-08-30 02:20:39 +0200581 .. deprecated:: 3.0 see :attr:`dtr`
Chris Liechti256ea212015-08-29 23:57:00 +0200582
583 .. method:: getCTS()
584
Chris Liechti518b0d32015-08-30 02:20:39 +0200585 .. deprecated:: 3.0 see :attr:`cts`
Chris Liechti256ea212015-08-29 23:57:00 +0200586
587 .. method:: getDSR()
588
Chris Liechti518b0d32015-08-30 02:20:39 +0200589 .. deprecated:: 3.0 see :attr:`dsr`
Chris Liechti256ea212015-08-29 23:57:00 +0200590
591 .. method:: getRI()
592
Chris Liechti518b0d32015-08-30 02:20:39 +0200593 .. deprecated:: 3.0 see :attr:`ri`
Chris Liechti256ea212015-08-29 23:57:00 +0200594
595 .. method:: getCD()
596
Chris Liechti518b0d32015-08-30 02:20:39 +0200597 .. deprecated:: 3.0 see :attr:`cd`
Chris Liechti256ea212015-08-29 23:57:00 +0200598
599 .. method:: getSettingsDict()
600
Chris Liechti518b0d32015-08-30 02:20:39 +0200601 .. deprecated:: 3.0 see :meth:`get_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200602
603 .. method:: applySettingsDict(d)
604
Chris Liechti518b0d32015-08-30 02:20:39 +0200605 .. deprecated:: 3.0 see :meth:`apply_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200606
607 .. method:: outWaiting()
608
Chris Liechti518b0d32015-08-30 02:20:39 +0200609 .. deprecated:: 3.0 see :attr:`out_waiting`
Chris Liechti256ea212015-08-29 23:57:00 +0200610
611 .. method:: setXON(level=True)
612
Chris Liechti518b0d32015-08-30 02:20:39 +0200613 .. deprecated:: 3.0 see :meth:`set_output_flow_control`
Chris Liechti256ea212015-08-29 23:57:00 +0200614
615 .. method:: flowControlOut(enable)
616
Chris Liechti518b0d32015-08-30 02:20:39 +0200617 .. deprecated:: 3.0 see :meth:`set_input_flow_control`
cliechtif4566342009-08-04 00:07:19 +0000618
cliechtic648da92011-12-29 01:17:51 +0000619 .. attribute:: rtsToggle
620
621 :platform: Windows
622
623 Attribute to configure RTS toggle control setting. When enabled and
624 supported by OS, RTS will be active when data is available and inactive
625 if no data is available.
626
627 .. versionadded:: 2.6
Chris Liechti518b0d32015-08-30 02:20:39 +0200628 .. versionchanged:: 3.0 (removed, see :attr:`rs485_mode` instead)
cliechtic648da92011-12-29 01:17:51 +0000629
630
cliechtic56e41d2011-08-25 22:06:38 +0000631Implementation detail: some attributes and functions are provided by the
632class :class:`SerialBase` and some by the platform specific class and
633others by the base class mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000634
Chris Liechtice621882015-08-06 19:29:31 +0200635RS485 support
636-------------
637The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to
638enable RS485 specific support on some platforms. Currently Windows and Linux
639(only a small number of devices) are supported.
cliechti4a567a02009-07-27 22:09:31 +0000640
Chris Liechtice621882015-08-06 19:29:31 +0200641:attr:`Serial.rs485_mode` needs to be set to an instance of
642:class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature.
cliechti4a567a02009-07-27 22:09:31 +0000643
Chris Liechtice621882015-08-06 19:29:31 +0200644Usage::
cliechti4a567a02009-07-27 22:09:31 +0000645
Chris Liechtice621882015-08-06 19:29:31 +0200646 ser = serial.Serial(...)
647 ser.rs485_mode = serial.rs485.RS485Settings(...)
648 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000649
Chris Liechtice621882015-08-06 19:29:31 +0200650There is a subclass :class:`rs485.RS485` available to emulate the RS485 support
651on regular serial ports.
cliechti4a567a02009-07-27 22:09:31 +0000652
cliechti4a567a02009-07-27 22:09:31 +0000653
Chris Liechtice621882015-08-06 19:29:31 +0200654.. class:: rs485.RS485Settings
cliechti4a567a02009-07-27 22:09:31 +0000655
Chris Liechtice621882015-08-06 19:29:31 +0200656 A class that holds RS485 specific settings which are supported on
657 some platforms.
cliechti4a567a02009-07-27 22:09:31 +0000658
Chris Liechtice621882015-08-06 19:29:31 +0200659 .. versionadded:: 3.0
cliechti4a567a02009-07-27 22:09:31 +0000660
Chris Liechtice621882015-08-06 19:29:31 +0200661 .. 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 +0000662
Chris Liechtice621882015-08-06 19:29:31 +0200663 :param bool rts_level_for_tx:
664 RTS level for transmission
cliechti4a567a02009-07-27 22:09:31 +0000665
Chris Liechtice621882015-08-06 19:29:31 +0200666 :param bool rts_level_for_rx:
667 RTS level for reception
cliechti4a567a02009-07-27 22:09:31 +0000668
Chris Liechtice621882015-08-06 19:29:31 +0200669 :param bool loopback:
670 When set to ``True`` transmitted data is also received.
cliechti4a567a02009-07-27 22:09:31 +0000671
Chris Liechtice621882015-08-06 19:29:31 +0200672 :param float delay_before_tx:
673 Delay after setting RTS but before transmission starts
674
675 :param float delay_before_rx:
676 Delay after transmission ends and resetting RTS
677
678 .. attribute:: rts_level_for_tx
679
680 RTS level for transmission.
681
682 .. attribute:: rts_level_for_rx
683
684 RTS level for reception.
685
686 .. attribute:: loopback
687
688 When set to ``True`` transmitted data is also received.
689
690 .. attribute:: delay_before_tx
691
692 Delay after setting RTS but before transmission starts (seconds as float).
693
694 .. attribute:: delay_before_rx
695
696 Delay after transmission ends and resetting RTS (seconds as float).
cliechti4a567a02009-07-27 22:09:31 +0000697
698
Chris Liechtice621882015-08-06 19:29:31 +0200699.. class:: rs485.RS485
cliechti4a567a02009-07-27 22:09:31 +0000700
Chris Liechtice621882015-08-06 19:29:31 +0200701 A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS
702 according to the RS485 settings.
cliechti4a567a02009-07-27 22:09:31 +0000703
Chris Liechti518b0d32015-08-30 02:20:39 +0200704 Usage::
705
706 ser = serial.rs485.RS485(...)
707 ser.rs485_mode = serial.rs485.RS485Settings(...)
708 ser.write(b'hello')
709
Chris Liechtice621882015-08-06 19:29:31 +0200710 .. warning:: This may work unreliably on some serial ports (control signals not
711 synchronized or delayed compared to data). Using delays may be unreliable
712 (varying times, larger than expected) as the OS may not support very fine
713 grained delays (no smaller than in the order of tens of milliseconds).
cliechti4a567a02009-07-27 22:09:31 +0000714
Chris Liechtice621882015-08-06 19:29:31 +0200715 .. note:: Some implementations support this natively in the class
716 :class:`Serial`. Better performance can be expected when the native version
717 is used.
cliechti4a567a02009-07-27 22:09:31 +0000718
Chris Liechtice621882015-08-06 19:29:31 +0200719 .. note:: The loopback property is ignored by this implementation. The actual
720 behavior depends on the used hardware.
cliechti4a567a02009-07-27 22:09:31 +0000721
cliechti4a567a02009-07-27 22:09:31 +0000722
cliechtie214ff82010-07-21 15:48:47 +0000723
cliechti25375b52010-07-21 23:27:13 +0000724
cliechti7aed8332009-08-05 14:19:31 +0000725:rfc:`2217` Network ports
726-------------------------
727
728.. warning:: This implementation is currently in an experimental state. Use
729 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000730
cliechtiec4ac1b2009-08-02 00:47:21 +0000731.. class:: rfc2217.Serial
732
Chris Liechti2a1befc2015-10-21 17:37:08 +0200733 This implements a :rfc:`2217` compatible client. Port names are :ref:`URL
734 <URLs>` in the form: ``rfc2217://<host>:<port>[?<option>[&<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000735
cliechtiec4ac1b2009-08-02 00:47:21 +0000736 This class API is compatible to :class:`Serial` with a few exceptions:
737
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100738 - ``write_timeout`` is not implemented
cliechtiec4ac1b2009-08-02 00:47:21 +0000739 - The current implementation starts a thread that keeps reading from the
740 (internal) socket. The thread is managed automatically by the
741 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
742 However it may be a problem for user applications that like to use select
743 instead of threads.
744
745 Due to the nature of the network and protocol involved there are a few
746 extra points to keep in mind:
747
748 - All operations have an additional latency time.
749 - Setting control lines (RTS/CTS) needs more time.
750 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
751 cache is updated depends entirely on the server. The server itself may
752 implement a polling at a certain rate and quick changes may be invisible.
753 - The network layer also has buffers. This means that :meth:`flush`,
Chris Liechti518b0d32015-08-30 02:20:39 +0200754 :meth:`reset_input_buffer` and :meth:`reset_output_buffer` may work with
755 additional delay. Likewise :attr:`in_waiting` returns the size of the
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100756 data arrived at the objects internal buffer and excludes any bytes in the
Chris Liechti518b0d32015-08-30 02:20:39 +0200757 network buffers or any server side buffer.
cliechtiec4ac1b2009-08-02 00:47:21 +0000758 - Closing and immediately reopening the same port may fail due to time
759 needed by the server to get ready again.
760
761 Not implemented yet / Possible problems with the implementation:
762
cliechti8611bf42009-08-03 00:34:03 +0000763 - :rfc:`2217` flow control between client and server (objects internal
764 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000765 - No authentication support (servers may not prompt for a password etc.)
766 - No encryption.
767
768 Due to lack of authentication and encryption it is not suitable to use this
769 client for connections across the internet and should only be used in
770 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000771
cliechti7c640ed2009-08-02 00:54:51 +0000772 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000773
cliechti7aed8332009-08-05 14:19:31 +0000774
775.. class:: rfc2217.PortManager
776
777 This class provides helper functions for implementing :rfc:`2217`
778 compatible servers.
779
Chris Liechti518b0d32015-08-30 02:20:39 +0200780 Basically, it implements everything needed for the :rfc:`2217` protocol.
cliechti7aed8332009-08-05 14:19:31 +0000781 It just does not open sockets and read/write to serial ports (though it
782 changes other port settings). The user of this class must take care of the
783 data transmission itself. The reason for that is, that this way, this class
784 supports all programming models such as threads and select.
785
786 Usage examples can be found in the examples where two TCP/IP - serial
787 converters are shown, one using threads (the single port server) and an
788 other using select (the multi port server).
789
790 .. note:: Each new client connection must create a new instance as this
791 object (and the :rfc:`2217` protocol) has internal state.
792
793 .. method:: __init__(serial_port, connection, debug_output=False)
794
795 :param serial_port: a :class:`Serial` instance that is managed.
796 :param connection: an object implementing :meth:`write`, used to write
797 to the network.
cliechti86844e82009-08-12 00:05:33 +0000798 :param debug_output: enables debug messages: a :class:`logging.Logger`
799 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000800
801 Initializes the Manager and starts negotiating with client in Telnet
802 and :rfc:`2217` protocol. The negotiation starts immediately so that
803 the class should be instantiated in the moment the client connects.
804
805 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000806 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000807 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
808 the :meth:`filter` method.
809
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100810 The *connection* object must implement a :meth:`write` function.
cliechti7aed8332009-08-05 14:19:31 +0000811 This function must ensure that *data* is written at once (no user data
812 mixed in, i.e. it must be thread-safe). All data must be sent in its
813 raw form (:meth:`escape` must not be used) as it is used to send Telnet
814 and :rfc:`2217` control commands.
815
cliechti86844e82009-08-12 00:05:33 +0000816 For diagnostics of the connection or the implementation, *debug_output*
817 can be set to an instance of a :class:`logging.Logger` (e.g.
818 ``logging.getLogger('rfc2217.server')``). The caller should configure
819 the logger using ``setLevel`` for the desired detail level of the logs.
820
cliechti7aed8332009-08-05 14:19:31 +0000821 .. method:: escape(data)
822
823 :param data: data to be sent over the network.
824 :return: data, escaped for Telnet/:rfc:`2217`
825
826 A generator that escapes all data to be compatible with :rfc:`2217`.
827 Implementors of servers should use this function to process all data
828 sent over the network.
829
830 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000831 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000832
833 .. method:: filter(data)
834
835 :param data: data read from the network, including Telnet and
836 :rfc:`2217` controls.
837 :return: data, free from Telnet and :rfc:`2217` controls.
838
839 A generator that filters and processes all data related to :rfc:`2217`.
840 Implementors of servers should use this function to process all data
841 received from the network.
842
843 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000844 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000845
cliechti7cb78e82009-08-05 15:47:57 +0000846 .. method:: check_modem_lines(force_notification=False)
847
848 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000849
850 This function needs to be called periodically (e.g. every second) when
851 the server wants to send NOTIFY_MODEMSTATE messages. This is required
852 to support the client for reading CTS/DSR/RI/CD status lines.
853
854 The function reads the status line and issues the notifications
855 automatically.
856
857 .. versionadded:: 2.5
858
cliechtiec4ac1b2009-08-02 00:47:21 +0000859.. seealso::
860
861 :rfc:`2217` - Telnet Com Port Control Option
862
863
cliechtic1c37602009-07-21 01:34:57 +0000864Exceptions
865==========
866
867.. exception:: SerialException
868
869 Base class for serial port exceptions.
870
cliechti4a567a02009-07-27 22:09:31 +0000871 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000872 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000873
cliechtic1c37602009-07-21 01:34:57 +0000874.. exception:: SerialTimeoutException
875
876 Exception that is raised on write timeouts.
877
878
879Constants
880=========
881
cliechti87686242009-08-18 00:58:31 +0000882*Parity*
883
cliechtic1c37602009-07-21 01:34:57 +0000884.. data:: PARITY_NONE
885.. data:: PARITY_EVEN
886.. data:: PARITY_ODD
887.. data:: PARITY_MARK
888.. data:: PARITY_SPACE
889
cliechti87686242009-08-18 00:58:31 +0000890*Stop bits*
891
cliechtic1c37602009-07-21 01:34:57 +0000892.. data:: STOPBITS_ONE
893.. data:: STOPBITS_ONE_POINT_FIVE
894.. data:: STOPBITS_TWO
895
cliechti06281be2011-08-25 23:08:29 +0000896Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +0000897bits.
898
cliechti87686242009-08-18 00:58:31 +0000899*Byte size*
900
cliechtic1c37602009-07-21 01:34:57 +0000901.. data:: FIVEBITS
902.. data:: SIXBITS
903.. data:: SEVENBITS
904.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000905
cliechti87686242009-08-18 00:58:31 +0000906
907*Others*
908
cliechti8611bf42009-08-03 00:34:03 +0000909Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000910software flow control:
cliechti6066f842009-07-24 00:05:45 +0000911
cliechti5b7d16a2009-07-21 21:53:59 +0000912.. data:: XON
913.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000914
cliechti4a567a02009-07-27 22:09:31 +0000915Module version:
cliechtif81362e2009-07-25 03:44:33 +0000916
917.. data:: VERSION
918
Chris Liechti518b0d32015-08-30 02:20:39 +0200919 A string indicating the pySerial version, such as ``3.0``.
cliechtif81362e2009-07-25 03:44:33 +0000920
cliechti87686242009-08-18 00:58:31 +0000921 .. versionadded:: 2.3
922
cliechti44eb98f2011-03-21 21:41:10 +0000923
cliechtie542b362011-03-18 00:49:16 +0000924Module functions and attributes
925===============================
cliechtif81362e2009-07-25 03:44:33 +0000926
927.. function:: device(number)
928
Chris Liechti518b0d32015-08-30 02:20:39 +0200929 .. versionchanged:: 3.0 removed, use ``serial.tools.list_ports`` instead
930
cliechti7c640ed2009-08-02 00:54:51 +0000931
cliechtie3ab3532009-08-05 12:40:38 +0000932.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +0000933
cliechti86844e82009-08-12 00:05:33 +0000934 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +0000935 :param do_not_open: When set to true, the serial port is not opened.
936 :return: an instance of :class:`Serial` or a compatible object.
937
938 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +0000939 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +0000940 to support both, local ports and remote ports. There is also support
Chris Liechti589c92a2015-09-04 23:04:34 +0200941 for other types, see :ref:`URL <URLs>` section.
cliechti7c640ed2009-08-02 00:54:51 +0000942
cliechtid7e7ce22009-08-03 02:01:07 +0000943 The port is not opened when a keyword parameter called *do_not_open* is
944 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +0000945
946 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +0000947
cliechti87686242009-08-18 00:58:31 +0000948
cliechtie542b362011-03-18 00:49:16 +0000949.. attribute:: protocol_handler_packages
950
951 This attribute is a list of package names (strings) that is searched for
952 protocol handlers.
953
954 e.g. we want to support a URL ``foobar://``. A module
955 ``my_handlers.protocol_foobar`` is provided by the user::
956
957 serial.protocol_handler_packages.append("my_handlers")
958 s = serial.serial_for_url("foobar://")
959
960 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
961 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
962 ``PACKAGE`` from this list.
963
964 .. versionadded:: 2.6
965
966
cliechti25375b52010-07-21 23:27:13 +0000967.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +0000968
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100969 :param sequence: bytes, bytearray or memoryview
cliechti25375b52010-07-21 23:27:13 +0000970 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +0000971
cliechtie542b362011-03-18 00:49:16 +0000972 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +0000973 compatible to Python 2.x and 3.x.
974
cliechtie542b362011-03-18 00:49:16 +0000975 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +0000976 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +0000977 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +0000978
979 This function is used internally and in the unit tests.
980
cliechtie542b362011-03-18 00:49:16 +0000981 .. versionadded:: 2.5
982
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100983.. function:: iterbytes(sequence)
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200984
Chris Liechti32ccf2e2015-12-19 23:42:40 +0100985 :param sequence: bytes, bytearray or memoryview
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200986 :returns: a generator that yields bytes
987
988 Some versions of Python (3.x) would return integers instead of bytes when
989 looping over an instance of ``bytes``. This helper function ensures that
990 bytes are returned.
991
992 .. versionadded:: 3.0
993
cliechtie214ff82010-07-21 15:48:47 +0000994
Chris Liechti811bf382015-10-10 00:54:47 +0200995Threading
996=========
997
998.. module:: serial.threaded
999.. versionadded:: 3.0
1000
1001.. warning:: This implementation is currently in an experimental state. Use
1002 at your own risk.
1003
1004This module provides classes to simplify working with threads and protocols.
1005
1006.. class:: Protocol
1007
Chris Liechti5665d572015-10-13 23:50:01 +02001008 Protocol as used by the :class:`ReaderThread`. This base class provides empty
Chris Liechti811bf382015-10-10 00:54:47 +02001009 implementations of all methods.
1010
1011
1012 .. method:: connection_made(transport)
1013
1014 :param transport: instance used to write to serial port.
1015
1016 Called when reader thread is started.
1017
1018 .. method:: data_received(data)
1019
Chris Liechtif8a09192015-12-20 23:36:38 +01001020 :param bytes data: received bytes
Chris Liechti811bf382015-10-10 00:54:47 +02001021
1022 Called with snippets received from the serial port.
1023
1024 .. method:: connection_lost(exc)
1025
1026 :param exc: Exception if connection was terminated by error else ``None``
1027
1028 Called when the serial port is closed or the reader loop terminated
1029 otherwise.
1030
1031.. class:: Packetizer(Protocol)
1032
1033 Read binary packets from serial port. Packets are expected to be terminated
1034 with a ``TERMINATOR`` byte (null byte by default).
1035
1036 The class also keeps track of the transport.
1037
1038 .. attribute:: TERMINATOR = b'\\0'
1039
1040 .. method:: __init__()
1041
1042 .. method:: connection_made(transport)
1043
Chris Liechtif8a09192015-12-20 23:36:38 +01001044 Stores transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001045
1046 .. method:: connection_lost(exc)
1047
Chris Liechtif8a09192015-12-20 23:36:38 +01001048 Forgets transport.
Chris Liechti811bf382015-10-10 00:54:47 +02001049
1050 .. method:: data_received(data)
1051
Chris Liechtif8a09192015-12-20 23:36:38 +01001052 :param bytes data: partial received data
1053
1054 Buffer received data and search for :attr:`TERMINATOR`, when found,
Chris Liechti811bf382015-10-10 00:54:47 +02001055 call :meth:`handle_packet`.
1056
1057 .. method:: handle_packet(packet)
1058
Chris Liechti1c4c5992016-01-18 20:46:00 +01001059 :param bytes packet: a packet as defined by ``TERMINATOR``
1060
Chris Liechti811bf382015-10-10 00:54:47 +02001061 Process packets - to be overridden by subclassing.
1062
1063
1064.. class:: LineReader(Packetizer)
1065
1066 Read and write (Unicode) lines from/to serial port.
1067 The encoding is applied.
1068
1069
1070 .. attribute:: TERMINATOR = b'\\r\\n'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001071
1072 Line ending.
1073
Chris Liechti811bf382015-10-10 00:54:47 +02001074 .. attribute:: ENCODING = 'utf-8'
Chris Liechti1c4c5992016-01-18 20:46:00 +01001075
1076 Encoding of the send and received data.
1077
Chris Liechti811bf382015-10-10 00:54:47 +02001078 .. attribute:: UNICODE_HANDLING = 'replace'
1079
Chris Liechti1c4c5992016-01-18 20:46:00 +01001080 Unicode error handly policy.
1081
Chris Liechti811bf382015-10-10 00:54:47 +02001082 .. method:: handle_packet(packet)
1083
Chris Liechti1c4c5992016-01-18 20:46:00 +01001084 :param bytes packet: a packet as defined by ``TERMINATOR``
1085
1086 In this case it will be a line, calls :meth:`handle_line` after applying
1087 the :attr:`ENCODING`.
1088
Chris Liechti811bf382015-10-10 00:54:47 +02001089 .. method:: handle_line(line)
1090
Chris Liechtif8a09192015-12-20 23:36:38 +01001091 :param str line: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001092
1093 Process one line - to be overridden by subclassing.
1094
1095 .. method:: write_line(text)
1096
Chris Liechtif8a09192015-12-20 23:36:38 +01001097 :param str text: Unicode string with one line (excluding line terminator)
Chris Liechti811bf382015-10-10 00:54:47 +02001098
Chris Liechtif8a09192015-12-20 23:36:38 +01001099 Write *text* to the transport. *text* is expected to be a Unicode
1100 string and the encoding is applied before sending and also the
1101 :attr:`TERMINATOR` (new line) is appended.
Chris Liechti811bf382015-10-10 00:54:47 +02001102
1103
Chris Liechti5665d572015-10-13 23:50:01 +02001104.. class:: ReaderThread(threading.Thread)
Chris Liechti811bf382015-10-10 00:54:47 +02001105
1106 Implement a serial port read loop and dispatch to a Protocol instance (like
1107 the :class:`asyncio.Protocol`) but do it with threads.
1108
Chris Liechtif8a09192015-12-20 23:36:38 +01001109 Calls to :meth:`close` will close the serial port but it is also possible
1110 to just :meth:`stop` this thread and continue to use the serial port
1111 instance otherwise.
Chris Liechti811bf382015-10-10 00:54:47 +02001112
1113 .. method:: __init__(serial_instance, protocol_factory)
1114
1115 :param serial_instance: serial port instance (opened) to be used.
1116 :param protocol_factory: a callable that returns a Protocol instance
1117
1118 Initialize thread.
1119
Chris Liechtif8a09192015-12-20 23:36:38 +01001120 Note that the ``serial_instance`` 's timeout is set to one second!
Chris Liechti811bf382015-10-10 00:54:47 +02001121 Other settings are not changed.
1122
1123 .. method:: stop()
1124
1125 Stop the reader thread.
1126
1127 .. method:: run()
1128
1129 The actual reader loop driven by the thread. It calls
1130 :meth:`Protocol.connection_made`, reads from the serial port calling
Chris Liechtif8a09192015-12-20 23:36:38 +01001131 :meth:`Protocol.data_received` and finally calls :meth:`Protocol.connection_lost`
Chris Liechti811bf382015-10-10 00:54:47 +02001132 when :meth:`close` is called or an error occurs.
1133
1134 .. method:: write(data)
1135
Chris Liechtif8a09192015-12-20 23:36:38 +01001136 :param bytes data: data to write
1137
Chris Liechti811bf382015-10-10 00:54:47 +02001138 Thread safe writing (uses lock).
1139
1140 .. method:: close()
1141
1142 Close the serial port and exit reader thread, calls :meth:`stop` (uses lock).
1143
1144 .. method:: connect()
1145
1146 Wait until connection is set up and return the transport and protocol
1147 instances.
1148
1149
1150 This class can be used as context manager, in this case it starts
1151 the thread and connects automatically. The serial port is closed
1152 when the context is left.
1153
1154 .. method:: __enter__()
1155
1156 :returns: protocol
1157
1158 Connect and return protocol instance.
1159
1160 .. method:: __exit__(exc_type, exc_val, exc_tb)
1161
1162 Closes serial port.
1163
Chris Liechtia1436b12015-10-12 23:19:04 +02001164Example::
1165
1166 class PrintLines(LineReader):
1167 def connection_made(self, transport):
1168 super(PrintLines, self).connection_made(transport)
1169 sys.stdout.write('port opened\n')
1170 self.write_line('hello world')
1171
1172 def handle_line(self, data):
1173 sys.stdout.write('line received: {}\n'.format(repr(data)))
1174
1175 def connection_lost(self, exc):
1176 if exc:
1177 traceback.print_exc(exc)
1178 sys.stdout.write('port closed\n')
1179
1180 ser = serial.serial_for_url('loop://', baudrate=115200, timeout=1)
Chris Liechti5665d572015-10-13 23:50:01 +02001181 with ReaderThread(ser, PrintLines) as protocol:
Chris Liechtia1436b12015-10-12 23:19:04 +02001182 protocol.write_line('hello')
1183 time.sleep(2)
1184
Chris Liechti811bf382015-10-10 00:54:47 +02001185
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001186asyncio
1187=======
1188
Chris Liechti589c92a2015-09-04 23:04:34 +02001189.. module:: serial.aio
1190
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001191.. warning:: This implementation is currently in an experimental state. Use
1192 at your own risk.
1193
Chris Liechti589c92a2015-09-04 23:04:34 +02001194Experimental asyncio support is available for Python 3.4 and newer. The module
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001195:mod:`serial.aio` provides a :class:`asyncio.Transport`:
1196``SerialTransport``.
1197
1198
1199A factory function (`asyncio.coroutine`) is provided:
1200
1201.. function:: create_serial_connection(loop, protocol_factory, \*args, \*\*kwargs)
1202
1203 :param loop: The event handler
1204 :param protocol_factory: Factory function for a :class:`asyncio.Protocol`
1205 :param args: Passed to the :class:`serial.Serial` init function
1206 :param kwargs: Passed to the :class:`serial.Serial` init function
1207 :platform: Posix
1208
1209 Get a connection making coroutine.
1210
1211Example::
1212
1213 class Output(asyncio.Protocol):
1214 def connection_made(self, transport):
1215 self.transport = transport
1216 print('port opened', transport)
Chris Liechti589c92a2015-09-04 23:04:34 +02001217 transport.serial.rts = False
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001218 transport.write(b'hello world\n')
1219
1220 def data_received(self, data):
1221 print('data received', repr(data))
1222 self.transport.close()
1223
1224 def connection_lost(self, exc):
1225 print('port closed')
1226 asyncio.get_event_loop().stop()
1227
1228 loop = asyncio.get_event_loop()
Chris Liechti589c92a2015-09-04 23:04:34 +02001229 coro = serial.aio.create_serial_connection(loop, Output, '/dev/ttyUSB0', baudrate=115200)
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001230 loop.run_until_complete(coro)
1231 loop.run_forever()
1232 loop.close()
1233