I have a PCB with an MCU that has a USB peripheral on it connected to a PC and I’m writing a UI to work with it. The USB port is configured in the device as a Comm port, so it looks like “COMx” when connected. I’d like my device to determine if it’s the correct COM port by looking at the iSerialNumber and iProduct strings in the descriptor block. I can already find all the Comm ports out of the USB device list using these two functions:
public string[] FindAllPorts()
{
List<string> ports = new List<string>();
foreach (ManagementObject obj in FindPorts())
{
try
{
if (obj["Caption"].ToString().Contains("(COM"))
{
string ComName = ParseCommName(obj);
if (ComName != null)
{
ports.Add(ComName);
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
return ports.ToArray();
}
static ManagementObject[] FindPorts()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity");
List<ManagementObject> objects = new List<ManagementObject>();
foreach (ManagementObject obj in searcher.Get())
{
objects.Add(obj);
}
return objects.ToArray();
}
catch (Exception e)
{
Console.WriteLine(e);
return new ManagementObject[] { };
}
}
and I can find the Name (or manufacturer, description, etc.) like this:
public List<string> ListPortsByName()
{
List<string> nameList = new List<string>();
foreach (ManagementObject obj in FindPorts())
{
try
{
if (obj["Caption"].ToString().Contains("(COM"))
nameList.Add(obj["Name"].ToString());
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
return nameList;
}
but replacing “Name” with “iProduct” does not give me the product string, so there must be another way to get the string descriptor block and extract from that.
Any help would be appreciated. Thank you.