de-vri-es / serial2-tokio-rs

Other
13 stars 3 forks source link

tokio codec and pins monitring #11

Open anton-dutov opened 1 month ago

anton-dutov commented 1 month ago

In my project i need to monitor cts/dsr pins and should have exchange with device by complex protocol. So i use tokio-codec, but it's takes ownership of port itself and i can't read pin states. is it possible to separate control flow and data flow? To allow me use codec on data flow, and reading pin states on control flow.

Something like that

let port = SerialPort::open(com_port, BAUD_RATE)?;
let (ctl, pipe) = port.slit_flow();

ctl.set_dtr(true)?;

let (mut pipe_tx, mut pipe_rx) = Framed::new(pipe, Codec).split();
de-vri-es commented 1 month ago

Hey!

I think you have two options:

  1. Migrate to tokio_util::codec as recommended by tokio_codec. Then you can use get_ref() or get_mut().

  2. If you can not migrate for some reason, you could use try_clone() before wrapping the serial port. Be sure not to perform reads or writes with the cloned serial port though, since it will interfere with tokio-codec.

:smiley:

anton-dutov commented 1 month ago

Thank you, it looks like what I need.