| Parameters: |
|
|---|---|
| Raises ValueError: | |
Will be raised when parameter are out of range, e.g. baud rate, data bits. |
|
| Raises SerialException: | |
In case the device can not be found or can not be configured. |
|
The port is immediately opened on object creation, when a port is given. It is not opened when port is None and a successive call to open() will be needed.
Possible values for the parameter port:
Possible values for the parameter timeout:
Note that enabling both flow control methods (xonxoff and rtscts) together may not be supported. It is common to use one of the methods at once, not both.
The following methods may raise ValueError when applied to a closed port.
| Parameter: | size – Number of bytes to read. |
|---|---|
| Returns: | Bytes read from the port. |
Read size bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read.
Changed in version 2.5: Returns an instance of bytes when available (Python 2.6 and newer) and str otherwise.
| Parameter: | data – Data to send. |
|---|---|
| Returns: | Number of bytes written. |
| Raises SerialTimeoutException: | |
| In case a write timeout is configured for the port and the time is exceeded. | |
Write the string data to the port.
Changed in version 2.5: Accepts instances of bytes and bytearray when available (Python 2.6 and newer) and str otherwise.
| Parameter: | duration – Time (float) to activate the BREAK condition. |
|---|
Send break condition. Timed, returns to idle state after given duration.
| Parameter: | level – when true activate BREAK condition, else disable. |
|---|
Set break: Controls TXD. When active, no transmitting is possible.
| Parameter: | level – Set control line to logic level. |
|---|
Set RTS line to specified logic level.
| Parameter: | level – Set control line to logic level. |
|---|
Set DTR line to specified logic level.
| Returns: | Current state (boolean) |
|---|
Return the state of the CTS line.
| Returns: | Current state (boolean) |
|---|
Return the state of the DSR line.
| Returns: | Current state (boolean) |
|---|
Return the state of the RI line.
| Returns: | Current state (boolean) |
|---|
Return the state of the CD line
Read-only attributes:
Device name. This is always the device name even if the port was opened by a number. (Read Only).
New in version 2.5.
New values can be assigned to the following attributes, the port will be reconfigured, even if it’s opened at that time:
Platform specific methods.
Warning
Programs using the following methods are not portable to other platforms!
| Platform: | Unix |
|---|
Configure the device for nonblocking operations. This can be useful if the port is used with select.
| Platform: | Unix |
|---|---|
| Returns: | File descriptor. |
Return file descriptor number for the port that is opened by this object.
| Platform: | Windows |
|---|---|
| Parameter: | level – Set flow control state. |
Set software flow control state.
The following attributes are implemented as properties. They work with open and closed ports.
The following constants are also provided:
| Parameters: |
|
|---|
Read a line which is terminated with end-of-line (eol) character (\\n by default) or until timeout.
| Parameters: |
|
|---|
Read a list of lines, until timeout. sizehint is ignored and only present for API compatibility with built-in File objects.
For compatibility with the io library are the following methods.
| Returns: | True |
|---|
New in version 2.5.
| Returns: | True |
|---|
New in version 2.5.
| Returns: | False |
|---|
New in version 2.5.
| Parameter: | b – bytearray or array instance |
|---|---|
| Returns: | Number of byte read |
Read up to len(b) bytes into bytearray b and return the number of bytes read.
New in version 2.5.
| Returns: | a dictionary with current port settings. |
|---|
Get a dictionary with port settings. This is useful to backup the current settings so that a later point in time they can be restored using applySettingsDict().
Note that control lines (RTS/DTR) are part of the settings.
New in version 2.5.
| Parameter: | d – a dictionary with port settings. |
|---|
Applies a dictionary that was created by getSettingsDict(). Only changes are applied and when a key is missing it means that the setting stays unchanged.
Note that control lines (RTS/DTR) are not changed.
New in version 2.5.
Note
For systems that provide the io library (Python 2.6 and newer), the class Serial will derive from io.RawIOBase. For all others from FileLike.
An abstract file like class. It is used as base class for Serial.
This class implements readline() and readlines() based on read and writelines() based on write. This class is used to provide the above functions for to Serial port objects.
Note that when the serial port was opened with no timeout that readline() blocks until it sees a newline (or the specified size is reached) and that readlines() would never return and therefore refuses to work (it raises an exception in this case)!
The following three methods are overridden in Serial.
The following functions are implemented for compatibility with other file-like objects, however serial ports are not seekable.
| Raises IOError: | always, as method is not supported on serial port |
|---|
New in version 2.5.
| Raises IOError: | always, as method is not supported on serial port |
|---|
New in version 2.5.
| Raises IOError: | always, as method is not supported on serial port |
|---|
New in version 2.5.
| Raises IOError: | always, as method is not supported on serial port |
|---|
New in version 2.5.
To be able to use the file like object as iterator for e.g. for line in Serial(0): ... usage:
Warning
This implementation is currently in an experimental state. Use at your own risk.
This implements a RFC 2217 compatible client. Port names are URLs in the form: rfc2217://<host>:<port>[/<option>[/<option>]]
This class API is compatible to Serial with a few exceptions:
Due to the nature of the network and protocol involved there are a few extra points to keep in mind:
Not implemented yet / Possible problems with the implementation:
Due to lack of authentication and encryption it is not suitable to use this client for connections across the internet and should only be used in controlled environments.
New in version 2.5.
This class provides helper functions for implementing RFC 2217 compatible servers.
Basically, it implements every thing needed for the RFC 2217 protocol. It just does not open sockets and read/write to serial ports (though it changes other port settings). The user of this class must take care of the data transmission itself. The reason for that is, that this way, this class supports all programming models such as threads and select.
Usage examples can be found in the examples where two TCP/IP - serial converters are shown, one using threads (the single port server) and an other using select (the multi port server).
Note
Each new client connection must create a new instance as this object (and the RFC 2217 protocol) has internal state.
| Parameters: |
|
|---|
Initializes the Manager and starts negotiating with client in Telnet and RFC 2217 protocol. The negotiation starts immediately so that the class should be instantiated in the moment the client connects.
The serial_port can be controlled by RFC 2217 commands. This object will modify the port settings (baud rate etc) and control lines (RTS/DTR) send BREAK etc. when the corresponding commands are found by the filter() method.
The connection object must implement a write(data)() function. This function must ensure that data is written at once (no user data mixed in, i.e. it must be thread-safe). All data must be sent in its raw form (escape() must not be used) as it is used to send Telnet and RFC 2217 control commands.
For diagnostics of the connection or the implementation, debug_output can be set to an instance of a logging.Logger (e.g. logging.getLogger('rfc2217.server')). The caller should configure the logger using setLevel for the desired detail level of the logs.
| Parameter: | data – data to be sent over the network. |
|---|---|
| Returns: | data, escaped for Telnet/RFC 2217 |
A generator that escapes all data to be compatible with RFC 2217. Implementors of servers should use this function to process all data sent over the network.
The function returns a generator which can be used in for loops. It can be converted to bytes using serial.to_bytes.
| Parameter: | data – data read from the network, including Telnet and RFC 2217 controls. |
|---|---|
| Returns: | data, free from Telnet and RFC 2217 controls. |
A generator that filters and processes all data related to RFC 2217. Implementors of servers should use this function to process all data received from the network.
The function returns a generator which can be used in for loops. It can be converted to bytes using serial.to_bytes.
| Parameter: | force_notification – Set to false. Parameter is for internal use. |
|---|
This function needs to be called periodically (e.g. every second) when the server wants to send NOTIFY_MODEMSTATE messages. This is required to support the client for reading CTS/DSR/RI/CD status lines.
The function reads the status line and issues the notifications automatically.
New in version 2.5.
See also
RFC 2217 - Telnet Com Port Control Option
Base class for serial port exceptions.
Changed in version 2.5: Now derrives from IOError instead of Exception
Default control characters (instances of bytes for Python 3.0+) for software flow control:
Module version:
Functions:
| Parameter: | number – Port number. |
|---|---|
| Returns: | String containing device name. |
| Deprecated: | Use device names directly. |
Convert a port number to a platform dependent device name. Unfortunately this does not work well for all platforms; e.g. some may miss USB-Serial converters and enumerate only internal serial ports.
The conversion may be made off-line, that is, there is no guarantee that the returned device name really exists on the system.
| Parameters: |
|
|---|---|
| Returns: | an instance of Serial or a compatible object. |
Get a native or a RFC 2217 implementation of the Serial class, depending on port/url. This factory function is useful when an application wants to support both, local ports and remote ports.
When url matches the form rfc2217://<host>:<port> an instance of rfc2217.Serial is returned. In all other cases the native (system dependant) Serial instance is returned.
The port is not opened when a keyword parameter called do_not_open is given and true, by default it is opened.
New in version 2.5.
The class rfc2217.Serial and the function serial_for_url() accept the following types URL:
(Future releases of pySerial might add more types).
Used to connect to RFC 2217 compatible servers. All serial port functions are supported.
Supported options in the URL are:
The purpose of this connection type is that applications using pySerial can connect to TCP/IP to serial port converters that do not support RFC 2217.
Uses a TCP/IP socket. All serial port settings, control and status lines are ignored. Only data is transmitted and received.
Supported options in the URL are:
The least useful type. It simulates a loop back connection. RX<->TX RTS<->CTS DTR<->DSR
Supported options in the URL are:
Examples: