4

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."

Community
  • 1
  • 1
Tim
  • 20,184
  • 24
  • 117
  • 214
  • So, after some editing by other users, the title of my post was changed to totally alter the question. This is not what I had in mind. Since I have no idea how to use the rollback feature I think I lost some good changes and edits as well. – Tim Jan 28 '09 at 22:20

6 Answers6

3

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,354
  • 1
  • 15
  • 25
  • +1, some USB serial ports might have the name usbtty..something as well - I'd check it out – Ana Betts Jan 28 '09 at 02:06
  • 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 Jan 30 '09 at 20:13
  • 2
    Actually, I disagree with this. HAL actually provides "friendly names" for Linux, but I'm not aware of any Mono API for it. – supercheetah Jun 04 '09 at 07:36
  • This is not an answer I would have accepted. Not sure why the community gets to accept this answer. The assertion taht "linux users will understand the terminology" is nonsense. – Tim Nov 25 '20 at 22:50
2

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
  • 15,707
  • 10
  • 68
  • 112
  • 3
    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 Jan 28 '09 at 22:16
  • 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 Jan 29 '09 at 00:00
  • 1
    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 Jan 29 '09 at 13:38
  • 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 Jan 29 '09 at 16:22
  • 1
    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 Jan 30 '09 at 13:30
0

I built a library for serial port control. It can search the friendly name from registry.

https://github.com/kcwongjoe/serial_port

std::vector<SerialPortInfo> comPorts = SerialPort::getSerialPortList();
std::cout << comPorts[0].friendlyName << std::endl;
JOE
  • 353
  • 2
  • 11
0

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)

-2

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

-3

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
  • 25,844
  • 28
  • 99
  • 154
  • I can already do this on windows. It is the part about cross platform that is missing. – Tim Jan 30 '09 at 13:29