diff --git a/Cargo.toml b/Cargo.toml index 58777e1..4defc45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde = "1.0.130" serialport = "4.0.1" +[target.'cfg(windows)'.dependencies] +wmi = "0.9.1" diff --git a/src/main.rs b/src/main.rs index 0cb91a0..03b987d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,19 @@ + +use serde::{Deserialize, Serialize}; use serialport::{available_ports, SerialPortType}; +use wmi::{COMLibrary, WMIConnection, WMIError}; + +#[cfg(windows)] +#[allow(non_camel_case_types)] +#[allow(non_snake_case)] +#[derive(Serialize, Deserialize, Debug)] +struct Win32_POTSModem { + Name: String, + STATUS: String, + AttachedTo: String, +} +#[cfg(any(windows, unix))] fn list_com_ports() { match available_ports() { Ok(ports) => { @@ -45,6 +59,39 @@ fn list_com_ports() { } } } + +#[cfg(windows)] +fn get_modem_ports_and_return_vec_struct() -> Result, WMIError> { + // Creating new COM Port + let com_con = COMLibrary::new()?; + // Create new WMI Connection using COM Port + let wmi_con = WMIConnection::new(com_con.into())?; + + // let modem_ports: Vec = wmi_con.query()?; + + let modem_ports: Vec = match wmi_con.query() { + Ok(modem_ports) => modem_ports, + Err(e) => { + return Err(e); + } + }; + Ok(modem_ports) +} + fn main() { list_com_ports(); + + match get_modem_ports_and_return_vec_struct() { + Ok(modem_ports) => { + for port in &modem_ports { + println!("{:#?}", port); + } + // println!("{}", modem_ports[0].Name); + // println!("{}", modem_ports[0].STATUS); + // println!("{}", modem_ports[0].AttachedTo); + } + Err(e) => { + eprintln!("Error: {}", e); + } + }; }