SerialPort.GetPortNames with Friendly Names

G

Guest

Hello there,

I'm using the GetPortNames to list the available Ports in a ComboBox. This
is working fine. But I want to show the end user not only boring 'COM1' to
'COMx'. Instead this I want to show the friendly Names like the Device
Manager shows, e.g. 'Prolific USB-to-Serial Comm Port (COM4)'.

So my Question is: How to get these friendly names?

thanks,
Robert
 
D

Dick Grier

Hi,

You can use System.Management. For example (code snip from my book):

ByVal e As System.EventArgs) Handles MyBase.Load

Dim ManObjReturn As ManagementObjectCollection

Dim ManObjSearch As ManagementObjectSearcher

Dim ManObj As ManagementObject

ManObjSearch = New ManagementObjectSearcher _

("Select * from Win32_POTSModem")

ManObjReturn = ManObjSearch.Get

For Each ManObj In ManObjReturn

ListBox1.Items.Add(CStr(ManObj("Name")) & _

" Uses: " & CStr(ManObj("AttachedTo")))

Next


However, realize that you still will have to use the more prosaic names that
GetPortNames returns when you set the PortName property.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
D

Dick Grier

BTW, the example that I posted is for modems. For other serial devices, I
think you will have to call GetPortNames, the perform a search of the
Registry for the associated "friendly name."

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
G

Guest

Hi Dick,

thanks for your help. I searched a little in the MSDN and found the
"Win32_SerialPort" instead of "Win32_POTSModem".
Unfortunately the "Win32_SerialPort" also lists only the Modems, but there
is also the "Win32_PnPEntity" which lists all PnP Devices including serial
ports.
Below is my Code Snippet for this sollution.
Unfortunately ths with this sollution now I get ONLY the friendly name.
Well, every ComPort I've ever seen lists the Portname at the end of the
friendly name in breckets "e.g. ... (COM11)", but I'm not shure, if this is
true for every ComPort in the world.

So, do you know if this is true for every ComPort?

Now my Code Snipper (in C#)

------------------------
ManagementObjectCollection ManObjReturn;
ManagementObjectSearcher ManObjSearch;
ManObjSearch = new ManagementObjectSearcher("Select * from Win32_PnPEntity");
ManObjReturn = ManObjSearch.Get();
foreach (ManagementObject ManObj in ManObjReturn)
{
if (ManObj["ClassGUID"].ToString() ==
"{4D36E978-E325-11CE-BFC1-08002BE10318}")
{
Debug.Print(ManObj["Name"].ToString());
}
}
 
D

Dick Grier

Hi,
Unfortunately ths with this sollution now I get ONLY the friendly name.
Well, every ComPort I've ever seen lists the Portname at the end of the
friendly name in breckets "e.g. ... (COM11)", but I'm not shure, if this is
true for every ComPort in the world.

So, do you know if this is true for every ComPort?
<<

Rules is/are rules, and I suspect that this isn't one. My real answer is,
"I don't know."

However, I suspect that as soon as you rely on it, something will go wrong.
None-the-less, it should be possible to call GetPortNames, and corelate the
results with those from WMI (System.Management). If GetPortNames results in
more serial ports than the System.Management query, then you can use the
"extra" without attempting to be so friendly.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top