Reconnaissance automatique du registre du port série Windows:
Comment ouvrir le registre Windows:
Entrez "regedit" dans la course pour ouvrir le registre
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>//用于异步读写的事件句柄
}