赞
踩
//#include <stdio.h>
//#include <stdlib.h>
//#include <vector>
//#include <locale.h>
#include <string>
#include <Windows.h>
#include <setupapi.h>
#pragma comment(lib, "setupapi.lib")
using namespace std;
//windows /dev/ttyGS1 ttyGS9----------------------------big MI
//#define MY_01 ("VID_05C6&PID_9091&MI_06")
//#define MY_02 ("VID_05C6&PID_901D&MI_04")
//#define MY_03 ("VID_05C6&PID_90DF&MI_03")
//#define MY_04 ("VID_05C6&PID_90B5&MI_06")
//#define MY_05 ("VID_05C6&PID_90B6&MI_07")
//#define MY_06 ("VID_05C6&PID_90B8&MI_07")
//#define MY_07 ("VID_05C6&PID_F00E&MI_04")
//#define MY_08 ("VID_05C6&PID_9024&MI_05")
//#define MY_09 ("VID_18D1&PID_4EE8&MI_06")
//#define MY_10 ("VID_18D1&PID_4001&MI_02")
//#define MY_11 ("VID_1782&PID_5D31&MI_08")
//#define MY_12 ("VID_1782&PID_5D20&MI_04")
//windows /dev/ttyGS0 ttyGS8----------------------------small MI
#define MY_01 ("VID_05C6&PID_9091&MI_04")
#define MY_02 ("VID_05C6&PID_901D&MI_02")
#define MY_03 ("VID_05C6&PID_90DF&MI_01")
#define MY_04 ("VID_05C6&PID_90B5&MI_04")
#define MY_05 ("VID_05C6&PID_90B6&MI_05")
#define MY_06 ("VID_05C6&PID_90B8&MI_05")
#define MY_07 ("VID_05C6&PID_F00E&MI_02")
#define MY_08 ("VID_05C6&PID_9024&MI_03")
#define MY_09 ("VID_18D1&PID_4EE8&MI_04")
#define MY_10 ("VID_18D1&PID_4001&MI_00")
#define MY_11 ("VID_1782&PID_5D31&MI_06")
#define MY_12 ("VID_1782&PID_5D20&MI_02")
#define MY_USB_PID_VID_F200 ("VID_05C6&PID_9018") //old device
#define MY_USB_PID_VID_F210 ("VID_18D1&PID_4001")
int extractNumberFromParentheses(const char* input) {
const char* start = strchr(input, '(');
const char* end = strchr(input, ')');
if (start != NULL && end != NULL) {
char number[10];
strncpy(number, start + 1, end - start - 1);
number[end - start - 1] = '\0';
return atoi(number+3); //skip COM
}
return -1; // Return -1 if parentheses not found or number extraction fails
}
int getSpecialComNumFromDevInfo()
{
//printf("Locale is: %s\n", setlocale(LC_ALL, "chs"));
HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("SetupDiGetClassDevs Err:%d", GetLastError());
return -1;
};
SP_CLASSIMAGELIST_DATA _spImageData = { 0 };
_spImageData.cbSize = sizeof(SP_CLASSIMAGELIST_DATA);
SetupDiGetClassImageList(&_spImageData);
SP_DEVINFO_DATA spDevInfoData = { 0 };
spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)
{
DWORD nSize = 0;
CHAR szDis[MAX_PATH] = { 0x00 };
if (!SetupDiGetDeviceInstanceIdA(hDevInfo, &spDevInfoData, szDis, sizeof(szDis), &nSize)) { break; }
printf("PID_VID:%s\r\n", szDis);
if (strstr(szDis, MY_USB_PID_VID_F200) == NULL
&& strstr(szDis, MY_USB_PID_VID_F210) == NULL
&& strstr(szDis, MY_01) ==NULL
&& strstr(szDis, MY_02) ==NULL
&& strstr(szDis, MY_03) ==NULL
&& strstr(szDis, MY_04) ==NULL
&& strstr(szDis, MY_05) ==NULL
&& strstr(szDis, MY_06) ==NULL
&& strstr(szDis, MY_07) ==NULL
&& strstr(szDis, MY_08) ==NULL
&& strstr(szDis, MY_09) ==NULL
&& strstr(szDis, MY_10) ==NULL
&& strstr(szDis, MY_11) ==NULL
&& strstr(szDis, MY_12) ==NULL)
{
continue;
}
printf("PID_VID mathched.\r\n");
char szName[MAX_PATH] = { 0 };
if (SetupDiGetDeviceRegistryPropertyA(hDevInfo, &spDevInfoData, SPDRP_FRIENDLYNAME, NULL, (PBYTE)szName, MAX_PATH - 1, 0))
{
printf("FriendlyName:%s\r\n\r\n", szName);
return extractNumberFromParentheses(szName);
}
}
SetupDiDestroyClassImageList(&_spImageData);
return -1;
}
void main()
{
printf("COM%d\r\n",getSpecialComNumFromDevInfo());
getchar();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。