Skip to main content

SerialConnection

Struct SerialConnection 

Source
pub struct SerialConnection { /* private fields */ }
Expand description

A single open serial port. Cheap to clone via Arc because all state lives behind a Mutex.

Implementations§

Source§

impl SerialConnection

Source

pub async fn open(config: ConnectionConfig) -> Result<Self>

Open a serial port using the supplied configuration.

Source

pub fn from_io(port: String, io: Box<dyn SerialIo>) -> Self

Build a connection from an arbitrary SerialIo backend. Used by tests to inject an in-memory duplex stream.

Source

pub fn from_io_with_config( config: ConnectionConfig, io: Box<dyn SerialIo>, ) -> Self

Source

pub fn id(&self) -> &str

Source

pub fn port(&self) -> &str

Source

pub fn name(&self) -> Option<&str>

Source

pub fn baud_rate(&self) -> u32

Source

pub fn flow_control(&self) -> FlowControl

Source

pub fn data_bits(&self) -> DataBits

Source

pub fn stop_bits(&self) -> StopBits

Source

pub fn parity(&self) -> Parity

Source

pub fn port_info(&self) -> Option<&PortInfo>

Return the OS-level port identity captured at open time.

Source

pub fn log(&self) -> &Arc<LogBuffer> ⓘ

Return the per-connection event log buffer.

Source

pub fn tx_framing_default(&self) -> Option<TxFramingConfig>

Default TX framing stored on the connection (from profile or open). Returns by value (cloned from interior mutex for live mutation).

Source

pub fn rx_framing_default(&self) -> Option<RxFramingConfig>

Default RX framing stored on the connection.

Source

pub fn rx_parser_default(&self) -> Option<ParserConfig>

Default RX parser stored on the connection.

Source

pub fn protocol_default(&self) -> Option<ProtocolPreset>

Default protocol preset stored on the connection. Copy, so returned by value. Lock internal mutex for live mutation via configure.

Source

pub fn max_buffered_bytes_default(&self) -> usize

Default max buffered bytes for read operations. Mutable live.

Source

pub fn active_profile_binding(&self) -> Option<ActiveProfileBinding>

The active profile-session binding, if this connection was opened through a public open path.

Source

pub fn rx_buffer_size(&self) -> usize

The effective RX ring buffer size configured at open time.

Source

pub fn state(&self) -> ConnectionState

Return the current connection health state.

Source

pub async fn mark_disconnected(&self, error_message: String)

Mark the connection as disconnected due to a fatal I/O error. Takes the io handle out (sets to None), cancels in-flight operations, and clears RX buffers.

Source

pub async fn reconnect(&self) -> Result<()>

Attempt to re-establish the serial port connection.

Rebuilds a SerialStream from the stored config and replaces the current io handle in place. Preserves all counters, id, name, and log buffer. Called by auto-reconnect tasks and the reconnect tool.

Source

pub fn record_tx_bytes(&self, n: usize)

Record n bytes written to the device.

Source

pub fn record_rx_bytes(&self, n: usize)

Record n bytes read from the device.

Source

pub fn last_activity_ms(&self) -> Option<u64>

Return the last I/O activity as milliseconds since Unix epoch.

Source

pub fn record_read_op(&self)

Record one successful read-pipeline operation (read, transact read half, or capture_boot).

Source

pub fn record_write_op(&self)

Record one successful write operation.

Source

pub fn record_truncation(&self)

Record one RX truncation (bytes_returned < bytes_observed).

Source

pub fn status_snapshot(&self) -> ConnectionStatus

Build a snapshot of the current status of this connection.

Source

pub fn cancel_token(&self) -> CancellationToken

Source

pub fn summary(&self) -> ConnectionSummary

Source

pub async fn write(&self, data: &[u8]) -> Result<usize>

Write a byte slice, flushing before returning.

Source

pub async fn read( &self, dst: &mut [u8], timeout_ms: Option<u64>, ) -> Result<usize>

Read up to dst.len() bytes. Returns SerialError::ReadTimeout if timeout_ms is set and elapses before any byte arrives.

When a timeout is given, the lock on the underlying IO is held for at most POLL_MS milliseconds at a time and released between polls. This lets concurrent write calls on the same connection proceed without waiting for the full read timeout — which is essential for the request/response pattern (transact) on CDC-ACM devices.

Source

pub async fn read_latest(&self, max_bytes: usize) -> Result<Vec<u8>>

Read up to max_bytes with a brief timeout (100ms) to capture any immediately available data without blocking long.

Source

pub async fn flush_buffers(&self, target: FlushTarget) -> Result<()>

Discard data buffered in the OS for the input, output, or both directions of this port.

Source

pub async fn set_dtr_rts(&self, dtr: bool, rts: bool) -> Result<()>

Drive the DTR and RTS control lines. Common use case: pulse DTR low to soft-reset an Arduino, or hold both low to enter the ESP32 bootloader.

Takes the per-connection line-control lock so the change cannot interleave with a capture_boot reset pulse.

Source

pub async fn set_flow_control(&self, flow_control: FlowControl) -> Result<()>

Source

pub async fn set_break_state(&self, enabled: bool) -> Result<()>

Set the BREAK condition on the TX line.

Source

pub async fn send_break(&self, duration_ms: u64) -> Result<()>

Assert the BREAK condition on the TX line for duration_ms milliseconds, then release it.

Source

pub async fn reconfigure( &self, baud_rate: Option<u32>, data_bits: Option<DataBits>, stop_bits: Option<StopBits>, parity: Option<Parity>, flow_control: Option<FlowControl>, ) -> Result<ConnectionStatus>

Reconfigure serial parameters on a live connection. Parameters passed as None are left unchanged. Returns the effective config after the operation completes.

Source

pub async fn close(&self) -> Result<()>

Trait Implementations§

Source§

impl Debug for SerialConnection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more