diff --git a/serial_port/find_usb_serial_port_win.cpp b/serial_port/find_usb_serial_port_win.cpp index 75996939..f43aa2bb 100644 --- a/serial_port/find_usb_serial_port_win.cpp +++ b/serial_port/find_usb_serial_port_win.cpp @@ -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() */ \ No newline at end of file +} /* find_usb_serial_port() */