1 unstable release
0.2.1 | Apr 26, 2025 |
---|---|
0.2.0 |
|
0.1.0 |
|
#1281 in Hardware support
223 downloads per month
34KB
536 lines
Serial Port Arbiter
This is a Linux-only serial port library that offers the following benefits
over directly using /dev/tty
:
- Opens the
/dev/tty
file with flags for non-blocking access. - Sets the
termios
flags to use the TTY in raw mode. - Prevents deadlocks caused by input buffer starvation.
- Prevents data garbling by implementing transaction arbitration.
- Gracefully handles interrupts and timeout errors.
- Gracefully handles connection errors and automatically reconnects.
- Provides a more convenient API than the raw
io::Read
andio::Write
.
This is an "async-less" library, and it is intended to remain that way. If you need asynchronous behavior, you can easily make it async-compatible in your own code.
Example
use std::{
io,
time::{Duration, Instant},
};
use serial_arbiter::*;
fn main() -> io::Result<()> {
let deadline = Instant::now() + Duration::from_secs(3);
// Connect
let port = Arbiter::new();
port.open("/dev/ttyACM0")?;
// Transmit request
port.transmit_str("Hello world\n", deadline)?;
println!("Request sent. Waiting for response...");
// Receive response
let response = port.receive_string(None, Some(deadline))?;
println!("Got response: {response:?}");
Ok(())
}
Go to the examples directory to see how automatic reconnection is working or how to use jsonrpc.
Dependencies
~2.5MB
~48K SLoC