Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows: Exclude disabled ports from available_ports() #144

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ project adheres to [Semantic Versioning](https://semver.org/).
### Added
### Changed
### Fixed
* Fixes a bug where `available_ports()` returned disabled devices on Windows.
[#144](https://github.com/serialport/serialport-rs/pull/144)

### Removed

## [4.3.0] - 2023-12-11
Expand Down
25 changes: 25 additions & 0 deletions src/windows/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,26 @@ impl PortDevice {
}
}

/// Retrieves the problem status of this device. For example, `CM_PROB_DISABLED` indicates
/// the device has been disabled in Device Manager.
fn problem(&mut self) -> Option<ULONG> {
let mut status = 0;
let mut problem_number = 0;
let res = unsafe {
CM_Get_DevNode_Status(
&mut status,
&mut problem_number,
self.devinfo_data.DevInst,
0,
)
};
if res == CR_SUCCESS {
Some(problem_number)
} else {
None
}
}

// Retrieves the port name (i.e. COM6) associated with this device.
pub fn name(&mut self) -> String {
let hkey = unsafe {
Expand Down Expand Up @@ -348,6 +368,11 @@ pub fn available_ports() -> Result<Vec<SerialPortInfo>> {
for guid in get_ports_guids()? {
let port_devices = PortDevices::new(&guid);
for mut port_device in port_devices {
// Ignore nonfunctional devices
if port_device.problem() != Some(0) {
continue;
}

let port_name = port_device.name();

debug_assert!(
Expand Down
Loading