串口注册表的自动识别

Windows串口注册表的自动识别:

打开Windows注册表的方法:

在运行输入“regedit”打开注册表


struct UartInfo
{
	DWORD UartNum;
	char UartName[20];
};

//获取串口列表
BOOL EnumComs(struct UartInfo **UartCom, LPDWORD UartComNumber)
{
	//LPCTSTR 即const char * 

	*UartComNumber = 0;
	const char subKey[100] = "HARDWARE\\DEVICEMAP\\SERIALCOMM";
	HKEY hNewKey;
	LONG lResult=RegOpenKeyEx( HKEY_LOCAL_MACHINE, subKey, 0, KEY_ALL_ACCESS, &hNewKey);   
	if(lResult != ERROR_SUCCESS)  
	{   
		PrintLog("打开COM注册表失败!!!");
		return FALSE;   
	}
	else
	{
		PrintLog("打开COM注册表成功!!!");
	}

	//DWORD即unsigned long
	DWORD ValuesNumber;
	DWORD MaxValueNameLen;
	DWORD MaxValueLen;
	CString str;
	//检索指定的子键下有多少个值项
	 lResult = RegQueryInfoKey(
								  hNewKey,
								  NULL,
								  NULL,
								  NULL,
								  NULL,
								  NULL,
								  NULL,
								  &ValuesNumber,
								  &MaxValueNameLen,
								  &MaxValueLen,
								  NULL,
								  NULL
								);
	 if(lResult != ERROR_SUCCESS)
	 {
		 RegCloseKey(hNewKey);
		 PrintLog("检索连接在PC上的串口数量失败!!!");
		 return FALSE;
	 }
	 else
	 {
		 str.Format("连接在PC上的串口数量是:%ld", ValuesNumber);
		 PrintLog(str);
		*UartCom =(struct UartInfo *)malloc( ValuesNumber * sizeof(struct UartInfo)); 
	 }

	 DWORD index;
	 DWORD uartindex = 0;
	 //CHAR  ValueName[MAX_VALUE_NAME]; 
	 CHAR  ValueName[100]; 
	 //DWORD ValueNameSize = MAX_VALUE_NAME;
	 DWORD ValueNameSize;
	 DWORD DataType;
	 BYTE DataBuffer[100] = "";
	 DWORD DataLen = 100;

	 //LPTSTR 即 char *, LPBYTE即 char *
	 //检索每个值项,获取值名,数据类型,数据
	 for(index = 0; index < ValuesNumber; index++)
	 {
		 memset(ValueName, 0, sizeof(ValueName));
		 memset(DataBuffer, 0, sizeof(DataBuffer));
		 ValueNameSize = 100;
		 DataLen = 100;
		 lResult = RegEnumValue(hNewKey,index,ValueName,&ValueNameSize,NULL, &DataType, DataBuffer, &DataLen);
		 if (lResult == ERROR_SUCCESS ) 
		 {
			 switch(DataType)
			 {
				 case REG_NONE:			 // No value type				(0)
					 //PrintLog("DataType: REG_NONE");
					 break;
				 case REG_SZ:			//Unicode nul terminated string (1)
					  //PrintLog("DataType: REG_SZ");
					break;
				 case REG_EXPAND_SZ:	// Unicode nul terminated string (2)
					  //PrintLog("DataType: REG_EXPAND_SZ");
					 break;
				 case REG_BINARY:		// Free form binary				 (3)
					  //PrintLog("DataType: REG_BINARY");
					 break;
				 case REG_DWORD:		// 32-bit number				(4)
					 //PrintLog("DataType: REG_DWORD");
					 break;
				 case REG_MULTI_SZ:		 // Multiple Unicode strings	(7)
					 //PrintLog("DataType: REG_MULTI_SZ");
					 break;
				 default:
					 //PrintLog("Other DataType!!!");
					 break;
			 }
#if 0
			 str.Format("值名:%s", ValueName);
			 PrintLog(str);
			 str.Format("值名长度:%ld", ValueNameSize);
			 PrintLog(str);

			 str.Format("数据:%s", DataBuffer);
			 PrintLog(str);
			 str.Format("数据长度:%ld", DataLen);
			 PrintLog(str);
#endif
			 memcpy((*UartCom)[uartindex].UartName, DataBuffer, DataLen);
			 (*UartCom)[uartindex].UartNum = ValuesNumber;
			 uartindex++;
		 }
		 else if(lResult == ERROR_NO_MORE_ITEMS)
		 {
			 PrintLog("检索串口完毕!!!");
		 }
		 else
		 {
			 DWORD dw = GetLastError();
			 str.Format("检索串口出错: 0x%08x", dw);
			 PrintLog(str);
			 return FALSE;
		 }
	 }

	 *UartComNumber = uartindex;

	 return TRUE;
}


在主函数中调用的代码:

{
<span style="white-space:pre">	</span>DWORD UartComNumber = 0;
	struct UartInfo *pUartCom;
	BOOL bResult;
	bResult = EnumComs(&pUartCom, &UartComNumber);
	DWORD index;

	if(bResult)
	{
		PrintLog("获取串口列表成功");
	}
	else
	{
		PrintLog("获取串口列表失败");
	}

	for( index= 0;index < UartComNumber; index++) 
	{  
		pCDlg->m_ComboBox.AddString(pUartCom[index].UartName);
	}
	if(pUartCom != NULL)
	{
		free(pUartCom);
		pUartCom = NULL;
	}

	ResetEvent(g_UartEvent);
	g_UartEvent = CreateEvent( NULL, TRUE, TRUE, NULL);<span style="white-space:pre">	</span>//用于异步读写的事件句柄
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值