Question

I asked this question:
Serial Port (rs232) in Mono for multiple platforms

and this one is related:
How do I get the friendly name of a COM port in Windows?

But I want to be able to get the "friendly" name on Windows- and possibly also on linux /mac if there is such a thing.

Is there any cross platform way to do it, or am I out of luck?

Here is what I am doing in my current app - and it works great for native C++ win32.

http://www.naughter.com/enumser.html

In any case it does not look like a pretty solution for cross-platform distribution. Does anyone have any suggestions?

EDIT - since people are having trouble understanding what I am asking for: as an example - COM9 is not a friendly name. I want something that says "COM9 - USB connector" or something like that. This is possible with the link above in Win32. It is nasty and hacky, but many times end users have no idea what COM port they need to open in my program unless there is a useful name - more useful than "COMn."

Was it helpful?

Solution

AFAIK there is no "friendly" name for the COMM devices in linux. What I suggest you do is use the /dev/ttyS# as your device name in a linux environment and list them as COMM# in windows.

The linux users will understand the terminology so there shouldn't be a worry there.

OTHER TIPS

Consider looking at the SerialPort.GetPortNames() static method. It's available in .NET 2.0, and it looks like it's implemented in Mono as well. According to http://www.go-mono.com/docs/>the mono docs page, GetPortNames exists on the Mono serial port object, so I'd give it a shot.

If it's implemented, it should return you a C# array of strings containing port names available on your computer. These should make sense for whatever underlying OS you have. For example, in windows it'll return COM1, COM2, COM4, and so on. It should return the string needed for the PortName property.

Update:

Taking a look at a post from the mono-dev mailing list it looks like it does work under *nix environments.

Try the following query in WMI:

"Select Name From Win32_PnPEntity" and search for objects that contain "COM", for example, I have a USB-Serial Converter device installed on my computer:

USB60FPW USB-Serial Converter (COM6)

You need to look into doing WMI. I haven't been able to run this myself, but if you combine this basic framework of how to retrieve a WMI object with this documentation of the Win32_SerialPort class, I think you can work something out.

Basically, you want to get a collection of all the Win32_SerialPorts on the system, then iterate through them. You may want the "Caption", or "Description", or maybe just "Name". My best advice is to just set a breakpoint and check the object's properties in debug mode so you can figure out exactly what gets populated.

Code:

    public static ArrayList GetComFriendlyNames()
    {
        ArrayList names = new ArrayList();
        try
        {
            ManagementObjectSearcher searcher =
              new ManagementObjectSearcher("root\\WMI",
              "SELECT InstanceName, PortName FROM MSSerial_PortName");

            foreach (ManagementObject port in searcher.Get())
            {
                names.Add(port["PortName"] + " - " + port["InstanceName"]);
            }
        }
        catch (ManagementException)
        {
        }
        return names;
    }

usage:

        ArrayList ports = GetComFriendlyNames();
        foreach (string name in ports)
        {
            Console.WriteLine(name);
        }

example output:

COM1 - ACPI\PNP0501\1_0

COM2 - ACPI\PNP0501\2_0

COM3 - FTDIBUSVID_0000+PID_0000+0&0000000&0&0?000_0

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top