port listing

This commit is contained in:
Mateusz Faderewski 2024-07-02 20:53:09 +02:00
parent 999dd244c7
commit 98f37af879
2 changed files with 22 additions and 10 deletions

View File

@ -1,7 +1,7 @@
pub struct DeviceInfo { pub struct DeviceInfo {
pub port: String,
pub description: String, pub description: String,
pub serial: String, pub serial: String,
pub port: String,
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -97,15 +97,23 @@ impl Wrapper {
) )
}; };
let description = unsafe { std::ffi::CStr::from_ptr(description.as_ptr()) }
.to_string_lossy()
.into_owned();
let serial = unsafe { std::ffi::CStr::from_ptr(serial.as_ptr()) }
.to_string_lossy()
.into_owned();
let port = if list.binary_search_by(|d| d.serial.cmp(&serial)).is_ok() {
format!("i:0x{vendor:04X}:0x{product:04X}:{index}")
} else {
format!("s:0x{vendor:04X}:0x{product:04X}:{serial}")
};
if result == 0 { if result == 0 {
list.push(DeviceInfo { list.push(DeviceInfo {
port: format!("i:0x{vendor:04X}:0x{product:04X}:{index}"), description,
description: unsafe { std::ffi::CStr::from_ptr(description.as_ptr()) } serial,
.to_string_lossy() port,
.into_owned(),
serial: unsafe { std::ffi::CStr::from_ptr(serial.as_ptr()) }
.to_string_lossy()
.into_owned(),
}); });
} }
@ -113,6 +121,8 @@ impl Wrapper {
index += 1; index += 1;
} }
list.sort_by(|a, b| a.serial.cmp(&b.serial));
Ok(list) Ok(list)
} else { } else {
match devices { match devices {

View File

@ -1,7 +1,7 @@
pub struct DeviceInfo { pub struct DeviceInfo {
pub port: String,
pub description: String, pub description: String,
pub serial: String, pub serial: String,
pub port: String,
} }
pub struct SerialDevice { pub struct SerialDevice {
@ -43,14 +43,16 @@ impl SerialDevice {
if let serialport::SerialPortType::UsbPort(info) = port.port_type { if let serialport::SerialPortType::UsbPort(info) = port.port_type {
if info.vid == vendor && info.pid == product { if info.vid == vendor && info.pid == product {
devices.push(DeviceInfo { devices.push(DeviceInfo {
port: port.port_name,
description: info.product.unwrap_or_default(), description: info.product.unwrap_or_default(),
serial: info.serial_number.unwrap_or_default(), serial: info.serial_number.unwrap_or_default(),
port: port.port_name,
}) })
} }
} }
} }
devices.sort_by(|a, b| a.serial.cmp(&b.serial));
Ok(devices) Ok(devices)
} }