-
How do I store dyn SerialPort in a struct such that I can have one struct instance per port connection? Or is there a better way? |
Beta Was this translation helpful? Give feedback.
Answered by
sirhcel
Aug 24, 2024
Replies: 1 comment
-
Do you mean this like in how to store a serial port instance in a struct? This could be done by storing the serial port trait object returned by use serialport::SerialPort;
struct Connection {
port: Box<dyn SerialPort>,
}
pub fn main() -> anyhow::Result<()> {
let port = serialport::new("/dev/ttyX", 115200).open()?;
let mut connection = Connection { port };
connection.port.write_all(b"Hello world!")?;
connection.port.flush()?;
Ok(())
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Resonanz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you mean this like in how to store a serial port instance in a struct? This could be done by storing the serial port trait object returned by
serialport::new()
in a field of this type -Box<dyn SerialPort>
. For example: