Skip to content

Commit 6573277

Browse files
kawadakksirhcel
authored andcommitted
windows: Exclude nonfunctional ports
Uses `CM_Get_DevNode_Status` to query device status and exclude devices reporting problems, such as being disabled by the user.
1 parent de45af7 commit 6573277

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/windows/enumerate.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,26 @@ impl PortDevice {
261261
}
262262
}
263263

264+
/// Retrieves the problem status of this device. For example, `CM_PROB_DISABLED` indicates
265+
/// the device has been disabled in Device Manager.
266+
fn problem(&mut self) -> Option<ULONG> {
267+
let mut status = 0;
268+
let mut problem_number = 0;
269+
let res = unsafe {
270+
CM_Get_DevNode_Status(
271+
&mut status,
272+
&mut problem_number,
273+
self.devinfo_data.DevInst,
274+
0,
275+
)
276+
};
277+
if res == CR_SUCCESS {
278+
Some(problem_number)
279+
} else {
280+
None
281+
}
282+
}
283+
264284
// Retrieves the port name (i.e. COM6) associated with this device.
265285
pub fn name(&mut self) -> String {
266286
let hkey = unsafe {
@@ -348,6 +368,11 @@ pub fn available_ports() -> Result<Vec<SerialPortInfo>> {
348368
for guid in get_ports_guids()? {
349369
let port_devices = PortDevices::new(&guid);
350370
for mut port_device in port_devices {
371+
// Ignore nonfunctional devices
372+
if port_device.problem() != Some(0) {
373+
continue;
374+
}
375+
351376
let port_name = port_device.name();
352377

353378
debug_assert!(

0 commit comments

Comments
 (0)