Crates.io | serial2-tokio |
lib.rs | serial2-tokio |
version | 0.1.14 |
source | src |
created_at | 2023-10-06 12:42:15.298756 |
updated_at | 2024-11-10 22:30:14.103123 |
description | cross platform serial ports for tokio based on the serial2 crate |
homepage | |
repository | https://github.com/de-vri-es/serial2-tokio-rs |
max_upload_size | |
id | 995056 |
size | 50,977 |
Serial port communication for tokio
using serial2
.
The serial2-tokio
crate provides a cross-platform interface to serial ports.
It aims to provide a simpler interface than other alternatives.
Currently supported features:
SerialPort
struct for all supported platforms.You can open and configure a serial port in one go with SerialPort::open()
.
The second argument to open()
must be a type that implements IntoSettings
.
In the simplest case, it is enough to pass a u32
for the baud rate.
Doing that will also configure a character size of 8 bits with 1 stop bit and disables parity checks and flow control.
For full control over the applied settings, pass a closure that receives the the current Settings
and return the desired settings.
If you do, you will almost always want to call Settings::set_raw()
before changing any other settings.
The SerialPort
struct implements the standard tokio::io::AsyncRead
and tokio::io::AsyncWrite
traits,
as well as read()
and write()
functions that take &self
instead of &mut self
.
This allows you to use the serial port concurrently from multiple tasks.
The SerialPort::available_ports()
function can be used to get a list of available serial ports on supported platforms.
This example opens a serial port and echoes back everything that is read.
use serial2_tokio::SerialPort;
// On Windows, use something like "COM1" or "COM15".
let port = SerialPort::open("/dev/ttyUSB0", 115200)?;
let mut buffer = [0; 256];
loop {
let read = port.read(&mut buffer).await?;
port.write(&buffer[..read]).await?;
}