Fix USB serial port detection in Windows. Fixes false Hue+ detection

This commit is contained in:
Adam Honse 2020-06-28 22:36:25 -05:00
parent dfb2e52e7b
commit d3fc88b24f

View file

@ -33,7 +33,7 @@ std::string find_usb_serial_port(unsigned short vid, unsigned short pid)
const char * DevEnum = "USB";
char ExpectedDeviceId[80] = {0}; //Store hardware id
char vid_pid[10] = {0}; //Store VID/PID
BYTE szBuffer[1024] = {0};
char szBuffer[1024] = {0};
DEVPROPTYPE ulPropertyType;
DWORD dwSize = 0;
@ -41,10 +41,10 @@ std::string find_usb_serial_port(unsigned short vid, unsigned short pid)
| Create device hardware id |
| "vid_ABCD&pid_CDEF" |
\*-----------------------------------------------------------------*/
strcpy(ExpectedDeviceId, "vid_");
strcpy(ExpectedDeviceId, "USB\\VID_");
snprintf(vid_pid, 10, "%04X", vid);
strcat(ExpectedDeviceId, vid_pid);
strcat(ExpectedDeviceId, "&pid_");
strcat(ExpectedDeviceId, "&PID_");
snprintf(vid_pid, 10, "%04X", pid);
strcat(ExpectedDeviceId, vid_pid);
@ -78,34 +78,40 @@ std::string find_usb_serial_port(unsigned short vid, unsigned short pid)
{
HKEY hDeviceRegistryKey;
hDeviceRegistryKey = SetupDiOpenDevRegKey(DeviceInfoSet, &DeviceInfoData,DICS_FLAG_GLOBAL, 0,DIREG_DEV, KEY_READ);
if (hDeviceRegistryKey == INVALID_HANDLE_VALUE)
/*-----------------------------------------------------*\
| Check if the string for this device property matches |
| our expected device string |
\*-----------------------------------------------------*/
if(strncmp(ExpectedDeviceId, szBuffer, 21) == 0)
{
break;
}
else
{
char pszPortName[BUFF_LEN];
DWORD dwSize = sizeof(pszPortName);
DWORD dwType = 0;
/*-----------------------------------------------------*\
| Read in the name of the port |
\*-----------------------------------------------------*/
if( (RegQueryValueEx(hDeviceRegistryKey,"PortName", NULL, &dwType, (LPBYTE) pszPortName, &dwSize) == ERROR_SUCCESS) && (dwType == REG_SZ))
hDeviceRegistryKey = SetupDiOpenDevRegKey(DeviceInfoSet, &DeviceInfoData,DICS_FLAG_GLOBAL, 0,DIREG_DEV, KEY_READ);
if (hDeviceRegistryKey == INVALID_HANDLE_VALUE)
{
if(strncmp(pszPortName, "COM", 3) == 0)
{
ret_str.append(pszPortName);
return ret_str;
}
break;
}
else
{
char pszPortName[BUFF_LEN];
DWORD dwSize = sizeof(pszPortName);
DWORD dwType = 0;
// Close the key now that we are finished with it
RegCloseKey(hDeviceRegistryKey);
/*-----------------------------------------------------*\
| Read in the name of the port |
\*-----------------------------------------------------*/
if( (RegQueryValueEx(hDeviceRegistryKey,"PortName", NULL, &dwType, (LPBYTE) pszPortName, &dwSize) == ERROR_SUCCESS) && (dwType == REG_SZ))
{
if(strncmp(pszPortName, "COM", 3) == 0)
{
ret_str.append(pszPortName);
return ret_str;
}
}
// Close the key now that we are finished with it
RegCloseKey(hDeviceRegistryKey);
}
}
}
}
}
if (DeviceInfoSet)
@ -115,4 +121,4 @@ std::string find_usb_serial_port(unsigned short vid, unsigned short pid)
return ret_str;
} /* find_usb_serial_port() */
} /* find_usb_serial_port() */