views:

2108

answers:

5

I asked this question:
http://stackoverflow.com/questions/434494/serial-port-rs232-in-mono-for-multiple-platforms

and this one is related:
http://stackoverflow.com/questions/304986/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."

+4  A: 

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.

Tanj
+1, some USB serial ports might have the name usbtty..something as well - I'd check it out
Paul Betts
Tanj is correct -- there is no (standard, at least) way to assign a "name" to a serial port device in Linux. The best you can manage is the portion of the device that comes after /dev/ -- if you find an example of what you mean, by all means give it to us.
Coderer
Actually, I disagree with this. HAL actually provides "friendly names" for Linux, but I'm not aware of any Mono API for it.
supercheetah
+2  A: 

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.

Robert P
This is a repeat of the content of the question I linked to in my question above. It is not helpful at all. COM1 is not meaningful enough - as I stated, I want more "friendly" information like what is available on Windows via other methods (see the links in my question)
Tim
Interesting ... I use serial ports all the time with the work I do, and almost no app I have cares about that kind of information - merely the label that is required to access it. Good luck with your search, though!
Robert P
The application doesn't need it - I want it to show to the end user when they select from a dropdown list - picking the COM port they want to use for the application. (it was a requested feature - not my idea)
Tim
If you have the opportunity, I'd definitely check with your customer. My company releases serial based products, and the COM# is usually enough for us and our customers - it's almost always what the serial port attaches to that's important. Of course, I'd still love to learn how this might work. :-)
Robert P
I wrote the software at the request of the hardware manufacturer. his customers end up being my customers. Rather than pick form a list of "COMn" it is nice to see "COMn - Belkin USB rs232" or something like that.
Tim
A: 

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.

Coderer
I can already do this on windows. It is the part about cross platform that is missing.
Tim
+1  A: 

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

A: 

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)

The question was about mono and xplatform - not WMI.
Tim