Skip to content

No com port number #257

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

Open
RichardWGNR opened this issue Mar 27, 2025 · 1 comment
Open

No com port number #257

RichardWGNR opened this issue Mar 27, 2025 · 1 comment

Comments

@RichardWGNR
Copy link

RichardWGNR commented Mar 27, 2025

Problem Description

I use serialport::available_ports() to get information about the current ports.
I have the next code:

        let ports = serialport::available_ports().unwrap();
        debug!("{ports:?}");

Which outputs the following to the command line:

18:56:39 [windows] 2025-03-26T13:56:39.768815Z DEBUG ThreadId(12) tmcs_client::flash::resolver::usb_resolver: [
18:56:39 [windows]     SerialPortInfo {                                                                                                                                                                                                                                                                             
18:56:39 [windows]         port_name: "COM1",                                                                                                                                                                                                                                                                       
18:56:39 [windows]         port_type: Unknown,                                                                                                                                                                                                                                                                      
18:56:39 [windows]     },                                                                                                                                                                                                                                                                                           
18:56:39 [windows]     SerialPortInfo {                                                                                                                                                                                                                                                                             
18:56:39 [windows]         port_name: "",  // ATTENTION HERE                                                                                                                                                                                                                                                                         
18:56:39 [windows]         port_type: UsbPort(                                                                                                                                                                                                                                                                      
18:56:39 [windows]             UsbPortInfo {                                                                                                                                                                                                                                                                        
18:56:39 [windows]                 vid: 1155,                                                                                                                                                                                                                                                                       
18:56:39 [windows]                 pid: 22336,                                                                                                                                                                                                                                                                      
18:56:39 [windows]                 serial_number: Some(                                                                                                                                                                                                                                                             
18:56:39 [windows]                     "TMCS_B000000000",                                                                                                                                                                                                                                                           
18:56:39 [windows]                 ),                                                                                                                                                                                                                                                                               
18:56:39 [windows]                 manufacturer: Some(                                                                                                                                                                                                                                                              
18:56:39 [windows]                     "STMicroelectronics.",                                                                                                                                                                                                                                                       
18:56:39 [windows]                 ),                                                                                                                                                                                                                                                                               
18:56:39 [windows]                 product: Some(                                                                                                                                                                                                                                                                   
18:56:39 [windows]                     "TMCarSolutions: Common Bootloader (COM 10)",                                                                                                                                                                                                                                
18:56:39 [windows]                 ),                                                                                                                                                                                                                                                                               
18:56:39 [windows]             },                                                                                                                                                                                                                                                                                   
18:56:39 [windows]         ),                                                                                                                                                                                                                                                                                       
18:56:39 [windows]     },                                                                                                                                                                                                                                                                                           
18:56:39 [windows] ]   

As you and I can see, there is no port name for the second device.

I don't know what the problem is, and I don't know how to reproduce this on another computer.

Other programs easily recognize the COM port name of this device.

Serial Debug Assistant
Image

Windows registry
Image

This may be because I changed the FriendlyName of the device in the Windows registry.
I also tried changing COM 10 to COM10, but that did not work.

My environment

Package Version
Windows 11
Rust 1.85.1
serialport 4.7.0
@RichardWGNR
Copy link
Author

RichardWGNR commented Mar 27, 2025

So far, I've done a workaround using the following function to which I give a string with the FriendlyName of the device.

    fn guess_com_port_from_str<S>(str: S) -> Option<String>
    where
        S: AsRef<str>
    {
        static RGX: LazyLock<Regex> = LazyLock::new(|| {
            Regex::new(r#"\(COM\s*(?<num>\d+)\s*\)"#).unwrap()
        });

        if let Some(caps) = RGX.captures(str.as_ref()) {
            let port_num = caps.name("num").unwrap().as_str();
            return Some(format!("COM{port_num}"))
        }

        None
    }
            list.insert(unique_device_sequence, UsbDevice {
                sw_name: info.product_string().map(|v| v.to_owned()),
                serial_number: sn.to_owned(),
                com_port: if device.port_name.len() > 0 {
                    device.port_name
                } else {
                    let com_port = usb_port.product
                        .as_ref()
                        .and_then(|product| Self::guess_com_port_from_str(product));

                    if let Some(port) = com_port {
                       port
                    } else {
                        continue;
                    }
                },
            });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant