Skip to content

Lots of serial ports; how to set up a data structure? #212

Answered by sirhcel
Resonanz asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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(())
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Resonanz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants