use crate ::transport::{to_timeout, Transport};
use std::io;
use std::path::Path;
use std::time::Instant;
pub const DEFAULT_BAUD_RATE: u32 = 57600 ;
pub struct UartTransport {
port: Box <dyn serialport::SerialPort>,
}
impl Transport for UartTransport {
fn read(&mut self , buf: &mut [u8], deadline: Instant) -> io::Result<()> {
self .read_until(buf, deadline)
}
fn write(&mut self , buf: &[u8], deadline: Instant) -> io::Result<()> {
self .write_until(buf, deadline)
}
}
impl UartTransport {
pub fn new(path: impl AsRef<Path>, baud_rate: Option<u32>) -> io::Result<Self > {
let path = path.as_ref().to_str().ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidInput, "Path is not valid unicode" )
})?;
let baud_rate = baud_rate.unwrap_or(DEFAULT_BAUD_RATE);
let port = serialport::new(path, baud_rate)
.preserve_dtr_on_open() // TIOCMBIS -> ENOTTY on pts
.open()?;
Ok(Self { port })
}
fn read_until(&mut self , buf: &mut [u8], deadline: Instant) -> io::Result<()> {
let mut cursor = buf;
while !cursor.is_empty() {
self .set_deadline(deadline)?;
let n = self .port.read(cursor)?;
if n == 0 {
return Err(io::Error::from(io::ErrorKind::UnexpectedEof));
}
assert!(n <= cursor.len());
cursor = &mut cursor[n..];
}
Ok(())
}
fn write_until(&mut self , buf: &[u8], deadline: Instant) -> io::Result<()> {
let mut cursor = buf;
while !cursor.is_empty() {
self .set_deadline(deadline)?;
let n = self .port.write(cursor)?;
if n == 0 {
return Err(io::Error::from(io::ErrorKind::UnexpectedEof));
}
assert!(n <= cursor.len());
cursor = &cursor[n..];
}
Ok(())
}
fn set_deadline(&mut self , deadline: Instant) -> io::Result<()> {
Ok(self .port.set_timeout(to_timeout(deadline)?)?)
}
}
Messung V0.5 in Prozent C=94 H=94 G=93
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-28)
¤
*© Formatika GbR, Deutschland