Skip to content

Commit

Permalink
Add get_available_port_names (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc authored Feb 28, 2024
1 parent 824ce7e commit 6831401
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

[package]
name = "z-serial"
version = "0.2.2"
version = "0.2.3"
edition = "2021"
repository = "https://github.com/ZettaScaleLabs/z-serial"
homepage = "http://zenoh.io"
Expand All @@ -39,4 +39,4 @@ log = "0.4"
clap = { version = "3.1", features = ["derive"] }
env_logger = "0.9.0"
tokio = {version = "1.17.0", features = ["full"] }
rand = "0.8"
rand = "0.8"
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//

use std::path::Path;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_serial::{ClearBuffer, SerialPort, SerialPortBuilderExt, SerialStream};

Expand Down Expand Up @@ -334,6 +335,25 @@ impl ZSerial {
}
}

pub fn get_available_port_names() -> tokio_serial::Result<Vec<String>> {
let port_names: Vec<String> = tokio_serial::available_ports()?
.iter()
.map(|info| {
Path::new(&info.port_name)
.file_name()
.map(|os_str| os_str.to_string_lossy().to_string())
.ok_or_else(|| {
tokio_serial::Error::new(
tokio_serial::ErrorKind::Unknown,
"Unsupported port name",
)
})
})
.collect::<Result<Vec<String>, _>>()?;

Ok(port_names)
}

#[cfg(test)]
mod tests {
use super::{WireFormat, COBS_BUF_SIZE};
Expand Down

0 comments on commit 6831401

Please sign in to comment.