pub trait SerialDevice:
AsyncRead
+ AsyncWrite
+ Send
+ Unpin {
// Required methods
fn set_baud_rate(&mut self, baud: u32) -> Result<()>;
fn set_data_bits(&mut self, bits: DataBits) -> Result<()>;
fn set_stop_bits(&mut self, bits: StopBits) -> Result<()>;
fn set_parity(&mut self, parity: Parity) -> Result<()>;
fn set_flow_control(&mut self, flow: FlowControl) -> Result<()>;
fn set_dtr(&mut self, level: bool) -> Result<()>;
fn set_rts(&mut self, level: bool) -> Result<()>;
fn send_break(&mut self, duration: Duration) -> Result<()>;
fn modem_status(&mut self) -> Result<ModemStatus>;
fn config(&self) -> &SerialConfig;
}Expand description
Runtime contract for every serial backend used by rtcom-core.
Implementors supply full-duplex async I/O (via AsyncRead +
AsyncWrite) plus the control-plane operations needed for interactive
sessions: baud / framing changes, DTR/RTS toggling, line-break injection,
and a modem-status snapshot.
§Examples
use rtcom_core::{SerialConfig, SerialDevice, SerialPortDevice};
use tokio::io::AsyncWriteExt;
let mut port = SerialPortDevice::open("/dev/ttyUSB0", SerialConfig::default())?;
port.write_all(b"AT\r\n").await?;Required Methods§
Sourcefn set_baud_rate(&mut self, baud: u32) -> Result<()>
fn set_baud_rate(&mut self, baud: u32) -> Result<()>
Changes the baud rate at runtime.
Successful calls also update the cached SerialConfig returned by
config.
§Errors
Returns an error if the underlying driver rejects the rate (e.g. the hardware cannot produce the requested divisor).
Sourcefn set_data_bits(&mut self, bits: DataBits) -> Result<()>
fn set_data_bits(&mut self, bits: DataBits) -> Result<()>
Sourcefn set_stop_bits(&mut self, bits: StopBits) -> Result<()>
fn set_stop_bits(&mut self, bits: StopBits) -> Result<()>
Sourcefn set_parity(&mut self, parity: Parity) -> Result<()>
fn set_parity(&mut self, parity: Parity) -> Result<()>
Changes the parity mode.
§Errors
Propagates backend errors if the new setting cannot be applied. Some
platforms reject Parity::Mark / Parity::Space.
Sourcefn set_flow_control(&mut self, flow: FlowControl) -> Result<()>
fn set_flow_control(&mut self, flow: FlowControl) -> Result<()>
Changes the flow-control mode.
§Errors
Propagates backend errors if the new setting cannot be applied.
Sourcefn set_dtr(&mut self, level: bool) -> Result<()>
fn set_dtr(&mut self, level: bool) -> Result<()>
Drives the DTR output line to level (true = asserted).
§Errors
Propagates backend errors if the line cannot be toggled.
Sourcefn set_rts(&mut self, level: bool) -> Result<()>
fn set_rts(&mut self, level: bool) -> Result<()>
Drives the RTS output line to level (true = asserted).
§Errors
Propagates backend errors if the line cannot be toggled.
Sourcefn send_break(&mut self, duration: Duration) -> Result<()>
fn send_break(&mut self, duration: Duration) -> Result<()>
Asserts a line break for duration.
The call blocks the current thread for the duration of the break. In
async contexts, schedule it via tokio::task::spawn_blocking if the
duration is long enough to matter.
§Errors
Propagates backend errors if the break cannot be asserted or cleared.
Sourcefn modem_status(&mut self) -> Result<ModemStatus>
fn modem_status(&mut self) -> Result<ModemStatus>
Reads the current input-side modem-status lines.
Takes &mut self because the underlying serialport API does: the
OS read may update internal driver state.
§Errors
Propagates backend errors if the modem status register cannot be read.
Sourcefn config(&self) -> &SerialConfig
fn config(&self) -> &SerialConfig
Returns the most recently applied SerialConfig.
This is always in sync with successful calls to the set_* methods;
it may diverge from the hardware if an external process reconfigures
the port behind our back.