blob: 9063ef25f49c21eb8e74886d77d6f7a2022ac007 [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
65 to :meth:`open` will be needed.
66
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
179 :platform: Posix
180 :platform: Windows
181
182 Return the number of bytes in the output buffer.
183
184 .. versionchanged:: 2.7 (Posix support added)
185 .. versionchanged:: 3.0 changed to property from ``outWaiting()``
cliechtic1c37602009-07-21 01:34:57 +0000186
Chris Liechti256ea212015-08-29 23:57:00 +0200187 .. method:: reset_input_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000188
189 Flush input buffer, discarding all it's contents.
190
Chris Liechti518b0d32015-08-30 02:20:39 +0200191 .. versionchanged:: 3.0 renamed from ``flushInput()``
192
Chris Liechti256ea212015-08-29 23:57:00 +0200193 .. method:: reset_output_buffer()
cliechtic1c37602009-07-21 01:34:57 +0000194
195 Clear output buffer, aborting the current output and
196 discarding all that is in the buffer.
197
Chris Liechti518b0d32015-08-30 02:20:39 +0200198 .. versionchanged:: 3.0 renamed from ``flushOutput()``
199
Chris Liechti256ea212015-08-29 23:57:00 +0200200 .. method:: send_break(duration=0.25)
cliechtic1c37602009-07-21 01:34:57 +0000201
cliechtibb5c3c52009-07-23 02:29:27 +0000202 :param duration: Time (float) to activate the BREAK condition.
203
cliechtic1c37602009-07-21 01:34:57 +0000204 Send break condition. Timed, returns to idle state after given
205 duration.
206
cliechtic1c37602009-07-21 01:34:57 +0000207
Chris Liechti256ea212015-08-29 23:57:00 +0200208 .. attribute:: break_condition
cliechtibb5c3c52009-07-23 02:29:27 +0000209
Chris Liechti256ea212015-08-29 23:57:00 +0200210 :getter: Get the current BREAK state
211 :setter: Control the BREAK state
212 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000213
Chris Liechti256ea212015-08-29 23:57:00 +0200214 When set to ``True`` activate BREAK condition, else disable.
215 Controls TXD. When active, no transmitting is possible.
cliechtic1c37602009-07-21 01:34:57 +0000216
Chris Liechti256ea212015-08-29 23:57:00 +0200217 .. attribute:: rts
cliechtibb5c3c52009-07-23 02:29:27 +0000218
Chris Liechti256ea212015-08-29 23:57:00 +0200219 :setter: Set the state of the RTS line
220 :getter: Return the state of the RTS line
221 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000222
Chris Liechti256ea212015-08-29 23:57:00 +0200223 Set RTS line to specified logic level. It is possible to assign this
224 value before opening the serial port, then the value is applied uppon
225 :meth:`open`.
cliechtic1c37602009-07-21 01:34:57 +0000226
Chris Liechti256ea212015-08-29 23:57:00 +0200227 .. attribute:: dtr
cliechtibb5c3c52009-07-23 02:29:27 +0000228
Chris Liechti256ea212015-08-29 23:57:00 +0200229 :setter: Set the state of the DTR line
230 :getter: Return the state of the DTR line
231 :type: bool
cliechtic1c37602009-07-21 01:34:57 +0000232
Chris Liechti256ea212015-08-29 23:57:00 +0200233 Set DTR line to specified logic level. It is possible to assign this
234 value before opening the serial port, then the value is applied uppon
235 :meth:`open`.
cliechtic1c37602009-07-21 01:34:57 +0000236
Chris Liechti518b0d32015-08-30 02:20:39 +0200237 Read-only attributes:
238
239 .. attribute:: name
240
241 :getter: Device name.
242
243 .. versionadded:: 2.5
244
Chris Liechti256ea212015-08-29 23:57:00 +0200245 .. attribute:: cts
246
247 :getter: Get the state of the CTS line
248 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000249
cliechtic1c37602009-07-21 01:34:57 +0000250 Return the state of the CTS line.
251
Chris Liechti256ea212015-08-29 23:57:00 +0200252 .. attribute:: dsr
cliechtic1c37602009-07-21 01:34:57 +0000253
Chris Liechti256ea212015-08-29 23:57:00 +0200254 :getter: Get the state of the DSR line
255 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000256
cliechtic1c37602009-07-21 01:34:57 +0000257 Return the state of the DSR line.
258
Chris Liechti256ea212015-08-29 23:57:00 +0200259 .. attribute:: ri
cliechtic1c37602009-07-21 01:34:57 +0000260
Chris Liechti256ea212015-08-29 23:57:00 +0200261 :getter: Get the state of the RI line
262 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000263
cliechtic1c37602009-07-21 01:34:57 +0000264 Return the state of the RI line.
265
Chris Liechti256ea212015-08-29 23:57:00 +0200266 .. attribute:: cd
cliechtic1c37602009-07-21 01:34:57 +0000267
Chris Liechti256ea212015-08-29 23:57:00 +0200268 :getter: Get the state of the CD line
269 :type: bool
cliechtibb5c3c52009-07-23 02:29:27 +0000270
cliechtic1c37602009-07-21 01:34:57 +0000271 Return the state of the CD line
272
cliechtif81362e2009-07-25 03:44:33 +0000273
cliechti25375b52010-07-21 23:27:13 +0000274 New values can be assigned to the following attributes (properties), the
275 port will be reconfigured, even if it's opened at that time:
cliechtic1c37602009-07-21 01:34:57 +0000276
cliechtiedcdbe42009-07-22 00:48:34 +0000277
278 .. attribute:: port
279
280 Read or write port. When the port is already open, it will be closed
281 and reopened with the new setting.
282
283 .. attribute:: baudrate
284
Chris Liechti518b0d32015-08-30 02:20:39 +0200285 :getter: Get current baud rate
286 :setter: Set new baud rate
287 :type: int
288
cliechti6066f842009-07-24 00:05:45 +0000289 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000290
291 .. attribute:: bytesize
292
Chris Liechti518b0d32015-08-30 02:20:39 +0200293 :getter: Get current byte size
294 :setter: Set new byte size. Possible values:
295 :const:`FIVEBITS`, :const:`SIXBITS`, :const:`SEVENBITS`,
296 :const:`EIGHTBITS`
297 :type: int
298
cliechti6066f842009-07-24 00:05:45 +0000299 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000300
301 .. attribute:: parity
302
Chris Liechti518b0d32015-08-30 02:20:39 +0200303 :getter: Get current parity setting
304 :setter: Set new parity mode. Possible values:
305 :const:`PARITY_NONE`, :const:`PARITY_EVEN`, :const:`PARITY_ODD`
306 :const:`PARITY_MARK`, :const:`PARITY_SPACE`
307
cliechti6066f842009-07-24 00:05:45 +0000308 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000309
310 .. attribute:: stopbits
311
Chris Liechti518b0d32015-08-30 02:20:39 +0200312 :getter: Get current stop bit setting
313 :setter: Set new stop bit setting. Possible values:
314 :const:`STOPBITS_ONE`, :const:`STOPBITS_ONE_POINT_FIVE`,
315 :const:`STOPBITS_TWO`
316
cliechti6066f842009-07-24 00:05:45 +0000317 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000318
319 .. attribute:: timeout
320
Chris Liechti518b0d32015-08-30 02:20:39 +0200321 :getter: Get current read timeout setting
322 :setter: Set read timeout
323 :type: float (seconds)
324
cliechti6066f842009-07-24 00:05:45 +0000325 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000326
Chris Liechti518b0d32015-08-30 02:20:39 +0200327 .. attribute:: write_timeout
328
329 :getter: Get current write timeout setting
330 :setter: Set write timeout
331 :type: float (seconds)
cliechtiedcdbe42009-07-22 00:48:34 +0000332
cliechti6066f842009-07-24 00:05:45 +0000333 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000334
Chris Liechti518b0d32015-08-30 02:20:39 +0200335 .. versionchanged:: 3.0 renamed from ``writeTimeout``
336
337 .. attribute:: inter_byte_timeout
338
339 :getter: Get current inter byte timeout setting
340 :setter: Disable (``None``) or enable the inter byte timeout
341 :type: float or None
342
343 Read or write current inter byte timeout setting.
344
345 .. versionchanged:: 3.0 renamed from ``interCharTimeout``
346
cliechtiedcdbe42009-07-22 00:48:34 +0000347 .. attribute:: xonxoff
348
Chris Liechti518b0d32015-08-30 02:20:39 +0200349 :getter: Get current software flow control setting
350 :setter: Enable or disable software flow control
351 :type: bool
352
cliechti6066f842009-07-24 00:05:45 +0000353 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000354
355 .. attribute:: rtscts
356
Chris Liechti518b0d32015-08-30 02:20:39 +0200357 :getter: Get current hardware flow control setting
358 :setter: Enable or disable hardware flow control
359 :type: bool
360
cliechti6066f842009-07-24 00:05:45 +0000361 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000362
363 .. attribute:: dsrdtr
364
Chris Liechti518b0d32015-08-30 02:20:39 +0200365 :getter: Get current hardware flow control setting
366 :setter: Enable or disable hardware flow control
367 :type: bool
368
cliechti6066f842009-07-24 00:05:45 +0000369 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000370
Chris Liechtice621882015-08-06 19:29:31 +0200371 .. attribute:: rs485_mode
372
Chris Liechti518b0d32015-08-30 02:20:39 +0200373 :getter: Get the current RS485 settings
374 :setter: Disable (``None``) or enable the RS485 settings
375 :type: :class:`rs485.RS485Settings` or ``None``
376 :platform: Posix (Linux, limited set of hardware)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100377 :platform: Windows (only RTS on TX possible)
Chris Liechtice621882015-08-06 19:29:31 +0200378
379 Attribute to configure RS485 support. When set to an instance of
380 :class:`rs485.RS485Settings` and supported by OS, RTS will be active
381 when data is sent and inactive otherwise (for reception). The
382 :class:`rs485.RS485Settings` class provides additional settings
383 supported on some platforms.
384
385 .. versionadded:: 3.0
386
387
cliechtiedcdbe42009-07-22 00:48:34 +0000388 The following constants are also provided:
389
390 .. attribute:: BAUDRATES
391
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100392 A list of valid baud rates. The list may be incomplete, such that higher
393 and/or intermediate baud rates may also be supported by the device
394 (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000395
396 .. attribute:: BYTESIZES
397
cliechti25375b52010-07-21 23:27:13 +0000398 A list of valid byte sizes for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000399
400 .. attribute:: PARITIES
401
cliechti25375b52010-07-21 23:27:13 +0000402 A list of valid parities for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000403
404 .. attribute:: STOPBITS
405
cliechti25375b52010-07-21 23:27:13 +0000406 A list of valid stop bit widths for the device (Read Only).
cliechtiedcdbe42009-07-22 00:48:34 +0000407
cliechti4a567a02009-07-27 22:09:31 +0000408
cliechti25375b52010-07-21 23:27:13 +0000409 The following methods are for compatibility with the :mod:`io` library.
cliechti4a567a02009-07-27 22:09:31 +0000410
411 .. method:: readable()
412
413 :return: True
cliechtif4566342009-08-04 00:07:19 +0000414
cliechti4a567a02009-07-27 22:09:31 +0000415 .. versionadded:: 2.5
416
417 .. method:: writable()
418
419 :return: True
cliechtif4566342009-08-04 00:07:19 +0000420
cliechti4a567a02009-07-27 22:09:31 +0000421 .. versionadded:: 2.5
422
423 .. method:: seekable()
424
425 :return: False
cliechtif4566342009-08-04 00:07:19 +0000426
cliechti4a567a02009-07-27 22:09:31 +0000427 .. versionadded:: 2.5
428
429 .. method:: readinto(b)
430
cliechti8611bf42009-08-03 00:34:03 +0000431 :param b: bytearray or array instance
cliechti4a567a02009-07-27 22:09:31 +0000432 :return: Number of byte read
433
cliechtid7e7ce22009-08-03 02:01:07 +0000434 Read up to len(b) bytes into :class:`bytearray` *b* and return the
435 number of bytes read.
cliechti4a567a02009-07-27 22:09:31 +0000436
437 .. versionadded:: 2.5
438
cliechti25375b52010-07-21 23:27:13 +0000439 The port settings can be read and written as dictionary.
440
Chris Liechti256ea212015-08-29 23:57:00 +0200441 .. method:: get_settings()
cliechti4065dce2009-08-10 00:55:46 +0000442
443 :return: a dictionary with current port settings.
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100444 :rtype: dict
cliechti4065dce2009-08-10 00:55:46 +0000445
446 Get a dictionary with port settings. This is useful to backup the
447 current settings so that a later point in time they can be restored
Chris Liechti518b0d32015-08-30 02:20:39 +0200448 using :meth:`apply_settings`.
cliechti4065dce2009-08-10 00:55:46 +0000449
450 Note that control lines (RTS/DTR) are part of the settings.
451
452 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100453 .. versionchanged:: 3.0 renamed from ``getSettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000454
Chris Liechti256ea212015-08-29 23:57:00 +0200455 .. method:: apply_settings(d)
cliechti4065dce2009-08-10 00:55:46 +0000456
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100457 :param dict d: a dictionary with port settings.
cliechti4065dce2009-08-10 00:55:46 +0000458
Chris Liechti518b0d32015-08-30 02:20:39 +0200459 Applies a dictionary that was created by :meth:`get_settings`. Only
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100460 changes are applied and when a key is missing, it means that the
461 setting stays unchanged.
cliechti4065dce2009-08-10 00:55:46 +0000462
463 Note that control lines (RTS/DTR) are not changed.
464
465 .. versionadded:: 2.5
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100466 .. versionchanged:: 3.0 renamed from ``applySettingsDict``
cliechti4065dce2009-08-10 00:55:46 +0000467
cliechti25375b52010-07-21 23:27:13 +0000468 Platform specific methods.
469
cliechtic648da92011-12-29 01:17:51 +0000470 .. warning:: Programs using the following methods and attributes are not
471 portable to other platforms!
cliechti25375b52010-07-21 23:27:13 +0000472
473 .. method:: nonblocking()
474
Chris Liechtice621882015-08-06 19:29:31 +0200475 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000476
477 Configure the device for nonblocking operation. This can be useful if
Chris Liechti518b0d32015-08-30 02:20:39 +0200478 the port is used with :mod:`select`. Note that :attr:`timeout` must
479 also be set to ``0``
cliechti25375b52010-07-21 23:27:13 +0000480
481 .. method:: fileno()
482
Chris Liechtice621882015-08-06 19:29:31 +0200483 :platform: Posix
cliechti25375b52010-07-21 23:27:13 +0000484 :return: File descriptor.
485
486 Return file descriptor number for the port that is opened by this object.
487 It is useful when serial ports are used with :mod:`select`.
488
Chris Liechti518b0d32015-08-30 02:20:39 +0200489 .. method:: set_input_flow_control(enable)
cliechti25375b52010-07-21 23:27:13 +0000490
cliechti2f0f8a32011-12-28 22:10:00 +0000491 :platform: Posix
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100492 :param bool enable: Set flow control state.
cliechti25375b52010-07-21 23:27:13 +0000493
cliechti2f0f8a32011-12-28 22:10:00 +0000494 Manually control flow - when software flow control is enabled.
495
496 This will send XON (true) and XOFF (false) to the other device.
497
Chris Liechti518b0d32015-08-30 02:20:39 +0200498 .. versionadded:: 2.7 (Posix support added)
499 .. versionchanged:: 3.0 renamed from ``flowControlOut``
cliechti2f0f8a32011-12-28 22:10:00 +0000500
Chris Liechti518b0d32015-08-30 02:20:39 +0200501 .. method:: set_output_flow_control(enable)
cliechti2f0f8a32011-12-28 22:10:00 +0000502
Chris Liechti518b0d32015-08-30 02:20:39 +0200503 :platform: Posix (HW and SW flow control)
504 :platform: Windows (SW flow control only)
Chris Liechti3e0dcc72015-12-18 23:23:26 +0100505 :param bool enable: Set flow control state.
cliechti2f0f8a32011-12-28 22:10:00 +0000506
507 Manually control flow of outgoing data - when hardware or software flow
508 control is enabled.
509
510 Sending will be suspended when called with ``False`` and enabled when
511 called with ``True``.
512
Chris Liechti518b0d32015-08-30 02:20:39 +0200513 .. versionchanged:: 2.7 (renamed on Posix, function was called ``flowControl``)
514 .. versionchanged:: 3.0 renamed from ``setXON``
Chris Liechti256ea212015-08-29 23:57:00 +0200515
516
Chris Liechti518b0d32015-08-30 02:20:39 +0200517
Chris Liechti9a99cb22015-08-30 22:16:50 +0200518 .. note:: The following members are deprecated and will be removed in a
Chris Liechti518b0d32015-08-30 02:20:39 +0200519 future release.
Chris Liechti256ea212015-08-29 23:57:00 +0200520
521 .. attribute:: portstr
522
Chris Liechti518b0d32015-08-30 02:20:39 +0200523 .. deprecated:: 2.5 use :attr:`name` instead
Chris Liechti256ea212015-08-29 23:57:00 +0200524
525 .. method:: inWaiting()
526
Chris Liechti518b0d32015-08-30 02:20:39 +0200527 .. deprecated:: 3.0 see :attr:`in_waiting`
528
529 .. attribute:: writeTimeout
530
531 .. deprecated:: 3.0 see :attr:`write_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200532
533 .. attribute:: interCharTimeout
534
Chris Liechti518b0d32015-08-30 02:20:39 +0200535 .. deprecated:: 3.0 see :attr:`inter_byte_timeout`
Chris Liechti256ea212015-08-29 23:57:00 +0200536
537 .. method:: sendBreak(duration=0.25)
538
Chris Liechti518b0d32015-08-30 02:20:39 +0200539 .. deprecated:: 3.0 see :meth:`send_break`
Chris Liechti256ea212015-08-29 23:57:00 +0200540
541 .. method:: flushInput()
542
Chris Liechti518b0d32015-08-30 02:20:39 +0200543 .. deprecated:: 3.0 see :meth:`reset_input_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200544
545 .. method:: flushOutput()
546
Chris Liechti518b0d32015-08-30 02:20:39 +0200547 .. deprecated:: 3.0 see :meth:`reset_output_buffer`
Chris Liechti256ea212015-08-29 23:57:00 +0200548
549 .. method:: setBreak(level=True)
550
Chris Liechti518b0d32015-08-30 02:20:39 +0200551 .. deprecated:: 3.0 see :attr:`break_condition`
Chris Liechti256ea212015-08-29 23:57:00 +0200552
553 .. method:: setRTS(level=True)
554
Chris Liechti518b0d32015-08-30 02:20:39 +0200555 .. deprecated:: 3.0 see :attr:`rts`
Chris Liechti256ea212015-08-29 23:57:00 +0200556
557 .. method:: setDTR(level=True)
558
Chris Liechti518b0d32015-08-30 02:20:39 +0200559 .. deprecated:: 3.0 see :attr:`dtr`
Chris Liechti256ea212015-08-29 23:57:00 +0200560
561 .. method:: getCTS()
562
Chris Liechti518b0d32015-08-30 02:20:39 +0200563 .. deprecated:: 3.0 see :attr:`cts`
Chris Liechti256ea212015-08-29 23:57:00 +0200564
565 .. method:: getDSR()
566
Chris Liechti518b0d32015-08-30 02:20:39 +0200567 .. deprecated:: 3.0 see :attr:`dsr`
Chris Liechti256ea212015-08-29 23:57:00 +0200568
569 .. method:: getRI()
570
Chris Liechti518b0d32015-08-30 02:20:39 +0200571 .. deprecated:: 3.0 see :attr:`ri`
Chris Liechti256ea212015-08-29 23:57:00 +0200572
573 .. method:: getCD()
574
Chris Liechti518b0d32015-08-30 02:20:39 +0200575 .. deprecated:: 3.0 see :attr:`cd`
Chris Liechti256ea212015-08-29 23:57:00 +0200576
577 .. method:: getSettingsDict()
578
Chris Liechti518b0d32015-08-30 02:20:39 +0200579 .. deprecated:: 3.0 see :meth:`get_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200580
581 .. method:: applySettingsDict(d)
582
Chris Liechti518b0d32015-08-30 02:20:39 +0200583 .. deprecated:: 3.0 see :meth:`apply_settings`
Chris Liechti256ea212015-08-29 23:57:00 +0200584
585 .. method:: outWaiting()
586
Chris Liechti518b0d32015-08-30 02:20:39 +0200587 .. deprecated:: 3.0 see :attr:`out_waiting`
Chris Liechti256ea212015-08-29 23:57:00 +0200588
589 .. method:: setXON(level=True)
590
Chris Liechti518b0d32015-08-30 02:20:39 +0200591 .. deprecated:: 3.0 see :meth:`set_output_flow_control`
Chris Liechti256ea212015-08-29 23:57:00 +0200592
593 .. method:: flowControlOut(enable)
594
Chris Liechti518b0d32015-08-30 02:20:39 +0200595 .. deprecated:: 3.0 see :meth:`set_input_flow_control`
cliechtif4566342009-08-04 00:07:19 +0000596
cliechtic648da92011-12-29 01:17:51 +0000597 .. attribute:: rtsToggle
598
599 :platform: Windows
600
601 Attribute to configure RTS toggle control setting. When enabled and
602 supported by OS, RTS will be active when data is available and inactive
603 if no data is available.
604
605 .. versionadded:: 2.6
Chris Liechti518b0d32015-08-30 02:20:39 +0200606 .. versionchanged:: 3.0 (removed, see :attr:`rs485_mode` instead)
cliechtic648da92011-12-29 01:17:51 +0000607
608
cliechtic56e41d2011-08-25 22:06:38 +0000609Implementation detail: some attributes and functions are provided by the
610class :class:`SerialBase` and some by the platform specific class and
611others by the base class mentioned above.
cliechti25375b52010-07-21 23:27:13 +0000612
Chris Liechtice621882015-08-06 19:29:31 +0200613RS485 support
614-------------
615The :class:`Serial` class has a :attr:`Serial.rs485_mode` attribute which allows to
616enable RS485 specific support on some platforms. Currently Windows and Linux
617(only a small number of devices) are supported.
cliechti4a567a02009-07-27 22:09:31 +0000618
Chris Liechtice621882015-08-06 19:29:31 +0200619:attr:`Serial.rs485_mode` needs to be set to an instance of
620:class:`rs485.RS485Settings` to enable or to ``None`` to disable this feature.
cliechti4a567a02009-07-27 22:09:31 +0000621
Chris Liechtice621882015-08-06 19:29:31 +0200622Usage::
cliechti4a567a02009-07-27 22:09:31 +0000623
Chris Liechtice621882015-08-06 19:29:31 +0200624 ser = serial.Serial(...)
625 ser.rs485_mode = serial.rs485.RS485Settings(...)
626 ser.write(b'hello')
cliechti4a567a02009-07-27 22:09:31 +0000627
Chris Liechtice621882015-08-06 19:29:31 +0200628There is a subclass :class:`rs485.RS485` available to emulate the RS485 support
629on regular serial ports.
cliechti4a567a02009-07-27 22:09:31 +0000630
cliechti4a567a02009-07-27 22:09:31 +0000631
Chris Liechtice621882015-08-06 19:29:31 +0200632.. class:: rs485.RS485Settings
cliechti4a567a02009-07-27 22:09:31 +0000633
Chris Liechtice621882015-08-06 19:29:31 +0200634 A class that holds RS485 specific settings which are supported on
635 some platforms.
cliechti4a567a02009-07-27 22:09:31 +0000636
Chris Liechtice621882015-08-06 19:29:31 +0200637 .. versionadded:: 3.0
cliechti4a567a02009-07-27 22:09:31 +0000638
Chris Liechtice621882015-08-06 19:29:31 +0200639 .. 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 +0000640
Chris Liechtice621882015-08-06 19:29:31 +0200641 :param bool rts_level_for_tx:
642 RTS level for transmission
cliechti4a567a02009-07-27 22:09:31 +0000643
Chris Liechtice621882015-08-06 19:29:31 +0200644 :param bool rts_level_for_rx:
645 RTS level for reception
cliechti4a567a02009-07-27 22:09:31 +0000646
Chris Liechtice621882015-08-06 19:29:31 +0200647 :param bool loopback:
648 When set to ``True`` transmitted data is also received.
cliechti4a567a02009-07-27 22:09:31 +0000649
Chris Liechtice621882015-08-06 19:29:31 +0200650 :param float delay_before_tx:
651 Delay after setting RTS but before transmission starts
652
653 :param float delay_before_rx:
654 Delay after transmission ends and resetting RTS
655
656 .. attribute:: rts_level_for_tx
657
658 RTS level for transmission.
659
660 .. attribute:: rts_level_for_rx
661
662 RTS level for reception.
663
664 .. attribute:: loopback
665
666 When set to ``True`` transmitted data is also received.
667
668 .. attribute:: delay_before_tx
669
670 Delay after setting RTS but before transmission starts (seconds as float).
671
672 .. attribute:: delay_before_rx
673
674 Delay after transmission ends and resetting RTS (seconds as float).
cliechti4a567a02009-07-27 22:09:31 +0000675
676
Chris Liechtice621882015-08-06 19:29:31 +0200677.. class:: rs485.RS485
cliechti4a567a02009-07-27 22:09:31 +0000678
Chris Liechtice621882015-08-06 19:29:31 +0200679 A subclass that replaces the :meth:`Serial.write` method with one that toggles RTS
680 according to the RS485 settings.
cliechti4a567a02009-07-27 22:09:31 +0000681
Chris Liechti518b0d32015-08-30 02:20:39 +0200682 Usage::
683
684 ser = serial.rs485.RS485(...)
685 ser.rs485_mode = serial.rs485.RS485Settings(...)
686 ser.write(b'hello')
687
Chris Liechtice621882015-08-06 19:29:31 +0200688 .. warning:: This may work unreliably on some serial ports (control signals not
689 synchronized or delayed compared to data). Using delays may be unreliable
690 (varying times, larger than expected) as the OS may not support very fine
691 grained delays (no smaller than in the order of tens of milliseconds).
cliechti4a567a02009-07-27 22:09:31 +0000692
Chris Liechtice621882015-08-06 19:29:31 +0200693 .. note:: Some implementations support this natively in the class
694 :class:`Serial`. Better performance can be expected when the native version
695 is used.
cliechti4a567a02009-07-27 22:09:31 +0000696
Chris Liechtice621882015-08-06 19:29:31 +0200697 .. note:: The loopback property is ignored by this implementation. The actual
698 behavior depends on the used hardware.
cliechti4a567a02009-07-27 22:09:31 +0000699
cliechti4a567a02009-07-27 22:09:31 +0000700
cliechtie214ff82010-07-21 15:48:47 +0000701
cliechti25375b52010-07-21 23:27:13 +0000702
cliechti7aed8332009-08-05 14:19:31 +0000703:rfc:`2217` Network ports
704-------------------------
705
706.. warning:: This implementation is currently in an experimental state. Use
707 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000708
cliechtiec4ac1b2009-08-02 00:47:21 +0000709.. class:: rfc2217.Serial
710
Chris Liechti2a1befc2015-10-21 17:37:08 +0200711 This implements a :rfc:`2217` compatible client. Port names are :ref:`URL
712 <URLs>` in the form: ``rfc2217://<host>:<port>[?<option>[&<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000713
cliechtiec4ac1b2009-08-02 00:47:21 +0000714 This class API is compatible to :class:`Serial` with a few exceptions:
715
Chris Liechti518b0d32015-08-30 02:20:39 +0200716 - write_timeout is not implemented
cliechtiec4ac1b2009-08-02 00:47:21 +0000717 - The current implementation starts a thread that keeps reading from the
718 (internal) socket. The thread is managed automatically by the
719 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
720 However it may be a problem for user applications that like to use select
721 instead of threads.
722
723 Due to the nature of the network and protocol involved there are a few
724 extra points to keep in mind:
725
726 - All operations have an additional latency time.
727 - Setting control lines (RTS/CTS) needs more time.
728 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
729 cache is updated depends entirely on the server. The server itself may
730 implement a polling at a certain rate and quick changes may be invisible.
731 - The network layer also has buffers. This means that :meth:`flush`,
Chris Liechti518b0d32015-08-30 02:20:39 +0200732 :meth:`reset_input_buffer` and :meth:`reset_output_buffer` may work with
733 additional delay. Likewise :attr:`in_waiting` returns the size of the
734 data arrived at the object internal buffer and excludes any bytes in the
735 network buffers or any server side buffer.
cliechtiec4ac1b2009-08-02 00:47:21 +0000736 - Closing and immediately reopening the same port may fail due to time
737 needed by the server to get ready again.
738
739 Not implemented yet / Possible problems with the implementation:
740
cliechti8611bf42009-08-03 00:34:03 +0000741 - :rfc:`2217` flow control between client and server (objects internal
742 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000743 - No authentication support (servers may not prompt for a password etc.)
744 - No encryption.
745
746 Due to lack of authentication and encryption it is not suitable to use this
747 client for connections across the internet and should only be used in
748 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000749
cliechti7c640ed2009-08-02 00:54:51 +0000750 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000751
cliechti7aed8332009-08-05 14:19:31 +0000752
753.. class:: rfc2217.PortManager
754
755 This class provides helper functions for implementing :rfc:`2217`
756 compatible servers.
757
Chris Liechti518b0d32015-08-30 02:20:39 +0200758 Basically, it implements everything needed for the :rfc:`2217` protocol.
cliechti7aed8332009-08-05 14:19:31 +0000759 It just does not open sockets and read/write to serial ports (though it
760 changes other port settings). The user of this class must take care of the
761 data transmission itself. The reason for that is, that this way, this class
762 supports all programming models such as threads and select.
763
764 Usage examples can be found in the examples where two TCP/IP - serial
765 converters are shown, one using threads (the single port server) and an
766 other using select (the multi port server).
767
768 .. note:: Each new client connection must create a new instance as this
769 object (and the :rfc:`2217` protocol) has internal state.
770
771 .. method:: __init__(serial_port, connection, debug_output=False)
772
773 :param serial_port: a :class:`Serial` instance that is managed.
774 :param connection: an object implementing :meth:`write`, used to write
775 to the network.
cliechti86844e82009-08-12 00:05:33 +0000776 :param debug_output: enables debug messages: a :class:`logging.Logger`
777 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000778
779 Initializes the Manager and starts negotiating with client in Telnet
780 and :rfc:`2217` protocol. The negotiation starts immediately so that
781 the class should be instantiated in the moment the client connects.
782
783 The *serial_port* can be controlled by :rfc:`2217` commands. This
cliechti06281be2011-08-25 23:08:29 +0000784 object will modify the port settings (baud rate etc.) and control lines
cliechti7aed8332009-08-05 14:19:31 +0000785 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
786 the :meth:`filter` method.
787
788 The *connection* object must implement a :meth:`write(data)` function.
789 This function must ensure that *data* is written at once (no user data
790 mixed in, i.e. it must be thread-safe). All data must be sent in its
791 raw form (:meth:`escape` must not be used) as it is used to send Telnet
792 and :rfc:`2217` control commands.
793
cliechti86844e82009-08-12 00:05:33 +0000794 For diagnostics of the connection or the implementation, *debug_output*
795 can be set to an instance of a :class:`logging.Logger` (e.g.
796 ``logging.getLogger('rfc2217.server')``). The caller should configure
797 the logger using ``setLevel`` for the desired detail level of the logs.
798
cliechti7aed8332009-08-05 14:19:31 +0000799 .. method:: escape(data)
800
801 :param data: data to be sent over the network.
802 :return: data, escaped for Telnet/:rfc:`2217`
803
804 A generator that escapes all data to be compatible with :rfc:`2217`.
805 Implementors of servers should use this function to process all data
806 sent over the network.
807
808 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000809 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000810
811 .. method:: filter(data)
812
813 :param data: data read from the network, including Telnet and
814 :rfc:`2217` controls.
815 :return: data, free from Telnet and :rfc:`2217` controls.
816
817 A generator that filters and processes all data related to :rfc:`2217`.
818 Implementors of servers should use this function to process all data
819 received from the network.
820
821 The function returns a generator which can be used in ``for`` loops.
cliechtie214ff82010-07-21 15:48:47 +0000822 It can be converted to bytes using :func:`serial.to_bytes`.
cliechti7aed8332009-08-05 14:19:31 +0000823
cliechti7cb78e82009-08-05 15:47:57 +0000824 .. method:: check_modem_lines(force_notification=False)
825
826 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000827
828 This function needs to be called periodically (e.g. every second) when
829 the server wants to send NOTIFY_MODEMSTATE messages. This is required
830 to support the client for reading CTS/DSR/RI/CD status lines.
831
832 The function reads the status line and issues the notifications
833 automatically.
834
835 .. versionadded:: 2.5
836
cliechtiec4ac1b2009-08-02 00:47:21 +0000837.. seealso::
838
839 :rfc:`2217` - Telnet Com Port Control Option
840
841
cliechtic1c37602009-07-21 01:34:57 +0000842Exceptions
843==========
844
845.. exception:: SerialException
846
847 Base class for serial port exceptions.
848
cliechti4a567a02009-07-27 22:09:31 +0000849 .. versionchanged:: 2.5
cliechti4cb94662013-10-17 03:17:50 +0000850 Now derives from :exc:`IOError` instead of :exc:`Exception`
cliechti4a567a02009-07-27 22:09:31 +0000851
cliechtic1c37602009-07-21 01:34:57 +0000852.. exception:: SerialTimeoutException
853
854 Exception that is raised on write timeouts.
855
856
857Constants
858=========
859
cliechti87686242009-08-18 00:58:31 +0000860*Parity*
861
cliechtic1c37602009-07-21 01:34:57 +0000862.. data:: PARITY_NONE
863.. data:: PARITY_EVEN
864.. data:: PARITY_ODD
865.. data:: PARITY_MARK
866.. data:: PARITY_SPACE
867
cliechti87686242009-08-18 00:58:31 +0000868*Stop bits*
869
cliechtic1c37602009-07-21 01:34:57 +0000870.. data:: STOPBITS_ONE
871.. data:: STOPBITS_ONE_POINT_FIVE
872.. data:: STOPBITS_TWO
873
cliechti06281be2011-08-25 23:08:29 +0000874Note that 1.5 stop bits are not supported on POSIX. It will fall back to 2 stop
cliechti41be0392010-01-02 03:02:38 +0000875bits.
876
cliechti87686242009-08-18 00:58:31 +0000877*Byte size*
878
cliechtic1c37602009-07-21 01:34:57 +0000879.. data:: FIVEBITS
880.. data:: SIXBITS
881.. data:: SEVENBITS
882.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000883
cliechti87686242009-08-18 00:58:31 +0000884
885*Others*
886
cliechti8611bf42009-08-03 00:34:03 +0000887Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000888software flow control:
cliechti6066f842009-07-24 00:05:45 +0000889
cliechti5b7d16a2009-07-21 21:53:59 +0000890.. data:: XON
891.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000892
cliechti4a567a02009-07-27 22:09:31 +0000893Module version:
cliechtif81362e2009-07-25 03:44:33 +0000894
895.. data:: VERSION
896
Chris Liechti518b0d32015-08-30 02:20:39 +0200897 A string indicating the pySerial version, such as ``3.0``.
cliechtif81362e2009-07-25 03:44:33 +0000898
cliechti87686242009-08-18 00:58:31 +0000899 .. versionadded:: 2.3
900
cliechti44eb98f2011-03-21 21:41:10 +0000901
cliechtie542b362011-03-18 00:49:16 +0000902Module functions and attributes
903===============================
cliechtif81362e2009-07-25 03:44:33 +0000904
905.. function:: device(number)
906
Chris Liechti518b0d32015-08-30 02:20:39 +0200907 .. versionchanged:: 3.0 removed, use ``serial.tools.list_ports`` instead
908
cliechti7c640ed2009-08-02 00:54:51 +0000909
cliechtie3ab3532009-08-05 12:40:38 +0000910.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +0000911
cliechti86844e82009-08-12 00:05:33 +0000912 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +0000913 :param do_not_open: When set to true, the serial port is not opened.
914 :return: an instance of :class:`Serial` or a compatible object.
915
916 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +0000917 on port/url. This factory function is useful when an application wants
cliechti25375b52010-07-21 23:27:13 +0000918 to support both, local ports and remote ports. There is also support
Chris Liechti589c92a2015-09-04 23:04:34 +0200919 for other types, see :ref:`URL <URLs>` section.
cliechti7c640ed2009-08-02 00:54:51 +0000920
cliechtid7e7ce22009-08-03 02:01:07 +0000921 The port is not opened when a keyword parameter called *do_not_open* is
922 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +0000923
924 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +0000925
cliechti87686242009-08-18 00:58:31 +0000926
cliechtie542b362011-03-18 00:49:16 +0000927.. attribute:: protocol_handler_packages
928
929 This attribute is a list of package names (strings) that is searched for
930 protocol handlers.
931
932 e.g. we want to support a URL ``foobar://``. A module
933 ``my_handlers.protocol_foobar`` is provided by the user::
934
935 serial.protocol_handler_packages.append("my_handlers")
936 s = serial.serial_for_url("foobar://")
937
938 For an URL starting with ``XY://`` is the function :func:`serial_for_url`
939 attempts to import ``PACKAGE.protocol_XY`` with each candidate for
940 ``PACKAGE`` from this list.
941
942 .. versionadded:: 2.6
943
944
cliechti25375b52010-07-21 23:27:13 +0000945.. function:: to_bytes(sequence)
cliechtie214ff82010-07-21 15:48:47 +0000946
947 :param sequence: String or list of integers
cliechti25375b52010-07-21 23:27:13 +0000948 :returns: an instance of ``bytes``
cliechtie214ff82010-07-21 15:48:47 +0000949
cliechtie542b362011-03-18 00:49:16 +0000950 Convert a sequence to a ``bytes`` type. This is used to write code that is
cliechtie214ff82010-07-21 15:48:47 +0000951 compatible to Python 2.x and 3.x.
952
cliechtie542b362011-03-18 00:49:16 +0000953 In Python versions prior 3.x, ``bytes`` is a subclass of str. They convert
cliechti25375b52010-07-21 23:27:13 +0000954 ``str([17])`` to ``'[17]'`` instead of ``'\x11'`` so a simple
cliechtie542b362011-03-18 00:49:16 +0000955 ``bytes(sequence)`` doesn't work for all versions of Python.
cliechtie214ff82010-07-21 15:48:47 +0000956
957 This function is used internally and in the unit tests.
958
cliechtie542b362011-03-18 00:49:16 +0000959 .. versionadded:: 2.5
960
Chris Liechtid14b1ab2015-08-21 00:28:53 +0200961.. function:: iterbytes(b)
962
963 :param b: bytes, bytearray or memoryview
964 :returns: a generator that yields bytes
965
966 Some versions of Python (3.x) would return integers instead of bytes when
967 looping over an instance of ``bytes``. This helper function ensures that
968 bytes are returned.
969
970 .. versionadded:: 3.0
971
cliechtie214ff82010-07-21 15:48:47 +0000972
Chris Liechti811bf382015-10-10 00:54:47 +0200973Threading
974=========
975
976.. module:: serial.threaded
977.. versionadded:: 3.0
978
979.. warning:: This implementation is currently in an experimental state. Use
980 at your own risk.
981
982This module provides classes to simplify working with threads and protocols.
983
984.. class:: Protocol
985
Chris Liechti5665d572015-10-13 23:50:01 +0200986 Protocol as used by the :class:`ReaderThread`. This base class provides empty
Chris Liechti811bf382015-10-10 00:54:47 +0200987 implementations of all methods.
988
989
990 .. method:: connection_made(transport)
991
992 :param transport: instance used to write to serial port.
993
994 Called when reader thread is started.
995
996 .. method:: data_received(data)
997
998 :param data: received bytes
999
1000 Called with snippets received from the serial port.
1001
1002 .. method:: connection_lost(exc)
1003
1004 :param exc: Exception if connection was terminated by error else ``None``
1005
1006 Called when the serial port is closed or the reader loop terminated
1007 otherwise.
1008
1009.. class:: Packetizer(Protocol)
1010
1011 Read binary packets from serial port. Packets are expected to be terminated
1012 with a ``TERMINATOR`` byte (null byte by default).
1013
1014 The class also keeps track of the transport.
1015
1016 .. attribute:: TERMINATOR = b'\\0'
1017
1018 .. method:: __init__()
1019
1020 .. method:: connection_made(transport)
1021
1022 Store transport
1023
1024 .. method:: connection_lost(exc)
1025
1026 Forget transport
1027
1028 .. method:: data_received(data)
1029
1030 Buffer received data and search for ``TERMINATOR``, when found,
1031 call :meth:`handle_packet`.
1032
1033 .. method:: handle_packet(packet)
1034
1035 Process packets - to be overridden by subclassing.
1036
1037
1038.. class:: LineReader(Packetizer)
1039
1040 Read and write (Unicode) lines from/to serial port.
1041 The encoding is applied.
1042
1043
1044 .. attribute:: TERMINATOR = b'\\r\\n'
1045 .. attribute:: ENCODING = 'utf-8'
1046 .. attribute:: UNICODE_HANDLING = 'replace'
1047
1048 .. method:: handle_packet(packet)
1049
1050 .. method:: handle_line(line)
1051
1052 :param line: Unicode string with one line (excluding line terminator)
1053
1054 Process one line - to be overridden by subclassing.
1055
1056 .. method:: write_line(text)
1057
1058 :param text: Unicode string with one line (excluding line terminator)
1059
1060 Write text to the transport. ``text`` is a Unicode string and the encoding
1061 is applied before sending ans also the newline is append.
1062
1063
Chris Liechti5665d572015-10-13 23:50:01 +02001064.. class:: ReaderThread(threading.Thread)
Chris Liechti811bf382015-10-10 00:54:47 +02001065
1066 Implement a serial port read loop and dispatch to a Protocol instance (like
1067 the :class:`asyncio.Protocol`) but do it with threads.
1068
1069 Calls to :meth:`close` will close the serial port but it is also possible to just
1070 :meth:`stop` this thread and continue the serial port instance otherwise.
1071
1072 .. method:: __init__(serial_instance, protocol_factory)
1073
1074 :param serial_instance: serial port instance (opened) to be used.
1075 :param protocol_factory: a callable that returns a Protocol instance
1076
1077 Initialize thread.
1078
1079 Note that the ``serial_instance`` timeout is set to one second!
1080 Other settings are not changed.
1081
1082 .. method:: stop()
1083
1084 Stop the reader thread.
1085
1086 .. method:: run()
1087
1088 The actual reader loop driven by the thread. It calls
1089 :meth:`Protocol.connection_made`, reads from the serial port calling
1090 :meth:`Protocol.data_received` and finally calling :meth:`Protocol.connection_lost`
1091 when :meth:`close` is called or an error occurs.
1092
1093 .. method:: write(data)
1094
1095 Thread safe writing (uses lock).
1096
1097 .. method:: close()
1098
1099 Close the serial port and exit reader thread, calls :meth:`stop` (uses lock).
1100
1101 .. method:: connect()
1102
1103 Wait until connection is set up and return the transport and protocol
1104 instances.
1105
1106
1107 This class can be used as context manager, in this case it starts
1108 the thread and connects automatically. The serial port is closed
1109 when the context is left.
1110
1111 .. method:: __enter__()
1112
1113 :returns: protocol
1114
1115 Connect and return protocol instance.
1116
1117 .. method:: __exit__(exc_type, exc_val, exc_tb)
1118
1119 Closes serial port.
1120
Chris Liechtia1436b12015-10-12 23:19:04 +02001121Example::
1122
1123 class PrintLines(LineReader):
1124 def connection_made(self, transport):
1125 super(PrintLines, self).connection_made(transport)
1126 sys.stdout.write('port opened\n')
1127 self.write_line('hello world')
1128
1129 def handle_line(self, data):
1130 sys.stdout.write('line received: {}\n'.format(repr(data)))
1131
1132 def connection_lost(self, exc):
1133 if exc:
1134 traceback.print_exc(exc)
1135 sys.stdout.write('port closed\n')
1136
1137 ser = serial.serial_for_url('loop://', baudrate=115200, timeout=1)
Chris Liechti5665d572015-10-13 23:50:01 +02001138 with ReaderThread(ser, PrintLines) as protocol:
Chris Liechtia1436b12015-10-12 23:19:04 +02001139 protocol.write_line('hello')
1140 time.sleep(2)
1141
Chris Liechti811bf382015-10-10 00:54:47 +02001142
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001143asyncio
1144=======
1145
Chris Liechti589c92a2015-09-04 23:04:34 +02001146.. module:: serial.aio
1147
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001148.. warning:: This implementation is currently in an experimental state. Use
1149 at your own risk.
1150
Chris Liechti589c92a2015-09-04 23:04:34 +02001151Experimental asyncio support is available for Python 3.4 and newer. The module
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001152:mod:`serial.aio` provides a :class:`asyncio.Transport`:
1153``SerialTransport``.
1154
1155
1156A factory function (`asyncio.coroutine`) is provided:
1157
1158.. function:: create_serial_connection(loop, protocol_factory, \*args, \*\*kwargs)
1159
1160 :param loop: The event handler
1161 :param protocol_factory: Factory function for a :class:`asyncio.Protocol`
1162 :param args: Passed to the :class:`serial.Serial` init function
1163 :param kwargs: Passed to the :class:`serial.Serial` init function
1164 :platform: Posix
1165
1166 Get a connection making coroutine.
1167
1168Example::
1169
1170 class Output(asyncio.Protocol):
1171 def connection_made(self, transport):
1172 self.transport = transport
1173 print('port opened', transport)
Chris Liechti589c92a2015-09-04 23:04:34 +02001174 transport.serial.rts = False
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001175 transport.write(b'hello world\n')
1176
1177 def data_received(self, data):
1178 print('data received', repr(data))
1179 self.transport.close()
1180
1181 def connection_lost(self, exc):
1182 print('port closed')
1183 asyncio.get_event_loop().stop()
1184
1185 loop = asyncio.get_event_loop()
Chris Liechti589c92a2015-09-04 23:04:34 +02001186 coro = serial.aio.create_serial_connection(loop, Output, '/dev/ttyUSB0', baudrate=115200)
Chris Liechti1f0c9b42015-09-03 23:49:48 +02001187 loop.run_until_complete(coro)
1188 loop.run_forever()
1189 loop.close()
1190