Serial package for trio
This project is an adaption of the pyserial project for trio.
Installation
$ pip install trio-serial
Example
from trio import run
from trio_serial import SerialStream
async def main():
async with SerialStream("/dev/ttyUSB0") as ser:
for i in range(10):
buf = await ser.receive_some()
await ser.send_all(buf)
await ser.send_all(buf)
run(main)
API reference
- class trio_serial.abstract.AbstractSerialStream(port, *, exclusive=False, baudrate=115200, bytesize=8, parity=Parity.NONE, stopbits=StopBits.ONE, xonxoff=False, rtscts=False)[source]
Operating system independant public interface of
SerialStream
.Create new SerialStream object.
- Parameters:
port (str) – Name of port. Format depends on implementation. This could be “/dev/ttyUSB0” on Linux or “COM7” on Windows.
exclusive (bool) – Lock port for exclusive use
baudrate (int) – Initial Port speed
bytesize (int) – Number of bits per byte
parity (Parity) – Parity
stopbits (StopBits) – Number of stop bits
xonxoff (bool) – Software Flow Control with XON/XOFF
rtscts (bool) – Hardware Flow Control with RTS/CTS
- abstract async aclose()[source]
Cleanly close the port.
Do nothing if already closed.
- Return type:
None
- abstract async aopen()[source]
Open the port and configure it with the initial state from
__init__()
.- Return type:
None
- abstract async get_cts()[source]
Retrieve current Clear To Send state.
- Returns:
Current CTS state
- Return type:
bool
- abstract async get_hangup()[source]
Retrieve current Hangup on Close state.
- Returns:
Current Hangup on Close state
- Return type:
bool
- abstract async get_rts()[source]
Retrieve current Ready To Send state.
- Returns:
Current RTS state
- Return type:
bool
- property port: str
Get the port name.
- Returns:
port name or device
- async receive_some(max_bytes=None)[source]
Receive some bytes from the serial port.
- Parameters:
max_bytes (int | None) – Maximum number of bytes to receive.
- Returns:
On success, between 1 and
max_bytes
bytes. On End-of-file (e.g. serial port is gone) an empty bytes object is returned.- Return type:
bytes
- async send_all(data)[source]
Send
data
to the serial port. :param data: Data to send- Parameters:
data (ByteString) –
- Return type:
None
- abstract async send_break(duration=0.25)[source]
Transmit a continuous stream of zero-valued bits for a specific duration.
- Params:
duration: Number of seconds
- Parameters:
duration (float) –
- Return type:
None
- abstract async set_hangup(value)[source]
Set Hangup on Close state.
- Parameters:
value (bool) – New Hangup on Close state
- Return type:
None
Contributing and Support
Please create a new Issue or Pull request on GitHub to contribute changes or ask questions.
Development and Testing
To test the package locally, run: .. code-block:: console
$ /path/to/pip install -U .
where pip could be in a virtual environment.
Changelog
0.4.0 - 2023-09-30
Remove loop on
receive_some
method. (#9)Update dependencies.
0.3.0 - 2021-05-15
Fix cleanup issue when running in sync context. Remove
close
method. (#4)
0.2.1 - 2021-04-09
0.1.2 - 2021-02-07
More relaxed dependencies
0.1.1 - 2021-01-31
Add new methods:
0.1.0 - 2020-12-21
Initial release.
License
This documentation is covered under the CC BY-SA 4.0 license.