blob: aa2e1760678484fde925c96463d64f19aa79ca5f [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
15 .. method:: __init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=0, rtscts=0, interCharTimeout=None)
16
17 :param port:
cliechti4a567a02009-07-27 22:09:31 +000018 Device name or port number number or :const:`None`.
cliechtic1c37602009-07-21 01:34:57 +000019
cliechtic1c37602009-07-21 01:34:57 +000020 :param baudrate:
21 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:
30 :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:
35 :const:`STOPBITS_ONE` :const:`STOPBITS_ONE_POINT_FIVE`
36 :const:`STOPBITS_TWO`
cliechtic1c37602009-07-21 01:34:57 +000037
38 :param timeout:
39 Set a read timeout value.
40
41 :param xonxoff:
cliechtic1c37602009-07-21 01:34:57 +000042 Enable software flow control.
43
44 :param rtscts:
cliechtic1c37602009-07-21 01:34:57 +000045 Enable hardware (RTS/CTS) flow control.
46
47 :param interCharTimeout:
cliechti4a567a02009-07-27 22:09:31 +000048 Inter-character timeout, :const:`None` to disable (default).
cliechtic1c37602009-07-21 01:34:57 +000049
cliechti6066f842009-07-24 00:05:45 +000050 :exception ValueError:
cliechti4a567a02009-07-27 22:09:31 +000051 Will be raised when parameter are out of range, e.g. baud rate, data bits.
cliechti6066f842009-07-24 00:05:45 +000052
53 :exception SerialException:
54 In case the device can not be found or can not be configured.
55
56
cliechti4a567a02009-07-27 22:09:31 +000057 The port is immediately opened on object creation, when a *port* is
58 given. It is not opened when *port* is :const:`None` and a successive call
59 to :meth:`open` will be needed.
60
61 Possible values for the parameter *port*:
cliechtic1c37602009-07-21 01:34:57 +000062
cliechti5134aab2009-07-21 19:47:59 +000063 - Number: number of device, numbering starts at zero.
64 - Device name: depending on operating system. e.g. ``/dev/ttyUSB0``
65 on GNU/Linux or ``COM3`` on Windows.
cliechtic1c37602009-07-21 01:34:57 +000066
cliechti4a567a02009-07-27 22:09:31 +000067 Possible values for the parameter *timeout*:
cliechti5134aab2009-07-21 19:47:59 +000068
cliechtibb5c3c52009-07-23 02:29:27 +000069 - ``timeout = None``: wait forever
70 - ``timeout = 0``: non-blocking mode (return immediately on read)
cliechtif81362e2009-07-25 03:44:33 +000071 - ``timeout = x``: set timeout to ``x`` seconds (float allowed)
cliechtic1c37602009-07-21 01:34:57 +000072
cliechti8611bf42009-08-03 00:34:03 +000073 Note that enabling both flow control methods (*xonxoff* and *rtscts*)
74 together may not be supported. It is common to use one of the methods
75 at once, not both.
cliechtic1c37602009-07-21 01:34:57 +000076
77 .. method:: open()
78
79 Open port.
80
81 .. method:: close()
82
83 Close port immediately.
84
cliechtic1c37602009-07-21 01:34:57 +000085
cliechti4a567a02009-07-27 22:09:31 +000086 The following methods may raise :exc:`ValueError` when applied to a closed
87 port.
cliechtic1c37602009-07-21 01:34:57 +000088
89 .. method:: read(size=1)
90
cliechtibb5c3c52009-07-23 02:29:27 +000091 :param size: Number of bytes to read.
92 :return: Bytes read from the port.
cliechtic1c37602009-07-21 01:34:57 +000093
cliechti4a567a02009-07-27 22:09:31 +000094 Read *size* bytes from the serial port. If a timeout is set it may
cliechtibb5c3c52009-07-23 02:29:27 +000095 return less characters as requested. With no timeout it will block
96 until the requested number of bytes is read.
cliechtic1c37602009-07-21 01:34:57 +000097
cliechti4a567a02009-07-27 22:09:31 +000098 .. versionchanged:: 2.5
99 Returns an instance of :class:`bytes` when available (Python 2.6
cliechti8611bf42009-08-03 00:34:03 +0000100 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000101
cliechtibb5c3c52009-07-23 02:29:27 +0000102 .. method:: write(data)
103
104 :param data: Data to send.
cliechti4a567a02009-07-27 22:09:31 +0000105 :return: Number of bytes written.
cliechti6066f842009-07-24 00:05:45 +0000106 :exception SerialTimeoutException:
107 In case a write timeout is configured for the port and the time is
108 exceeded.
109
cliechti4a567a02009-07-27 22:09:31 +0000110 Write the string *data* to the port.
cliechtic1c37602009-07-21 01:34:57 +0000111
cliechti4a567a02009-07-27 22:09:31 +0000112 .. versionchanged:: 2.5
cliechtiddd78132009-07-28 01:13:28 +0000113 Accepts instances of :class:`bytes` and :class:`bytearray` when
cliechti8611bf42009-08-03 00:34:03 +0000114 available (Python 2.6 and newer) and :class:`str` otherwise.
cliechti4a567a02009-07-27 22:09:31 +0000115
116 .. method:: inWaiting()
117
118 Return the number of chars in the receive buffer.
119
120 .. method:: flush()
cliechtic1c37602009-07-21 01:34:57 +0000121
122 Flush of file like objects. In this case, wait until all data is
123 written.
124
125 .. method:: flushInput()
126
127 Flush input buffer, discarding all it's contents.
128
129 .. method:: flushOutput()
130
131 Clear output buffer, aborting the current output and
132 discarding all that is in the buffer.
133
134 .. method:: sendBreak(duration=0.25)
135
cliechtibb5c3c52009-07-23 02:29:27 +0000136 :param duration: Time (float) to activate the BREAK condition.
137
cliechtic1c37602009-07-21 01:34:57 +0000138 Send break condition. Timed, returns to idle state after given
139 duration.
140
141 .. method:: setBreak(level=True)
142
cliechtibb5c3c52009-07-23 02:29:27 +0000143 :param level: when true activate BREAK condition, else disable.
144
cliechtic1c37602009-07-21 01:34:57 +0000145 Set break: Controls TXD. When active, no transmitting is possible.
146
147 .. method:: setRTS(level=True)
148
cliechtibb5c3c52009-07-23 02:29:27 +0000149 :param level: Set control line to logic level.
150
cliechtic1c37602009-07-21 01:34:57 +0000151 Set RTS line to specified logic level.
152
153 .. method:: setDTR(level=True)
154
cliechtibb5c3c52009-07-23 02:29:27 +0000155 :param level: Set control line to logic level.
156
cliechtic1c37602009-07-21 01:34:57 +0000157 Set DTR line to specified logic level.
158
159 .. method:: getCTS()
160
cliechtibb5c3c52009-07-23 02:29:27 +0000161 :return: Current state (boolean)
162
cliechtic1c37602009-07-21 01:34:57 +0000163 Return the state of the CTS line.
164
165 .. method:: getDSR()
166
cliechtibb5c3c52009-07-23 02:29:27 +0000167 :return: Current state (boolean)
168
cliechtic1c37602009-07-21 01:34:57 +0000169 Return the state of the DSR line.
170
171 .. method:: getRI()
172
cliechtibb5c3c52009-07-23 02:29:27 +0000173 :return: Current state (boolean)
174
cliechtic1c37602009-07-21 01:34:57 +0000175 Return the state of the RI line.
176
177 .. method:: getCD()
178
cliechtibb5c3c52009-07-23 02:29:27 +0000179 :return: Current state (boolean)
180
cliechtic1c37602009-07-21 01:34:57 +0000181 Return the state of the CD line
182
183 Read-only attributes:
184
cliechtif81362e2009-07-25 03:44:33 +0000185 .. attribute:: name
cliechtic1c37602009-07-21 01:34:57 +0000186
cliechti5134aab2009-07-21 19:47:59 +0000187 Device name. This is always the device name even if the
188 port was opened by a number. (Read Only).
cliechtic1c37602009-07-21 01:34:57 +0000189
cliechtif81362e2009-07-25 03:44:33 +0000190 .. versionadded:: 2.5
191
192 .. attribute:: portstr
193
194 :deprecated: use :attr:`name` instead
195
cliechtic1c37602009-07-21 01:34:57 +0000196 .. attribute:: BAUDRATES
197
198 A list of valid baud rates. The list may be incomplete such that higher
199 baud rates may be supported by the device and that values in between the
200 standard baud rates are supported. (Read Only).
201
202 .. attribute:: BYTESIZES
203
204 A list of valid byte sizes for the device (Read Only).
205
206 .. attribute:: PARITIES
207
208 A list of valid parities for the device (Read Only).
209
210 .. attribute:: STOPBITS
211
212 A list of valid stop bit widths for the device (Read Only).
213
214
cliechti4a567a02009-07-27 22:09:31 +0000215 New values can be assigned to the following attributes, the port will be
216 reconfigured, even if it's opened at that time:
cliechtic1c37602009-07-21 01:34:57 +0000217
218 .. attribute:: port
219
220 Port name/number as set by the user.
221
222 .. attribute:: baudrate
223
224 Current baud rate setting.
225
226 .. attribute:: bytesize
227
228 Byte size in bits.
229
230 .. attribute:: parity
231
232 Parity setting.
233
234 .. attribute:: stopbits
235
236 Stop bit with.
237
238 .. attribute:: timeout
239
cliechti5134aab2009-07-21 19:47:59 +0000240 Timeout setting (seconds, float).
cliechtic1c37602009-07-21 01:34:57 +0000241
242 .. attribute:: xonxoff
243
244 If Xon/Xoff flow control is enabled.
245
246 .. attribute:: rtscts
247
248 If hardware flow control is enabled.
249
250 Platform specific methods.
251
252 .. warning:: Programs using the following methods are not portable to other platforms!
253
254 .. method:: nonblocking()
255
256 :platform: Unix
cliechti86e87872009-07-21 13:32:45 +0000257
cliechtic1c37602009-07-21 01:34:57 +0000258 Configure the device for nonblocking operations. This can be useful if
259 the port is used with ``select``.
260
261 .. method:: fileno()
262
263 :platform: Unix
cliechtibb5c3c52009-07-23 02:29:27 +0000264 :return: File descriptor.
cliechti86e87872009-07-21 13:32:45 +0000265
cliechtibb5c3c52009-07-23 02:29:27 +0000266 Return file descriptor number for the port that is opened by this object.
cliechtic1c37602009-07-21 01:34:57 +0000267
268 .. method:: setXON(level=True)
269
270 :platform: Windows
cliechtibb5c3c52009-07-23 02:29:27 +0000271 :param level: Set flow control state.
cliechti86e87872009-07-21 13:32:45 +0000272
cliechtic1c37602009-07-21 01:34:57 +0000273 Set software flow control state.
274
275
cliechtiedcdbe42009-07-22 00:48:34 +0000276.. class:: SerialBase
277
278 The following attributes are implemented as properties. They work with open
279 and closed ports.
280
281 .. attribute:: port
282
283 Read or write port. When the port is already open, it will be closed
284 and reopened with the new setting.
285
286 .. attribute:: baudrate
287
cliechti6066f842009-07-24 00:05:45 +0000288 Read or write current baud rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000289
290 .. attribute:: bytesize
291
cliechti6066f842009-07-24 00:05:45 +0000292 Read or write current data byte size setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000293
294 .. attribute:: parity
295
cliechti6066f842009-07-24 00:05:45 +0000296 Read or write current parity setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000297
298 .. attribute:: stopbits
299
cliechti6066f842009-07-24 00:05:45 +0000300 Read or write current stop bit width setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000301
302 .. attribute:: timeout
303
cliechti6066f842009-07-24 00:05:45 +0000304 Read or write current read timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000305
306 .. attribute:: writeTimeout
307
cliechti6066f842009-07-24 00:05:45 +0000308 Read or write current write timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000309
310 .. attribute:: xonxoff
311
cliechti6066f842009-07-24 00:05:45 +0000312 Read or write current software flow control rate setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000313
314 .. attribute:: rtscts
315
cliechti6066f842009-07-24 00:05:45 +0000316 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000317
318 .. attribute:: dsrdtr
319
cliechti6066f842009-07-24 00:05:45 +0000320 Read or write current hardware flow control setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000321
322 .. attribute:: interCharTimeout
323
cliechti6066f842009-07-24 00:05:45 +0000324 Read or write current inter character timeout setting.
cliechtiedcdbe42009-07-22 00:48:34 +0000325
326 The following constants are also provided:
327
328 .. attribute:: BAUDRATES
329
330 A tuple of standard baud rate values. The actual device may support more
331 or less...
332
333 .. attribute:: BYTESIZES
334
335 A tuple of supported byte size values.
336
337 .. attribute:: PARITIES
338
339 A tuple of supported parity settings.
340
341 .. attribute:: STOPBITS
342
343 A tuple of supported stop bit settings.
344
cliechti4a567a02009-07-27 22:09:31 +0000345 .. method:: readline(size=None, eol='\\n')
346
347 :param size: Max number of bytes to read, ``None`` -> no limit.
348 :param eol: The end of line character.
349
350 Read a line which is terminated with end-of-line (*eol*) character
351 (``\\n`` by default) or until timeout.
352
353 .. method:: readlines(sizehint=None, eol='\\n')
354
355 :param size: Ignored parameter.
356 :param eol: The end of line character.
357
358 Read a list of lines, until timeout. *sizehint* is ignored and only
359 present for API compatibility with built-in File objects.
360
361 .. method:: xreadlines(sizehint=None)
362
363 Just calls :meth:`readlines` - here for compatibility.
364
365 For compatibility with the :mod:`io` library are the following methods.
366
367 .. method:: readable()
368
369 :return: True
cliechtif4566342009-08-04 00:07:19 +0000370
cliechti4a567a02009-07-27 22:09:31 +0000371 .. versionadded:: 2.5
372
373 .. method:: writable()
374
375 :return: True
cliechtif4566342009-08-04 00:07:19 +0000376
cliechti4a567a02009-07-27 22:09:31 +0000377 .. versionadded:: 2.5
378
379 .. method:: seekable()
380
381 :return: False
cliechtif4566342009-08-04 00:07:19 +0000382
cliechti4a567a02009-07-27 22:09:31 +0000383 .. versionadded:: 2.5
384
385 .. method:: readinto(b)
386
cliechti8611bf42009-08-03 00:34:03 +0000387 :param b: bytearray or array instance
cliechti4a567a02009-07-27 22:09:31 +0000388 :return: Number of byte read
389
cliechtid7e7ce22009-08-03 02:01:07 +0000390 Read up to len(b) bytes into :class:`bytearray` *b* and return the
391 number of bytes read.
cliechti4a567a02009-07-27 22:09:31 +0000392
393 .. versionadded:: 2.5
394
cliechtif4566342009-08-04 00:07:19 +0000395
cliechti4a567a02009-07-27 22:09:31 +0000396.. note::
397
398 For systems that provide the :mod:`io` library (Python 2.6 and newer), the
cliechtid7e7ce22009-08-03 02:01:07 +0000399 class :class:`Serial` will derive from :class:`io.RawIOBase`. For all
cliechti4a567a02009-07-27 22:09:31 +0000400 others from :class:`FileLike`.
401
402.. class:: FileLike
403
404 An abstract file like class. It is used as base class for :class:`Serial`.
405
406 This class implements :meth:`readline` and :meth:`readlines` based on read
407 and :meth:`writelines` based on write. This class is used to provide the
408 above functions for to Serial port objects.
409
410 Note that when the serial port was opened with no timeout that
411 :meth:`readline` blocks until it sees a newline (or the specified size is
412 reached) and that :meth:`readlines` would never return and therefore
413 refuses to work (it raises an exception in this case)!
414
415 .. method:: writelines(sequence)
416
417 Write a list of strings to the port.
418
419
420 The following three methods are overridden in :class:`Serial`.
421
422 .. method:: flush()
423
424 Flush of file like objects. It's a no-op in this class, may be overridden.
425
426 .. method:: read()
427
428 Raises NotImplementedError, needs to be overridden by subclass.
429
430 .. method:: write(data)
431
432 Raises NotImplementedError, needs to be overridden by subclass.
433
434 The following functions are implemented for compatibility with other
435 file-like objects, however serial ports are not seekable.
436
437
438 .. method:: seek(pos, whence=0)
439
440 :exception IOError: always, as method is not supported on serial port
441
442 .. versionadded:: 2.5
443
444 .. method:: tell()
445
446 :exception IOError: always, as method is not supported on serial port
447
448 .. versionadded:: 2.5
449
450 .. method:: truncate(self, n=None)
451
452 :exception IOError: always, as method is not supported on serial port
453
454 .. versionadded:: 2.5
455
456 .. method:: isatty()
457
458 :exception IOError: always, as method is not supported on serial port
459
460 .. versionadded:: 2.5
461
462 To be able to use the file like object as iterator for e.g.
463 ``for line in Serial(0): ...`` usage:
464
465 .. method:: next()
466
467 Return the next line by calling :meth:`readline`.
468
469 .. method:: __iter__()
470
471 Returns self.
472
cliechti7aed8332009-08-05 14:19:31 +0000473:rfc:`2217` Network ports
474-------------------------
475
476.. warning:: This implementation is currently in an experimental state. Use
477 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000478
cliechtiec4ac1b2009-08-02 00:47:21 +0000479.. class:: rfc2217.Serial
480
cliechtif4566342009-08-04 00:07:19 +0000481 This implements a :rfc:`2217` compatible client. Port names are URLs_ in the
482 form: ``rfc2217://<host>:<port>[/<option>[/<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000483
cliechtiec4ac1b2009-08-02 00:47:21 +0000484 This class API is compatible to :class:`Serial` with a few exceptions:
485
486 - numbers as port name are not allowed, only URLs in the form described
487 above.
488 - writeTimeout is not implemented
489 - The current implementation starts a thread that keeps reading from the
490 (internal) socket. The thread is managed automatically by the
491 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
492 However it may be a problem for user applications that like to use select
493 instead of threads.
494
495 Due to the nature of the network and protocol involved there are a few
496 extra points to keep in mind:
497
498 - All operations have an additional latency time.
499 - Setting control lines (RTS/CTS) needs more time.
500 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
501 cache is updated depends entirely on the server. The server itself may
502 implement a polling at a certain rate and quick changes may be invisible.
503 - The network layer also has buffers. This means that :meth:`flush`,
504 :meth:`flushInput` and :meth:`flushOutput` may work with additional delay.
505 Likewise :meth:`inWaiting` returns the size of the data arrived at the
506 object internal buffer and excludes any bytes in the network buffers or
507 any server side buffer.
508 - Closing and immediately reopening the same port may fail due to time
509 needed by the server to get ready again.
510
511 Not implemented yet / Possible problems with the implementation:
512
cliechti8611bf42009-08-03 00:34:03 +0000513 - :rfc:`2217` flow control between client and server (objects internal
514 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000515 - No authentication support (servers may not prompt for a password etc.)
516 - No encryption.
517
518 Due to lack of authentication and encryption it is not suitable to use this
519 client for connections across the internet and should only be used in
520 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000521
cliechti7c640ed2009-08-02 00:54:51 +0000522 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000523
cliechti7aed8332009-08-05 14:19:31 +0000524
525.. class:: rfc2217.PortManager
526
527 This class provides helper functions for implementing :rfc:`2217`
528 compatible servers.
529
530 Basically, it implements every thing needed for the :rfc:`2217` protocol.
531 It just does not open sockets and read/write to serial ports (though it
532 changes other port settings). The user of this class must take care of the
533 data transmission itself. The reason for that is, that this way, this class
534 supports all programming models such as threads and select.
535
536 Usage examples can be found in the examples where two TCP/IP - serial
537 converters are shown, one using threads (the single port server) and an
538 other using select (the multi port server).
539
540 .. note:: Each new client connection must create a new instance as this
541 object (and the :rfc:`2217` protocol) has internal state.
542
543 .. method:: __init__(serial_port, connection, debug_output=False)
544
545 :param serial_port: a :class:`Serial` instance that is managed.
546 :param connection: an object implementing :meth:`write`, used to write
547 to the network.
cliechti7cb78e82009-08-05 15:47:57 +0000548 :param debug_output: used for development please set for False
cliechti7aed8332009-08-05 14:19:31 +0000549
550 Initializes the Manager and starts negotiating with client in Telnet
551 and :rfc:`2217` protocol. The negotiation starts immediately so that
552 the class should be instantiated in the moment the client connects.
553
554 The *serial_port* can be controlled by :rfc:`2217` commands. This
555 object will modify the port settings (baud rate etc) and control lines
556 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
557 the :meth:`filter` method.
558
559 The *connection* object must implement a :meth:`write(data)` function.
560 This function must ensure that *data* is written at once (no user data
561 mixed in, i.e. it must be thread-safe). All data must be sent in its
562 raw form (:meth:`escape` must not be used) as it is used to send Telnet
563 and :rfc:`2217` control commands.
564
565 .. method:: escape(data)
566
567 :param data: data to be sent over the network.
568 :return: data, escaped for Telnet/:rfc:`2217`
569
570 A generator that escapes all data to be compatible with :rfc:`2217`.
571 Implementors of servers should use this function to process all data
572 sent over the network.
573
574 The function returns a generator which can be used in ``for`` loops.
575 It can be converted to bytes using ``serial.to_bytes``.
576
577 .. method:: filter(data)
578
579 :param data: data read from the network, including Telnet and
580 :rfc:`2217` controls.
581 :return: data, free from Telnet and :rfc:`2217` controls.
582
583 A generator that filters and processes all data related to :rfc:`2217`.
584 Implementors of servers should use this function to process all data
585 received from the network.
586
587 The function returns a generator which can be used in ``for`` loops.
588 It can be converted to bytes using ``serial.to_bytes``.
589
cliechti7cb78e82009-08-05 15:47:57 +0000590 .. method:: check_modem_lines(force_notification=False)
591
592 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000593
594 This function needs to be called periodically (e.g. every second) when
595 the server wants to send NOTIFY_MODEMSTATE messages. This is required
596 to support the client for reading CTS/DSR/RI/CD status lines.
597
598 The function reads the status line and issues the notifications
599 automatically.
600
601 .. versionadded:: 2.5
602
cliechtiec4ac1b2009-08-02 00:47:21 +0000603.. seealso::
604
605 :rfc:`2217` - Telnet Com Port Control Option
606
607
cliechtic1c37602009-07-21 01:34:57 +0000608Exceptions
609==========
610
611.. exception:: SerialException
612
613 Base class for serial port exceptions.
614
cliechti4a567a02009-07-27 22:09:31 +0000615 .. versionchanged:: 2.5
616 Now derrives from :exc:`IOError` instead of :exc:`Exception`
617
cliechtic1c37602009-07-21 01:34:57 +0000618.. exception:: SerialTimeoutException
619
620 Exception that is raised on write timeouts.
621
622
623Constants
624=========
625
cliechti5134aab2009-07-21 19:47:59 +0000626Parity
cliechtic1c37602009-07-21 01:34:57 +0000627------
628.. data:: PARITY_NONE
629.. data:: PARITY_EVEN
630.. data:: PARITY_ODD
631.. data:: PARITY_MARK
632.. data:: PARITY_SPACE
633
cliechti4a567a02009-07-27 22:09:31 +0000634Stop bits
635---------
cliechtic1c37602009-07-21 01:34:57 +0000636.. data:: STOPBITS_ONE
637.. data:: STOPBITS_ONE_POINT_FIVE
638.. data:: STOPBITS_TWO
639
cliechti4a567a02009-07-27 22:09:31 +0000640Byte size
641---------
cliechtic1c37602009-07-21 01:34:57 +0000642.. data:: FIVEBITS
643.. data:: SIXBITS
644.. data:: SEVENBITS
645.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000646
647Others
648-------
cliechti8611bf42009-08-03 00:34:03 +0000649Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000650software flow control:
cliechti6066f842009-07-24 00:05:45 +0000651
cliechti5b7d16a2009-07-21 21:53:59 +0000652.. data:: XON
653.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000654
cliechti4a567a02009-07-27 22:09:31 +0000655Module version:
cliechtif81362e2009-07-25 03:44:33 +0000656
657.. data:: VERSION
658
659 A string indicating the pySerial version, such as ``2.5``.
660
661Functions:
662
663.. function:: device(number)
664
665 :param number: Port number.
666 :return: String containing device name.
667 :deprecated: Use device names directly.
668
669 Convert a port number to a platform dependent device name. Unfortunately
670 this does not work well for all platforms; e.g. some may miss USB-Serial
671 converters and enumerate only internal serial ports.
672
673 The conversion may be made off-line, that is, there is no guarantee that
674 the returned device name really exists on the system.
cliechti7c640ed2009-08-02 00:54:51 +0000675
676
cliechtie3ab3532009-08-05 12:40:38 +0000677.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +0000678
cliechtif4566342009-08-04 00:07:19 +0000679 :param url: Device name, number or URL_
cliechti7c640ed2009-08-02 00:54:51 +0000680 :param do_not_open: When set to true, the serial port is not opened.
681 :return: an instance of :class:`Serial` or a compatible object.
682
683 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +0000684 on port/url. This factory function is useful when an application wants
685 to support both, local ports and remote ports.
cliechti7c640ed2009-08-02 00:54:51 +0000686
687 When *url* matches the form ``rfc2217://<host>:<port>`` an instance of
688 :class:`rfc2217.Serial` is returned. In all other cases the native (system
689 dependant) :class:`Serial` instance is returned.
690
cliechtid7e7ce22009-08-03 02:01:07 +0000691 The port is not opened when a keyword parameter called *do_not_open* is
692 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +0000693
694 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +0000695
696.. _URL: URLs_
697
698URLs
699----
cliechtie3ab3532009-08-05 12:40:38 +0000700The class :class:`rfc2217.Serial` and the function :func:`serial_for_url`
cliechtiab90e072009-08-06 01:44:34 +0000701accept the following types URL:
cliechtif4566342009-08-04 00:07:19 +0000702
cliechtiab90e072009-08-06 01:44:34 +0000703- ``rfc2217://<host>:<port>[/<option>[/<option>]]``
704- ``socket://<host>:<port>[/<option>[/<option>]]``
cliechtif4566342009-08-04 00:07:19 +0000705
706(Future releases of pySerial might add more types).
707
cliechtiab90e072009-08-06 01:44:34 +0000708``rfc2217://``
709 Used to connect to :rfc:`2217` compatible servers. All serial port
710 functions are supported.
711
712 Supported options in the URL are:
713
714 - ``ign_set_control`` does not wait for acknowledges to SET_CONTROL. This
715 option can be used for non compliant servers (i.e. when getting an
716 ``remote rejected value for option 'control'`` error when connecting).
717
718 - ``poll_modem``: The client issues NOTIFY_MODEMSTATE requests when status
719 lines are read (CTS/DTR/RI/CD). Without this option it relies on the server
720 sending the notifications automatically (that's what the RFC suggests and
721 most servers do). Enable this option when :meth:`getCTS` does not work as
722 expected, i.e. for servers that do not send notifications.
723
724 - ``debug``: Prints diagnostic messages (not useful for end users).
725
726``socket://``
727 The purpose of this connection type is that applications using pySerial can
728 connect to TCP/IP to serial port converters that do not support :rfc:`2217`.
729
730 Uses a TCP/IP socket. All serial port settings, control and status lines
731 are ignored. Only data is transmitted and received.
732
733 Supported options in the URL are:
734
735 - ``debug``: Prints diagnostic messages (not useful for end users).
736
cliechtif4566342009-08-04 00:07:19 +0000737Examples::
738
739 rfc2217://localhost:7000
cliechti7cb78e82009-08-05 15:47:57 +0000740 rfc2217://localhost:7000/poll_modem
cliechtif4566342009-08-04 00:07:19 +0000741 rfc2217://localhost:7000/ign_set_control/debug