Overview¶
Python serial.SerialTimeoutException Examples. The following are code examples for showing how to use serial.SerialTimeoutException. They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.
The function
serial_for_url()
accepts the following types of URLs:rfc2217://<host>:<port>[?<option>[&<option>...]]
socket://<host>:<port>[?logging={debug|info|warning|error}]
loop://[?logging={debug|info|warning|error}]
hwgrep://<regexp>[&skip_busy][&n=N]
spy://port[?option[=value][&option[=value]]]
alt://port?class=<classname>
Changed in version 3.0: Options are specified with
?
and &
instead of /
Device names are also supported, e.g.:
/dev/ttyUSB0
(Linux)COM3
(Windows)
Future releases of pySerial might add more types. Since pySerial 2.6 it is alsopossible for the user to add protocol handlers using
protocol_handler_packages
.rfc2217://
¶
Used to connect to RFC 2217 compatible servers. All serial portfunctions are supported. Implemented by
rfc2217.Serial
.Supported options in the URL are:
ign_set_control
does not wait for acknowledges to SET_CONTROL. Thisoption can be used for non compliant servers (i.e. when getting anremoterejectedvalueforoption'control'
error when connecting).poll_modem
: The client issues NOTIFY_MODEMSTATE requests when statuslines are read (CTS/DTR/RI/CD). Without this option it relies on the serversending the notifications automatically (that’s what the RFC suggests andmost servers do). Enable this option whencts
does not work asexpected, i.e. for servers that do not send notifications.timeout=<value>
: Change network timeout (default 3 seconds). This isuseful when the server takes a little more time to send its answers. Thetimeout applies to the initial Telnet / RFC 2271 negotiation as wellas changing port settings or control line change commands.logging={debug|info|warning|error}
: Prints diagnostic messages (notuseful for end users). It uses the logging module and a logger calledpySerial.rfc2217
so that the application can setup up logginghandlers etc. It will calllogging.basicConfig()
which initializesfor output onsys.stderr
(if no logging was set up already).
Warning
The connection is not encrypted and no authentication issupported! Only use it in trusted environments.
socket://
¶
The purpose of this connection type is that applications using pySerial canconnect 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 linesare ignored. Only data is transmitted and received.
Supported options in the URL are:
logging={debug|info|warning|error}
: Prints diagnostic messages (notuseful for end users). It uses the logging module and a logger calledpySerial.socket
so that the application can setup up logging handlersetc. It will calllogging.basicConfig()
which initializes foroutput onsys.stderr
(if no logging was set up already).
Warning
The connection is not encrypted and no authentication issupported! Only use it in trusted environments.
loop://
¶
The least useful type. It simulates a loop back connection(
RX<->TX
RTS<->CTS
DTR<->DSR
). It could be used to testapplications or run the unit tests.Supported options in the URL are:
logging={debug|info|warning|error}
: Prints diagnostic messages (notuseful for end users). It uses the logging module and a logger calledpySerial.loop
so that the application can setup up logging handlersetc. It will calllogging.basicConfig()
which initializes foroutput onsys.stderr
(if no logging was set up already).
hwgrep://
¶
This type uses
serial.tools.list_ports
to obtain a list of ports andsearches the list for matches by a regexp that follows the slashes (see Pythonsre
module for detailed syntax information).Note that options are separated using the character
&
, this also applies tothe first, where URLs usually use ?
. This exception is made as the questionmark is used in regexp itself.Depending on the capabilities of the
list_ports
module on the system, it ispossible to search for the description or hardware ID of a device, e.g. USBVID:PID or texts.Unfortunately, on some systems
list_ports
only lists a subset of the portnames with no additional information. Currently, on Windows and Linux andOSX it should find additional information.Supported options in the URL are:
n=N
: pick the N’th entry instead of the firstskip_busy
: skip ports that can not be opened, e.g. because they arealready in use. This may not work as expected on platforms where the file isnot locked automatically (e.g. Posix).
spy://
¶
Wrapping the native serial port, this protocol makes it possible tointercept the data received and transmitted as well as the access to thecontrol lines, break and flush commands. It is mainly used to debugapplications.
Supported options in the URL are:
file=FILENAME
output to given file or device instead of stderrcolor
enable ANSI escape sequences to colorize outputraw
output the read and written data directly (default is to create ahex dump). In this mode, no control line and other commands are logged.all
also showin_waiting
and emptyread()
calls (hidden bydefault because of high traffic).
Example:
Outputs:
Another example, on POSIX, open a second terminal window and find out it’sdevice (e.g. with the
ps
command in the TTY column), assumed to be/dev/pts/2
here, double quotes are used so that the ampersand in the URL isnot interpreted by the shell:The spy output will be live in the second terminal window.
New in version 3.0.
alt://
¶
This handler allows to select alternate implementations of the native serialport.
Currently only the POSIX platform provides alternative implementations.
PosixPollSerial
- Poll based read implementation. Not all systems support poll properly.However this one has better handling of errors, such as a devicedisconnecting while it’s in use (e.g. USB-serial unplugged).
VTIMESerial
- Implement timeout using
VTIME
/VMIN
of TTY device instead of usingselect
. This means that inter character timeout and overall timeoutcan not be used at the same time. Overall timeout is disabled wheninter-character timeout is used. The error handling is degraded.
Examples:
New in version 3.0.
Examples¶
rfc2217://localhost:7000
rfc2217://localhost:7000?poll_modem
rfc2217://localhost:7000?ign_set_control&timeout=5.5
socket://localhost:7777
loop://?logging=debug
hwgrep://0451:f432
(USB VID:PID)spy://COM54?file=log.txt
alt:///dev/ttyUSB0?class=PosixPollSerial
Bases:
object
Instantiate a Serial object and open the tty device at the specifiedpath with the specified baudrate, and the defaults of 8 data bits, noparity, 1 stop bit, no software flow control (xonxoff), and no hardwareflow control (rtscts).
Parameters: |
|
---|---|
Returns: | Serial object. |
Return type: | |
Raises: |
|
read
(length, timeout=None)[source]¶Read up to length number of bytes from the serial port with anoptional timeout.
timeout can be positive for a timeout in seconds, 0 for anon-blocking read, or negative or None for a blocking read that willblock until length number of bytes are read. Default is a blockingread.
For a non-blocking or timeout-bound read, read() may return data whoselength is less than or equal to the requested length.
Parameters: |
|
---|---|
Returns: | data read. |
Return type: | bytes |
Raises: | SerialError – if an I/O or OS error occurs. |
write
(data)[source]¶Write data to the serial port and return the number of byteswritten.
Parameters: | data (bytes, bytearray, list) – a byte array or list of 8-bit integers to write. |
---|---|
Returns: | number of bytes written. |
Return type: | int |
Raises: |
|
poll
(timeout=None)[source]¶Poll for data available for reading from the serial port.
timeout can be positive for a timeout in seconds, 0 for anon-blocking poll, or negative or None for a blocking poll. Default isa blocking poll.
Parameters: | timeout (int, float, None) – timeout duration in seconds. |
---|---|
Returns: | True if data is available for reading from the serial port, False if not. |
Return type: | bool |
flush
()[source]¶Flush the write buffer of the serial port, blocking until all bytesare written.
Raises: | SerialError – if an I/O or OS error occurs. |
---|
input_waiting
()[source]¶Query the number of bytes waiting to be read from the serial port.
Returns: | number of bytes waiting to be read. |
---|---|
Return type: | int |
Raises: | SerialError – if an I/O or OS error occurs. |
output_waiting
()[source]¶Query the number of bytes waiting to be written to the serial port.
Returns: | number of bytes waiting to be written. |
---|---|
Return type: | int |
Raises: | SerialError – if an I/O or OS error occurs. |
close
()[source]¶Close the tty device.
Raises: | SerialError – if an I/O or OS error occurs. |
---|
fd
¶Get the file descriptor of the underlying tty device.
Type: | int |
---|
![Serial Serial](/uploads/1/2/6/4/126416842/378943506.jpg)
devpath
¶Get the device path of the underlying tty device.
Type: | str |
---|
baudrate
¶Get or set the baudrate.
Raises: |
|
---|---|
Type: | int |
databits
¶Get or set the data bits. Can be 5, 6, 7, 8.
Raises: |
|
---|---|
Type: | int |
parity
¶Get or set the parity. Can be “none”, “even”, “odd”.
Raises: |
|
---|---|
Type: | str |
stopbits
¶Get or set the stop bits. Can be 1 or 2.
Raises: |
|
---|---|
Type: | int |
xonxoff
¶Get or set software flow control.
Raises: |
|
---|---|
Type: | bool |
rtscts
¶Get or set hardware flow control.
Raises: |
|
---|---|
Type: | bool |