pub struct SerialInterface { /* private fields */ }
Expand description

Represents a serial interface with various modes and functionalities. It handles serial communication, including reading, writing, and managing port settings. It operates in different modes such as Master, Slave, and Sniff.

Implementations§

source§

impl SerialInterface

source

pub fn new() -> Result<Self, SerialInterfaceError>

Creates a new instance of the SerialInterface with default settings. Returns a SerialInterface object encapsulated in a Result, with an error if initialization fails.

source

pub fn path(self, path: String) -> Self

Sets the path for the serial interface. Returns the modified instance of the SerialInterface for method chaining.

source

pub fn bauds(self, bauds: BaudRate) -> Self

Sets the baud rate for the serial interface. Returns the modified instance of the SerialInterface for method chaining.

source

pub fn char_size(self, size: CharSize) -> Self

Sets the character size for the serial interface. Returns the modified instance of the SerialInterface for method chaining.

source

pub fn parity(self, parity: Parity) -> Self

Sets the parity for the serial interface. Returns the modified instance of the SerialInterface for method chaining.

source

pub fn stop_bits(self, stop_bits: StopBits) -> Self

Sets the parity for the serial interface. Returns the modified instance of the SerialInterface for method chaining.

source

pub fn flow_control(self, flow_control: FlowControl) -> Self

Sets the flow control for the serial interface. Returns the modified instance of the SerialInterface for method chaining.

source

pub fn mode(self, mode: Mode) -> Result<Self, SerialInterfaceError>

Sets the operating mode of the SerialInterface. The mode can be changed only when the current mode is ‘Stop’. Returns a Result with the modified instance or an error if the mode cannot be changed.

source

pub fn modbus_id(self, modbus_id: u8) -> Self

Sets the Modbus ID for the serial interface. Returns the modified instance of the SerialInterface for method chaining.

source

pub fn silence(self, silence: Duration) -> Self

Sets the silence interval for the serial interface. Silence interval used to detect end of modbus frame. Returns the modified instance of the SerialInterface for method chaining.

source

pub fn receiver(self, receiver: Receiver<SerialMessage>) -> Self

Sets the receiver channel for the serial interface. Returns the modified instance of the SerialInterface for method chaining.

source

pub fn sender(self, sender: Sender<SerialMessage>) -> Self

Sets the sender channel for the serial interface. Returns the modified instance of the SerialInterface for method chaining.

source

pub fn set_mode(&mut self, m: Mode) -> Result<(), SerialInterfaceError>

Sets the operating mode of the SerialInterface. Can only be set when the current mode is ‘Stop’. Returns a Result with () or an error if the mode cannot be changed.

source

pub fn get_mode(&self) -> &Mode

Retrieves the current operating mode of the SerialInterface.

source

pub fn get_state(&self) -> &Status

Retrieves the current status of the SerialInterface.

source

pub fn list_ports() -> Result<Vec<String>, SerialInterfaceError>

Lists available serial ports. Returns a Result containing a list of port names or an error if ports cannot be listed.

source

pub fn open(&mut self) -> Result<(), SerialInterfaceError>

Open the serial port.

source

pub fn close(&mut self) -> Result<(), SerialInterfaceError>

Close the serial port.

source

pub async fn listen(&mut self) -> Result<Option<Mode>, SerialInterfaceError>

Sniffing feature: listen on serial line and send a SerialMessage::Receive() via mpsc channel for every serial request received, for every loop iteration, check if a SerialMessage is arrived via mpsc channel. If receive a SerialMessage::Send(), pause listen in order to send message then resume listening. Stop listening if receive SerialMessage::SetMode(Stop). Almost SerialMessage are handled silently by self.read_message().

source

pub async fn write_read( &mut self, data: Vec<u8>, timeout: &Duration ) -> Result<Option<SerialMessage>, SerialInterfaceError>

Master feature: write a request, then wait for response, when response received, stop listening. Returns early if receive SerialMessage::SetMode(Mode::Stop)). Does not accept SerialMessage::Send() as we already waiting for a response. Almost SerialMessage are handled silently by self.read_message().

source

pub async fn wait_for_request( &mut self ) -> Result<Option<SerialMessage>, SerialInterfaceError>

Slave feature: listen the line until request receive, then stop listening. Returns early if receive SerialMessage::SetMode(Mode::Stop) or SerialMessage::Send(). Almost SerialMessage are handled silently by self.read_message().

source

pub async fn start(&mut self)

Main loop

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, 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>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.