In my one project, I need to create an application to upgrade the firmware of the device. Here comport is using for the communication. We cannot fix the COM ID of the USB device in the application because the device could be enumerated with different com id on a different machine as per the availability.
So the solution is that displays the list of all known com ports and users select the correct com port. But it is not a good idea to show all com port, I want that application to identify the com id itself.
Every USB device has a unique hardware id (the combination of VID and PID), so to accomplish the above task my approach is to get COM PORT of USB Serial Device on the basis of VID/PID. The registry of the windows kept the information of the device, so simple logic here to open the windows registry and get the com id.
You can See this Article, Read & Write Windows Registry
How to find the VID and PID numbers
To find the vid (Vendor ID) and PID (Product ID) of a serial USB device first you need to attach the device with your PC. Now you need to follow the below steps to get the vid and PID.
1. Go to Control Panel > Device Manager > Ports.
2. Select the desire com port and double click on it or right-click and select Properties.
3. Go to the Details tab and select Hardware ID to view its PID and VID.
If you want to learn about windows internal, here 10 Free days windows internal course for you.
See the below code to get COM PORT of USB Serial Device (Arduino ) in the Windows machine.
#include "stdafx.h" #include <initguid.h> #include <windows.h> #include <Setupapi.h> //Buffer length #define BUFF_LEN 20 void GetComPort(TCHAR *pszComePort, TCHAR * vid, TCHAR * pid) { HDEVINFO DeviceInfoSet; DWORD DeviceIndex =0; SP_DEVINFO_DATA DeviceInfoData; PCWSTR DevEnum = L"USB"; TCHAR ExpectedDeviceId[80] = {0}; //Store hardware id BYTE szBuffer[1024] = {0}; DEVPROPTYPE ulPropertyType; DWORD dwSize = 0; DWORD Error = 0; //create device hardware id wcscpy_s(ExpectedDeviceId,L"vid_"); wcscat_s(ExpectedDeviceId,vid); wcscat_s(ExpectedDeviceId,L"&pid_"); wcscat_s(ExpectedDeviceId,pid); //SetupDiGetClassDevs returns a handle to a device information set DeviceInfoSet = SetupDiGetClassDevs( NULL, DevEnum, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT); if (DeviceInfoSet == INVALID_HANDLE_VALUE) return; //Fills a block of memory with zeros ZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA)); DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); //Receive information about an enumerated device while (SetupDiEnumDeviceInfo( DeviceInfoSet, DeviceIndex, &DeviceInfoData)) { DeviceIndex++; //Retrieves a specified Plug and Play device property if (SetupDiGetDeviceRegistryProperty (DeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID, &ulPropertyType, (BYTE*)szBuffer, sizeof(szBuffer), // The size, in bytes &dwSize)) { HKEY hDeviceRegistryKey; //Get the key hDeviceRegistryKey = SetupDiOpenDevRegKey(DeviceInfoSet, &DeviceInfoData,DICS_FLAG_GLOBAL, 0,DIREG_DEV, KEY_READ); if (hDeviceRegistryKey == INVALID_HANDLE_VALUE) { Error = GetLastError(); break; //Not able to open registry } else { // Read in the name of the port wchar_t pszPortName[BUFF_LEN]; DWORD dwSize = sizeof(pszPortName); DWORD dwType = 0; if( (RegQueryValueEx(hDeviceRegistryKey,L"PortName", NULL, &dwType, (LPBYTE) pszPortName, &dwSize) == ERROR_SUCCESS) && (dwType == REG_SZ)) { // Check if it really is a com port if( _tcsnicmp( pszPortName, _T("COM"), 3) == 0) { int nPortNr = _ttoi( pszPortName + 3 ); if( nPortNr != 0 ) { _tcscpy_s(pszComePort,BUFF_LEN,pszPortName); } } } // Close the key now that we are finished with it RegCloseKey(hDeviceRegistryKey); } } } if (DeviceInfoSet) { SetupDiDestroyDeviceInfoList(DeviceInfoSet); } } int _tmain(int argc, _TCHAR* argv[]) { //Store com port information TCHAR pszPortName[BUFF_LEN] = {0}; //function to get com id GetComPort(pszPortName,L"2341",L"0042"); //Print available com port wprintf (L"\n\n COM Port ID = %s\n",pszPortName); return 0; }
Recommended Posts for you:
- Best 5 C Books.
- Serial port programming using Win32 API (Windows).
- Reading And Writing Windows Registry Using WinAPI
- Install the port monitor silently without user interaction.
- C++ Interview Questions with Answers.
- C-Sharp Interview Questions.
- Python Interview Questions with Answer.
- Memory Layout in C.
- 100 C interview questions, your interviewer might ask.
- C Interview Questions for the experience.
- 10 questions about dynamic memory allocation
- File handling in C, in few hours.
Reference: MSDN