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

Fix serial number reporting on Windows for some devices #131

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 9 additions & 9 deletions src/windows/dcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub(crate) fn get_dcb(handle: HANDLE) -> Result<DCB> {
dcb.DCBlength = std::mem::size_of::<DCB>() as u32;

if unsafe { GetCommState(handle, &mut dcb) } != 0 {
return Ok(dcb);
Ok(dcb)
} else {
return Err(super::error::last_os_error());
Err(super::error::last_os_error())
}
}

Expand Down Expand Up @@ -57,9 +57,9 @@ pub(crate) fn init(dcb: &mut DCB) {

pub(crate) fn set_dcb(handle: HANDLE, mut dcb: DCB) -> Result<()> {
if unsafe { SetCommState(handle, &mut dcb as *mut _) != 0 } {
return Ok(());
Ok(())
} else {
return Err(super::error::last_os_error());
Err(super::error::last_os_error())
}
}

Expand All @@ -78,18 +78,18 @@ pub(crate) fn set_data_bits(dcb: &mut DCB, data_bits: DataBits) {

pub(crate) fn set_parity(dcb: &mut DCB, parity: Parity) {
dcb.Parity = match parity {
Parity::None => NOPARITY as u8,
Parity::Odd => ODDPARITY as u8,
Parity::Even => EVENPARITY as u8,
Parity::None => NOPARITY,
Parity::Odd => ODDPARITY,
Parity::Even => EVENPARITY,
};

dcb.set_fParity(if parity == Parity::None { FALSE } else { TRUE } as DWORD);
}

pub(crate) fn set_stop_bits(dcb: &mut DCB, stop_bits: StopBits) {
dcb.StopBits = match stop_bits {
StopBits::One => ONESTOPBIT as u8,
StopBits::Two => TWOSTOPBITS as u8,
StopBits::One => ONESTOPBIT,
StopBits::Two => TWOSTOPBITS,
};
}

Expand Down
21 changes: 13 additions & 8 deletions src/windows/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn parse_usb_port_info(hardware_id: &str) -> Option<UsbPortInfo> {
r"VID_(?P<vid>[[:xdigit:]]{4})",
r"[&+]PID_(?P<pid>[[:xdigit:]]{4})",
r"(?:[&+]MI_(?P<iid>[[:xdigit:]]{2})){0,1}",
r"([\\+](?P<serial>\w+))?"
r"([\\+](?P<serial>[\w&]+))?"
))
.unwrap();

Expand All @@ -103,7 +103,14 @@ fn parse_usb_port_info(hardware_id: &str) -> Option<UsbPortInfo> {
Some(UsbPortInfo {
vid: u16::from_str_radix(&caps[1], 16).ok()?,
pid: u16::from_str_radix(&caps[2], 16).ok()?,
serial_number: caps.name("serial").map(|m| m.as_str().to_string()),
serial_number: caps.name("serial").map(|m| {
let m = m.as_str();
if m.contains('&') {
m.split('&').nth(1).unwrap().to_string()
} else {
m.to_string()
}
}),
manufacturer: None,
product: None,
#[cfg(feature = "usbportinfo-interface")]
Expand Down Expand Up @@ -273,10 +280,8 @@ impl PortDevice {
)
};

if res == FALSE {
if unsafe { GetLastError() } != ERROR_INSUFFICIENT_BUFFER {
return None;
}
if res == FALSE && unsafe { GetLastError() } != ERROR_INSUFFICIENT_BUFFER {
return None;
}

// Using the unicode version of 'SetupDiGetDeviceRegistryProperty' seems to report the
Expand Down Expand Up @@ -310,7 +315,7 @@ pub fn available_ports() -> Result<Vec<SerialPortInfo>> {
}

ports.push(SerialPortInfo {
port_name: port_name,
port_name,
port_type: port_device.port_type(),
});
}
Expand All @@ -326,7 +331,7 @@ fn test_parsing_usb_port_information() {
assert_eq!(info.vid, 0x1D50);
assert_eq!(info.pid, 0x6018);
// FIXME: The 'serial number' as reported by the HWID likely needs some review
assert_eq!(info.serial_number, Some("6".to_string()));
assert_eq!(info.serial_number, Some("A694CA9".to_string()));
#[cfg(feature = "usbportinfo-interface")]
assert_eq!(info.interface, Some(2));

Expand Down
Loading