add configurable serial timeout #1

Merged
muellerr merged 1 commits from configurable-serial-timeout into main 2025-11-12 18:08:19 +01:00
3 changed files with 18 additions and 11 deletions
+4 -1
View File
@@ -1,9 +1,12 @@
all: check build test clippy fmt docs coverage
all: check build test clippy check-fmt docs coverage
clippy:
cargo clippy -- -D warnings
fmt:
cargo fmt --all
check-fmt:
cargo fmt --all -- --check
check:
+7 -8
View File
@@ -1,4 +1,6 @@
//! # Serial packet transport with COBS encoding.
use std::time::Duration;
use cobs::CobsDecoderOwned;
use crate::transport::PacketTransport;
@@ -22,19 +24,16 @@ impl PacketTransportSerialCobs {
port_name: &str,
baud_rate: u32,
max_rx_packet_size: usize,
serial_timeout: Duration,
) -> Result<Self, std::io::Error> {
let serial = serialport::new(port_name, baud_rate).open()?;
Ok(PacketTransportSerialCobs {
serial,
decoder: CobsDecoderOwned::new(max_rx_packet_size),
reception_buffer: [0u8; 1024],
log_decoding_errors: true,
})
let mut serial = serialport::new(port_name, baud_rate).open()?;
serial.set_timeout(serial_timeout)?;
Ok(Self::new(serial, CobsDecoderOwned::new(max_rx_packet_size)))
}
/// Generic constructor.
pub fn new(serial: Box<dyn serialport::SerialPort>, decoder: cobs::CobsDecoderOwned) -> Self {
PacketTransportSerialCobs {
Self {
serial,
decoder,
reception_buffer: [0u8; 1024],
+7 -2
View File
@@ -1,5 +1,8 @@
//! # Packet transport via TCP with COBS encoding.
use std::{io::{Read as _, Write as _}, time::Duration};
use std::{
io::{Read as _, Write as _},
time::Duration,
};
use crate::transport::PacketTransport;
@@ -20,7 +23,9 @@ impl PacketTransportTcpWithCobs {
/// The `tcp_stream` parameter is the underlying TCP stream which should already be connected.
pub fn new(tcp_stream: std::net::TcpStream, decoder: cobs::CobsDecoderOwned) -> Self {
tcp_stream.set_nonblocking(true).unwrap();
tcp_stream.set_read_timeout(Some(Duration::from_millis(100))).unwrap();
tcp_stream
.set_read_timeout(Some(Duration::from_millis(100)))
.unwrap();
Self {
tcp_stream,
decoder,