Python Serial Timeout Example
Bases: object
- Python Serial Disable Timeout
- Python Serial Readline Timeout
- Python Serial Timeout Example Python
- Python Serial Time Out Example Paragraph
- Python Serial Timeout Example Software
Project: spi-flash-programmer Author: nfd File: spiflashprogrammerclient.py License: Creative. The following are 30 code examples for showing how to use serial.PARITYNONE. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the. CGI example and TinyWeb weight together only 375 kb with Python 2.2!). Hint 3: When serving files (not CGI), TinyWeb uses Windows file extension Return type: bytes Read size bytes from the serial port. If a timeout is set it may return less characters as requested. Now I am trying to achieve the same thing using Python serial connector.
- A key parameter in the pyserial Serial class is the timeout parameter. This parameter is defined as: timeout=None, #set a timeout value, None for waiting forever The Serial class read function also accepts a size parameter that indicates how many characters should be read.
- The following are 24 code examples for showing how to use serial.serialforurl.These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
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 blocking read with a timeout inseconds, zero for a non-blocking read, or negative or None for ablocking read that will block until length number of bytes are read.Default is a blocking read.
For a non-blocking or timeout-bound read, read() may return less thanthe requested number of bytes.
For a blocking read with the VMIN setting configured, read() willblock until at least VMIN bytes are read. For a blocking read with bothVMIN and VTIME settings configured, read() will block until at leastVMIN bytes are read or the VTIME interbyte timeout expires after thelast byte read. In either case, read() may return less than therequested number of bytes.
Parameters: |
|
---|---|
Returns: | data read. |
Return type: | bytes |
Raises: |
|
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 with anoptional timeout.
timeout can be positive for a timeout in seconds, zero 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
¶Python Serial Disable Timeout
Get the file descriptor of the underlying tty device.
Type: | int |
---|
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 |
vmin
¶Get or set the VMIN termios setting for minimum number of bytes returnedfrom a blocking read. Can be between 0 and 255.
When configured in conjunction with VTIME, VTIME acts as an interbytetimeout that restarts on every byte received, and a blocking read willblock until at least VMIN bytes are read or the VTIME timeout expires afterthe last byte read. See the termios man page for more information.
Raises: |
|
---|---|
Type: | int |
vtime
¶Get or set the VTIME termios setting for timeout in seconds of ablocking read. Can be between 0 to 25.5 seconds, with a resolution of 0.1seconds.
When configured in conjunction with VMIN, VTIME acts as an interbytetimeout that restarts on every byte received, and a blocking read willblock until at least VMIN bytes are read or the VTIME timeout expires afterthe last byte read. See the termios man page for more information.
Raises: |
|
---|---|
Type: | float |
Python Serial Readline Timeout
Miniterm¶
Miniterm is now available as module instead of example.see serial.tools.miniterm for details.
- miniterm.py
- The miniterm program.
- setup-miniterm-py2exe.py
- This is a py2exe setup script for Windows. It can be used to create astandalone
miniterm.exe
.
TCP/IP - serial bridge¶
This program opens a TCP/IP port. When a connection is made to that port (e.g.with telnet) it forwards all data to the serial port and vice versa.
This example only exports a raw socket connection. The next examplebelow gives the client much more control over the remote serial port.
Python Serial Timeout Example Python
- The serial port settings are set on the command line when starting theprogram.
- There is no possibility to change settings from remote.
- All data is passed through as-is.
- tcp_serial_redirect.py
- Main program.
Single-port TCP/IP - serial bridge (RFC 2217)¶
Divinity original sin 2 source. Simple cross platform RFC 2217 serial port server. It uses threads and isportable (runs on POSIX, Windows, etc).
- The port settings and control lines (RTS/DTR) can be changed at any timeusing RFC 2217 requests. The status lines (DSR/CTS/RI/CD) are polled everysecond and notifications are sent to the client.
- Telnet character IAC (0xff) needs to be doubled in data stream. IAC followedby another value is interpreted as Telnet command sequence.
- Telnet negotiation commands are sent when connecting to the server.
- RTS/DTR are activated on client connect and deactivated on disconnect.
- Default port settings are set again when client disconnects.
New in version 2.5.
- rfc2217_server.py
- Main program.
- setup-rfc2217_server-py2exe.py
- This is a py2exe setup script for Windows. It can be used to create astandalone
rfc2217_server.exe
.
Multi-port TCP/IP - serial bridge (RFC 2217)¶
This example implements a TCP/IP to serial port service that works withmultiple ports at once. It uses select, no threads, for the serial ports andthe network sockets and therefore runs on POSIX systems only.
- Full control over the serial port with RFC 2217.
- Check existence of
/tty/USB0..8
. This is done every 5 seconds usingos.path.exists
. - Send zeroconf announcements when port appears or disappears (usespython-avahi and dbus). Service name:
_serial_port._tcp
. - Each serial port becomes available as one TCP/IP server. e.g.
/dev/ttyUSB0
is reachable at<host>:7000
. - Single process for all ports and sockets (not per port).
- The script can be started as daemon.
- Logging to stdout or when run as daemon to syslog.
- Default port settings are set again when client disconnects.
- modem status lines (CTS/DSR/RI/CD) are not polled periodically and the servertherefore does not send NOTIFY_MODEMSTATE on its own. However it responds torequest from the client (i.e. use the
poll_modem
option in the URL whenusing a pySerial client.)
Requirements:
- Python (>= 2.4)
- python-avahi
- python-dbus
- python-serial (>= 2.5)
Installation as daemon:
- Copy the script
port_publisher.py
to/usr/local/bin
. - Copy the script
port_publisher.sh
to/etc/init.d
. - Add links to the runlevels using
update-rc.dport_publisher.shdefaults99
- That’s it :-) the service will be started on next reboot. Alternatively run
invoke-rc.dport_publisher.shstart
as root.
New in version 2.5: new example
- port_publisher.py
- Multi-port TCP/IP-serial converter (RFC 2217) for POSIX environments.
- port_publisher.sh
- Example init.d script.
wxPython examples¶
A simple terminal application for wxPython and a flexible serial portconfiguration dialog are shown here.
- wxTerminal.py
- A simple terminal application. Note that the length of the buffer islimited by wx and it may suddenly stop displaying new input.
- wxTerminal.wxg
- A wxGlade design file for the terminal application.
- wxSerialConfigDialog.py
- A flexible serial port configuration dialog.
- wxSerialConfigDialog.wxg
- The wxGlade design file for the configuration dialog.
- setup-wxTerminal-py2exe.py
- A py2exe setup script to package the terminal application.
Unit tests¶
Python Serial Time Out Example Paragraph
The project uses a number of unit test to verify the functionality. They allneed a loop back connector. The scripts itself contain more information. Alltest scripts are contained in the directory test
.
The unit tests are performed on port loop://
unless a different devicename or URL is given on the command line (sys.argv[1]
). e.g. to run thetest on an attached USB-serial converter hwgrep://USB
could be used orthe actual name such as /dev/ttyUSB0
or COM1
(depending on platform).
Python Serial Timeout Example Software
- run_all_tests.py
- Collect all tests from all
test*
files and run them. By default, theloop://
device is used. - test.py
- Basic tests (binary capabilities, timeout, control lines).
- test_advanced.py
- Test more advanced features (properties).
- test_high_load.py
- Tests involving sending a lot of data.
- test_readline.py
- Tests involving
readline
. - test_iolib.py
- Tests involving the
io
library. Only available for Python 2.6 andnewer. - test_url.py
- Tests involving the URL feature.