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
impl SerialInterface
Sourcepub fn new() -> Result<Self, SerialInterfaceError>
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.
Sourcepub fn path(self, path: String) -> Self
pub fn path(self, path: String) -> Self
Sets the path for the serial interface. Returns the modified instance of the SerialInterface for method chaining.
Sourcepub fn bauds(self, bauds: BaudRate) -> Self
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.
Sourcepub fn char_size(self, size: CharSize) -> Self
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.
Sourcepub fn parity(self, parity: Parity) -> Self
pub fn parity(self, parity: Parity) -> Self
Sets the parity for the serial interface. Returns the modified instance of the SerialInterface for method chaining.
Sourcepub fn stop_bits(self, stop_bits: StopBits) -> Self
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.
Sourcepub fn flow_control(self, flow_control: FlowControl) -> Self
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.
Sourcepub fn modbus_id(self, modbus_id: u8) -> Self
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.
Sourcepub fn silence(self, silence: Duration) -> Self
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.
Sourcepub fn receiver(self, receiver: Receiver<SerialMessage>) -> Self
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.
Sourcepub fn sender(self, sender: Sender<SerialMessage>) -> Self
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.
Sourcepub fn set_mode(&mut self, m: Mode) -> Result<(), SerialInterfaceError>
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.
Sourcepub fn list_ports() -> Result<Vec<String>, SerialInterfaceError>
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.
Sourcepub fn open(&mut self) -> Result<(), SerialInterfaceError>
pub fn open(&mut self) -> Result<(), SerialInterfaceError>
Open the serial port.
Sourcepub fn close(&mut self) -> Result<(), SerialInterfaceError>
pub fn close(&mut self) -> Result<(), SerialInterfaceError>
Close the serial port.
Sourcepub fn listen(&mut self) -> Result<Option<Mode>, SerialInterfaceError>
pub 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().
Sourcepub fn write_read(
&mut self,
data: Vec<u8>,
timeout: &Duration,
) -> Result<Option<SerialMessage>, SerialInterfaceError>
pub 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().
Sourcepub fn wait_for_request(
&mut self,
) -> Result<Option<SerialMessage>, SerialInterfaceError>
pub 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().