diff --git a/CHANGELOG.md b/CHANGELOG.md index 591370d1..9e807c8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ project adheres to [Semantic Versioning](https://semver.org/). * Fixes a bug on Linux without udev where `available_ports()` returned wrong device file paths. [#122](https://github.com/serialport/serialport-rs/pull/122) +* Fixes a bug on Windows where some USB device serial numbers were truncated. + [#131](https://github.com/serialport/serialport-rs/pull/131) ### Removed diff --git a/src/windows/dcb.rs b/src/windows/dcb.rs index 6af7e493..d0207fd6 100644 --- a/src/windows/dcb.rs +++ b/src/windows/dcb.rs @@ -11,9 +11,9 @@ pub(crate) fn get_dcb(handle: HANDLE) -> Result { dcb.DCBlength = std::mem::size_of::() 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()) } } @@ -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()) } } @@ -78,9 +78,9 @@ 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); @@ -88,8 +88,8 @@ pub(crate) fn set_parity(dcb: &mut DCB, parity: Parity) { 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, }; } diff --git a/src/windows/enumerate.rs b/src/windows/enumerate.rs index 06196b16..d5e9e7a1 100644 --- a/src/windows/enumerate.rs +++ b/src/windows/enumerate.rs @@ -94,7 +94,7 @@ fn parse_usb_port_info(hardware_id: &str) -> Option { r"VID_(?P[[:xdigit:]]{4})", r"[&+]PID_(?P[[:xdigit:]]{4})", r"(?:[&+]MI_(?P[[:xdigit:]]{2})){0,1}", - r"([\\+](?P\w+))?" + r"([\\+](?P[\w&]+))?" )) .unwrap(); @@ -103,7 +103,14 @@ fn parse_usb_port_info(hardware_id: &str) -> Option { 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")] @@ -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 @@ -310,7 +315,7 @@ pub fn available_ports() -> Result> { } ports.push(SerialPortInfo { - port_name: port_name, + port_name, port_type: port_device.port_type(), }); } @@ -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));