0

I have a problem in order to detect the actual ComPort of a bluetooth device after a manual pairing. First of all I have to mention that in dev envinroment I use Windows 8.1. I searched on the internet solutions for this problem and I found a C# code that is related to this command in PowerShell :

 Get-WmiObject -query "select DeviceID,PNPDeviceID from Win32_SerialPort"

but, I have 3 connections on my PC as you can see in the picture below : enter image description here

and when I run the command, there are just two of them, and my device "MiCon_*" is not available :

enter image description here

Its a Windows 8 problem ? Or I am doing something wrong ?

If I check the PnP_Entity, it gives me only the name like "Bluetooth Serial Port (Com4)" :

enter image description here

How can I detect COMPort using friendly name ?

Dragos Stoica
  • 1,753
  • 1
  • 21
  • 42

1 Answers1

0

HKLM\HARDWARE\DEVICEMAP\SERIALCOMM contains a list of all active com devices.

Since you labeled this question with c#, here is a snippet that'll list them.

List<string> keys = null;
string rootKeyName = null;
using (RegistryKey RootKey = Registry.LocalMachine.OpenSubKey("HARDWARE\\DEVICEMAP\\SERIALCOMM", false))
{
    if (RootKey != null)
    {
        keys = new List<string>(RootKey.GetValueNames());
        rootKeyName = RootKey.Name;
    }
}
if (keys != null && !string.IsNullOrEmpty(rootKeyName))
{
    foreach (string drv in keys)
    {
        string port = Registry.GetValue(rootKeyName, drv, string.Empty) as string;
        if( port != null)
        {
            Console.WriteLine("Port: {0}, Device: {1}", port, drv);
        }
    }
}

yields output like:

Port: CNCA0, Device: \Device\com0com10
Port: CNCB0, Device: \Device\com0com20
Port: COM34, Device: \Device\VCP0

Device names can be converted like this (wmic sample, not c# code, enter it in a command prompt)

wmic path Win32_PnPSignedDriver where "pdo like '%com0com%'" get devicename,pdo

Output:

DeviceName                      PDO
com0com - serial port emulator  \Device\com0com20
com0com - serial port emulator  \Device\com0com10

wmi query from: https://superuser.com/a/790650

Community
  • 1
  • 1
Mark Jansen
  • 1,491
  • 12
  • 24
  • This is almost like I need, but can I have a connection between \Device\Serial3 and the friendly name of the device? in this case, for COM4 the name returned is "\Device\Serial3", but I need to match it with the friendly name which is "MinCon_*". Thanks! – Dragos Stoica Jul 24 '15 at 10:48
  • [here](http://stackoverflow.com/questions/2937585/how-to-open-a-serial-port-by-friendly-name) they suggest either WMI or SetupAPI. – Mark Jansen Jul 24 '15 at 10:54
  • Oh, and the reason why i did not suggest to use SerialPort.GetPortNames, is because that function (used to have) a bug in its implementation, resulting in sometimes invalid or nonexisting ports being returned. Given, that was a few years back so they might have fixed it, but just in case i prefer to refrain from using it. – Mark Jansen Jul 24 '15 at 10:56
  • I tried this code before, but it returns "Bluetooth Serial Port (COM4)" and I need "Micon_*" – Dragos Stoica Jul 24 '15 at 10:57