blob: c24355989016839453d5aebbbf19e4eb593ab289 [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:
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
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
cliechti7053b232009-08-10 01:37:34 +0000395 .. method:: getSettingsDict()
cliechti4065dce2009-08-10 00:55:46 +0000396
397 :return: a dictionary with current port settings.
398
399 Get a dictionary with port settings. This is useful to backup the
400 current settings so that a later point in time they can be restored
401 using :meth:`applySettingsDict`.
402
403 Note that control lines (RTS/DTR) are part of the settings.
404
405 .. versionadded:: 2.5
406
cliechti7053b232009-08-10 01:37:34 +0000407 .. method:: applySettingsDict(d)
cliechti4065dce2009-08-10 00:55:46 +0000408
409 :param d: a dictionary with port settings.
410
411 Applies a dictionary that was created by :meth:`getSettingsDict`. Only
412 changes are applied and when a key is missing it means that the setting
413 stays unchanged.
414
415 Note that control lines (RTS/DTR) are not changed.
416
417 .. versionadded:: 2.5
418
cliechtif4566342009-08-04 00:07:19 +0000419
cliechti4a567a02009-07-27 22:09:31 +0000420.. note::
421
422 For systems that provide the :mod:`io` library (Python 2.6 and newer), the
cliechtid7e7ce22009-08-03 02:01:07 +0000423 class :class:`Serial` will derive from :class:`io.RawIOBase`. For all
cliechti4a567a02009-07-27 22:09:31 +0000424 others from :class:`FileLike`.
425
426.. class:: FileLike
427
428 An abstract file like class. It is used as base class for :class:`Serial`.
429
430 This class implements :meth:`readline` and :meth:`readlines` based on read
431 and :meth:`writelines` based on write. This class is used to provide the
432 above functions for to Serial port objects.
433
434 Note that when the serial port was opened with no timeout that
435 :meth:`readline` blocks until it sees a newline (or the specified size is
436 reached) and that :meth:`readlines` would never return and therefore
437 refuses to work (it raises an exception in this case)!
438
439 .. method:: writelines(sequence)
440
441 Write a list of strings to the port.
442
443
444 The following three methods are overridden in :class:`Serial`.
445
446 .. method:: flush()
447
448 Flush of file like objects. It's a no-op in this class, may be overridden.
449
450 .. method:: read()
451
452 Raises NotImplementedError, needs to be overridden by subclass.
453
454 .. method:: write(data)
455
456 Raises NotImplementedError, needs to be overridden by subclass.
457
458 The following functions are implemented for compatibility with other
459 file-like objects, however serial ports are not seekable.
460
461
462 .. method:: seek(pos, whence=0)
463
464 :exception IOError: always, as method is not supported on serial port
465
466 .. versionadded:: 2.5
467
468 .. method:: tell()
469
470 :exception IOError: always, as method is not supported on serial port
471
472 .. versionadded:: 2.5
473
474 .. method:: truncate(self, n=None)
475
476 :exception IOError: always, as method is not supported on serial port
477
478 .. versionadded:: 2.5
479
480 .. method:: isatty()
481
482 :exception IOError: always, as method is not supported on serial port
483
484 .. versionadded:: 2.5
485
486 To be able to use the file like object as iterator for e.g.
487 ``for line in Serial(0): ...`` usage:
488
489 .. method:: next()
490
491 Return the next line by calling :meth:`readline`.
492
493 .. method:: __iter__()
494
495 Returns self.
496
cliechti7aed8332009-08-05 14:19:31 +0000497:rfc:`2217` Network ports
498-------------------------
499
500.. warning:: This implementation is currently in an experimental state. Use
501 at your own risk.
cliechtiedcdbe42009-07-22 00:48:34 +0000502
cliechtiec4ac1b2009-08-02 00:47:21 +0000503.. class:: rfc2217.Serial
504
cliechtif4566342009-08-04 00:07:19 +0000505 This implements a :rfc:`2217` compatible client. Port names are URLs_ in the
506 form: ``rfc2217://<host>:<port>[/<option>[/<option>]]``
cliechtiec4ac1b2009-08-02 00:47:21 +0000507
cliechtiec4ac1b2009-08-02 00:47:21 +0000508 This class API is compatible to :class:`Serial` with a few exceptions:
509
510 - numbers as port name are not allowed, only URLs in the form described
511 above.
512 - writeTimeout is not implemented
513 - The current implementation starts a thread that keeps reading from the
514 (internal) socket. The thread is managed automatically by the
515 :class:`rfc2217.Serial` port object on :meth:`open`/:meth:`close`.
516 However it may be a problem for user applications that like to use select
517 instead of threads.
518
519 Due to the nature of the network and protocol involved there are a few
520 extra points to keep in mind:
521
522 - All operations have an additional latency time.
523 - Setting control lines (RTS/CTS) needs more time.
524 - Reading the status lines (DSR/DTR etc.) returns a cached value. When that
525 cache is updated depends entirely on the server. The server itself may
526 implement a polling at a certain rate and quick changes may be invisible.
527 - The network layer also has buffers. This means that :meth:`flush`,
528 :meth:`flushInput` and :meth:`flushOutput` may work with additional delay.
529 Likewise :meth:`inWaiting` returns the size of the data arrived at the
530 object internal buffer and excludes any bytes in the network buffers or
531 any server side buffer.
532 - Closing and immediately reopening the same port may fail due to time
533 needed by the server to get ready again.
534
535 Not implemented yet / Possible problems with the implementation:
536
cliechti8611bf42009-08-03 00:34:03 +0000537 - :rfc:`2217` flow control between client and server (objects internal
538 buffer may eat all your memory when never read).
cliechtid7e7ce22009-08-03 02:01:07 +0000539 - No authentication support (servers may not prompt for a password etc.)
540 - No encryption.
541
542 Due to lack of authentication and encryption it is not suitable to use this
543 client for connections across the internet and should only be used in
544 controlled environments.
cliechtiec4ac1b2009-08-02 00:47:21 +0000545
cliechti7c640ed2009-08-02 00:54:51 +0000546 .. versionadded:: 2.5
cliechtiec4ac1b2009-08-02 00:47:21 +0000547
cliechti7aed8332009-08-05 14:19:31 +0000548
549.. class:: rfc2217.PortManager
550
551 This class provides helper functions for implementing :rfc:`2217`
552 compatible servers.
553
554 Basically, it implements every thing needed for the :rfc:`2217` protocol.
555 It just does not open sockets and read/write to serial ports (though it
556 changes other port settings). The user of this class must take care of the
557 data transmission itself. The reason for that is, that this way, this class
558 supports all programming models such as threads and select.
559
560 Usage examples can be found in the examples where two TCP/IP - serial
561 converters are shown, one using threads (the single port server) and an
562 other using select (the multi port server).
563
564 .. note:: Each new client connection must create a new instance as this
565 object (and the :rfc:`2217` protocol) has internal state.
566
567 .. method:: __init__(serial_port, connection, debug_output=False)
568
569 :param serial_port: a :class:`Serial` instance that is managed.
570 :param connection: an object implementing :meth:`write`, used to write
571 to the network.
cliechti86844e82009-08-12 00:05:33 +0000572 :param debug_output: enables debug messages: a :class:`logging.Logger`
573 instance or None.
cliechti7aed8332009-08-05 14:19:31 +0000574
575 Initializes the Manager and starts negotiating with client in Telnet
576 and :rfc:`2217` protocol. The negotiation starts immediately so that
577 the class should be instantiated in the moment the client connects.
578
579 The *serial_port* can be controlled by :rfc:`2217` commands. This
580 object will modify the port settings (baud rate etc) and control lines
581 (RTS/DTR) send BREAK etc. when the corresponding commands are found by
582 the :meth:`filter` method.
583
584 The *connection* object must implement a :meth:`write(data)` function.
585 This function must ensure that *data* is written at once (no user data
586 mixed in, i.e. it must be thread-safe). All data must be sent in its
587 raw form (:meth:`escape` must not be used) as it is used to send Telnet
588 and :rfc:`2217` control commands.
589
cliechti86844e82009-08-12 00:05:33 +0000590 For diagnostics of the connection or the implementation, *debug_output*
591 can be set to an instance of a :class:`logging.Logger` (e.g.
592 ``logging.getLogger('rfc2217.server')``). The caller should configure
593 the logger using ``setLevel`` for the desired detail level of the logs.
594
cliechti7aed8332009-08-05 14:19:31 +0000595 .. method:: escape(data)
596
597 :param data: data to be sent over the network.
598 :return: data, escaped for Telnet/:rfc:`2217`
599
600 A generator that escapes all data to be compatible with :rfc:`2217`.
601 Implementors of servers should use this function to process all data
602 sent over the network.
603
604 The function returns a generator which can be used in ``for`` loops.
605 It can be converted to bytes using ``serial.to_bytes``.
606
607 .. method:: filter(data)
608
609 :param data: data read from the network, including Telnet and
610 :rfc:`2217` controls.
611 :return: data, free from Telnet and :rfc:`2217` controls.
612
613 A generator that filters and processes all data related to :rfc:`2217`.
614 Implementors of servers should use this function to process all data
615 received from the network.
616
617 The function returns a generator which can be used in ``for`` loops.
618 It can be converted to bytes using ``serial.to_bytes``.
619
cliechti7cb78e82009-08-05 15:47:57 +0000620 .. method:: check_modem_lines(force_notification=False)
621
622 :param force_notification: Set to false. Parameter is for internal use.
cliechti7aed8332009-08-05 14:19:31 +0000623
624 This function needs to be called periodically (e.g. every second) when
625 the server wants to send NOTIFY_MODEMSTATE messages. This is required
626 to support the client for reading CTS/DSR/RI/CD status lines.
627
628 The function reads the status line and issues the notifications
629 automatically.
630
631 .. versionadded:: 2.5
632
cliechtiec4ac1b2009-08-02 00:47:21 +0000633.. seealso::
634
635 :rfc:`2217` - Telnet Com Port Control Option
636
637
cliechtic1c37602009-07-21 01:34:57 +0000638Exceptions
639==========
640
641.. exception:: SerialException
642
643 Base class for serial port exceptions.
644
cliechti4a567a02009-07-27 22:09:31 +0000645 .. versionchanged:: 2.5
646 Now derrives from :exc:`IOError` instead of :exc:`Exception`
647
cliechtic1c37602009-07-21 01:34:57 +0000648.. exception:: SerialTimeoutException
649
650 Exception that is raised on write timeouts.
651
652
653Constants
654=========
655
cliechti87686242009-08-18 00:58:31 +0000656*Parity*
657
cliechtic1c37602009-07-21 01:34:57 +0000658.. data:: PARITY_NONE
659.. data:: PARITY_EVEN
660.. data:: PARITY_ODD
661.. data:: PARITY_MARK
662.. data:: PARITY_SPACE
663
cliechti87686242009-08-18 00:58:31 +0000664*Stop bits*
665
cliechtic1c37602009-07-21 01:34:57 +0000666.. data:: STOPBITS_ONE
667.. data:: STOPBITS_ONE_POINT_FIVE
668.. data:: STOPBITS_TWO
669
cliechti87686242009-08-18 00:58:31 +0000670*Byte size*
671
cliechtic1c37602009-07-21 01:34:57 +0000672.. data:: FIVEBITS
673.. data:: SIXBITS
674.. data:: SEVENBITS
675.. data:: EIGHTBITS
cliechti5b7d16a2009-07-21 21:53:59 +0000676
cliechti87686242009-08-18 00:58:31 +0000677
678*Others*
679
cliechti8611bf42009-08-03 00:34:03 +0000680Default control characters (instances of :class:`bytes` for Python 3.0+) for
cliechtidaf47ba2009-07-28 01:28:16 +0000681software flow control:
cliechti6066f842009-07-24 00:05:45 +0000682
cliechti5b7d16a2009-07-21 21:53:59 +0000683.. data:: XON
684.. data:: XOFF
cliechtif81362e2009-07-25 03:44:33 +0000685
cliechti4a567a02009-07-27 22:09:31 +0000686Module version:
cliechtif81362e2009-07-25 03:44:33 +0000687
688.. data:: VERSION
689
690 A string indicating the pySerial version, such as ``2.5``.
691
cliechti87686242009-08-18 00:58:31 +0000692 .. versionadded:: 2.3
693
694Module functions
695================
cliechtif81362e2009-07-25 03:44:33 +0000696
697.. function:: device(number)
698
699 :param number: Port number.
700 :return: String containing device name.
701 :deprecated: Use device names directly.
702
703 Convert a port number to a platform dependent device name. Unfortunately
704 this does not work well for all platforms; e.g. some may miss USB-Serial
705 converters and enumerate only internal serial ports.
706
707 The conversion may be made off-line, that is, there is no guarantee that
708 the returned device name really exists on the system.
cliechti7c640ed2009-08-02 00:54:51 +0000709
710
cliechtie3ab3532009-08-05 12:40:38 +0000711.. function:: serial_for_url(url, \*args, \*\*kwargs)
cliechti7c640ed2009-08-02 00:54:51 +0000712
cliechti86844e82009-08-12 00:05:33 +0000713 :param url: Device name, number or :ref:`URL <URLs>`
cliechti7c640ed2009-08-02 00:54:51 +0000714 :param do_not_open: When set to true, the serial port is not opened.
715 :return: an instance of :class:`Serial` or a compatible object.
716
717 Get a native or a :rfc:`2217` implementation of the Serial class, depending
cliechtid7e7ce22009-08-03 02:01:07 +0000718 on port/url. This factory function is useful when an application wants
719 to support both, local ports and remote ports.
cliechti7c640ed2009-08-02 00:54:51 +0000720
721 When *url* matches the form ``rfc2217://<host>:<port>`` an instance of
722 :class:`rfc2217.Serial` is returned. In all other cases the native (system
723 dependant) :class:`Serial` instance is returned.
724
cliechtid7e7ce22009-08-03 02:01:07 +0000725 The port is not opened when a keyword parameter called *do_not_open* is
726 given and true, by default it is opened.
cliechti7c640ed2009-08-02 00:54:51 +0000727
728 .. versionadded:: 2.5
cliechtif4566342009-08-04 00:07:19 +0000729
cliechti87686242009-08-18 00:58:31 +0000730
cliechti86844e82009-08-12 00:05:33 +0000731.. _URLs:
cliechtif4566342009-08-04 00:07:19 +0000732
733URLs
734----
cliechtie3ab3532009-08-05 12:40:38 +0000735The class :class:`rfc2217.Serial` and the function :func:`serial_for_url`
cliechtiab90e072009-08-06 01:44:34 +0000736accept the following types URL:
cliechtif4566342009-08-04 00:07:19 +0000737
cliechtiab90e072009-08-06 01:44:34 +0000738- ``rfc2217://<host>:<port>[/<option>[/<option>]]``
739- ``socket://<host>:<port>[/<option>[/<option>]]``
cliechti41973a92009-08-06 02:18:21 +0000740- ``loop://[<option>[/<option>]]``
cliechtif4566342009-08-04 00:07:19 +0000741
cliechti87686242009-08-18 00:58:31 +0000742Device names are also supported:
743
744- ``/dev/ttyUSB0`` (Linux)
745- ``COM3`` (Windows)
746
cliechtif4566342009-08-04 00:07:19 +0000747(Future releases of pySerial might add more types).
748
cliechtiab90e072009-08-06 01:44:34 +0000749``rfc2217://``
750 Used to connect to :rfc:`2217` compatible servers. All serial port
751 functions are supported.
752
753 Supported options in the URL are:
754
755 - ``ign_set_control`` does not wait for acknowledges to SET_CONTROL. This
756 option can be used for non compliant servers (i.e. when getting an
757 ``remote rejected value for option 'control'`` error when connecting).
758
759 - ``poll_modem``: The client issues NOTIFY_MODEMSTATE requests when status
760 lines are read (CTS/DTR/RI/CD). Without this option it relies on the server
761 sending the notifications automatically (that's what the RFC suggests and
762 most servers do). Enable this option when :meth:`getCTS` does not work as
763 expected, i.e. for servers that do not send notifications.
764
cliechtidfe2d272009-08-10 22:19:41 +0000765 - ``timeout=<value>``: Change network timeout (default 3 seconds). This is
766 useful when the server takes a little more time to send its answers. The
767 timeout applies to the initial Telnet / :rfc:`2271` negotiation as well
768 as changing port settings or control line change commands.
769
cliechti86844e82009-08-12 00:05:33 +0000770 - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not
cliechtic64ba692009-08-12 00:32:47 +0000771 useful for end users). It uses the logging module and a logger called
772 ``pySerial.rfc2217`` so that the application can setup up logging
773 handlers etc. It will call :meth:`logging.basicConfig` which initializes
774 for output on ``sys.stderr`` (if no logging was set up already).
cliechtiab90e072009-08-06 01:44:34 +0000775
776``socket://``
777 The purpose of this connection type is that applications using pySerial can
778 connect to TCP/IP to serial port converters that do not support :rfc:`2217`.
779
780 Uses a TCP/IP socket. All serial port settings, control and status lines
781 are ignored. Only data is transmitted and received.
782
783 Supported options in the URL are:
784
cliechtic64ba692009-08-12 00:32:47 +0000785 - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not
786 useful for end users). It uses the logging module and a logger called
787 ``pySerial.socket`` so that the application can setup up logging handlers
788 etc. It will call :meth:`logging.basicConfig` which initializes for
789 output on ``sys.stderr`` (if no logging was set up already).
cliechtiab90e072009-08-06 01:44:34 +0000790
cliechti41973a92009-08-06 02:18:21 +0000791``loop://``
792 The least useful type. It simulates a loop back connection.
793 ``RX<->TX`` ``RTS<->CTS`` ``DTR<->DSR``
794
795 Supported options in the URL are:
796
cliechtic64ba692009-08-12 00:32:47 +0000797 - ``logging=[debug|info|warning|error]``: Prints diagnostic messages (not
798 useful for end users). It uses the logging module and a logger called
799 ``pySerial.loop`` so that the application can setup up logging handlers
800 etc. It will call :meth:`logging.basicConfig` which initializes for
801 output on ``sys.stderr`` (if no logging was set up already).
cliechti41973a92009-08-06 02:18:21 +0000802
803
cliechtidfe2d272009-08-10 22:19:41 +0000804Examples:
cliechtif4566342009-08-04 00:07:19 +0000805
cliechtidfe2d272009-08-10 22:19:41 +0000806- ``rfc2217://localhost:7000``
807- ``rfc2217://localhost:7000/poll_modem``
808- ``rfc2217://localhost:7000/ign_set_control/timeout=5.5``
809- ``socket://localhost:7777``
cliechtic64ba692009-08-12 00:32:47 +0000810- ``loop://logging=debug``