当前位置:   article > 正文

纯C++自动识别设备串口

纯C++自动识别设备串口

在这里插入图片描述

//#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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/483986
推荐阅读
相关标签
  

闽ICP备14008679号