Skip to content

Commit

Permalink
Query modem ports on windows with wmi
Browse files Browse the repository at this point in the history
Signed-off-by: Bin.H <H-H-bin@163.com>
  • Loading branch information
Bin.H authored and Bin.H committed Oct 18, 2021
1 parent 21d5499 commit e2451c1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
47 changes: 47 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -45,6 +59,39 @@ fn list_com_ports() {
}
}
}

#[cfg(windows)]
fn get_modem_ports_and_return_vec_struct() -> Result<Vec<Win32_POTSModem>, 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<Win32_POTSModem> = wmi_con.query()?;

let modem_ports: Vec<Win32_POTSModem> = 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);
}
};
}

0 comments on commit e2451c1

Please sign in to comment.