Enumerating COM port
-
Dear All, I am having a USB serial device and I want to create handle to the device whenever the device is connected. I want to open the handle using VID and PID of the USB serial device. I tried using SetupDi calls but not detecting the ports. The below is my code snippet. Please help me to solve this issue.
//GUID is DEFINE_GUID(GUID_DEVINTERFACE_COMPORT, 0x4d36e978, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18);
bool status = false;
bool bRet = false;
HDEVINFO devInfoSet = INVALID_HANDLE_VALUE;
SP_DEVICE_INTERFACE_DATA pDeviceData;
SP_DEVINFO_DATA devInfoData;
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
DWORD index = 0;
ULONG i=0;
ULONG requiredLength=0;
pDeviceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
devInfoSet =SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT,NULL,NULL,(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE) );
if(devInfoSet!=INVALID_HANDLE_VALUE)
{
while(TRUE)
{
bRet = SetupDiEnumDeviceInterfaces(devInfoSet, /* HDEVINFO DeviceInfoSet */NULL, /* PSP_DEVINFO_DATA DeviceInfoData */
&GUID_DEVINTERFACE_COMPORT, /* CONST GUID * InterfaceClassGuid */index,
&pDeviceData /* PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData */);
int x = GetLastError();
if(bRet)
index++;
else
break;// finally the index is zero only
}}