pub struct Channel<P, T: ?Sized> { /* private fields */ }
Expand description
Channel encode and decode message with protocol, and send and receive bytes via stream
Examples
ⓘ
let mut channel = Channel::new(
RplidarProtocol::new(),
serial_port
);
channel.write(&Message::new(1)).unwrap();
Implementations§
source§impl<P, T> Channel<P, T>where
P: ProtocolDecoder + ProtocolEncoder,
T: Read + Write + ?Sized,
impl<P, T> Channel<P, T>where
P: ProtocolDecoder + ProtocolEncoder,
T: Read + Write + ?Sized,
sourcepub fn new(protocol: P, stream: Box<T>) -> Channel<P, T>
pub fn new(protocol: P, stream: Box<T>) -> Channel<P, T>
Create a new Channel
to read and write messages
Example
ⓘ
let channel = Channel::new(
RplidarProtocol::new(),
serial_port
);
sourcepub fn with_read_buffer_size(
protocol: P,
stream: Box<T>,
read_buffer_size: usize
) -> Channel<P, T>
pub fn with_read_buffer_size(
protocol: P,
stream: Box<T>,
read_buffer_size: usize
) -> Channel<P, T>
Create a new Channel
with non-default ring buffer capacity
Example
ⓘ
let channel = Channel::with_read_buffer_size(
RplidarProtocol::new(),
serial_port,
100000 as usize
);
sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the channel status This function is usually used to reset protocol encoder and decoder when meet communication error
Example
ⓘ
match channel.invoke(&Message::new(1), Duration::from_secs(1)) {
Ok(_) => {},
Err(_) => { channel.reset(); }
}
sourcepub fn read(&mut self) -> Result<Option<Message>>
pub fn read(&mut self) -> Result<Option<Message>>
Read message from channel
Example
ⓘ
if let Some(msg) = channel.read().unwrap() {
println!("{:?}", msg);
}