1. 打开串口端口
首先通过读取注册表,获取可用的串口端口
int GetComList_Reg(CComboBox * CCombox)
{
HKEY hkey;
int result;
int i = 0;
CString strComName;//串口名称
CString strDrName;//串口详细名称
result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T("Hardware\\DeviceMap\\SerialComm"),
NULL,
KEY_READ,
&hkey);
if (ERROR_SUCCESS == result) // 打开串口注册表
{
TCHAR portName[0x100], commName[0x100];
DWORD dwLong, dwSize;
CCombox->ResetContent();
do
{
dwSize = sizeof(portName) / sizeof(TCHAR);
dwLong = dwSize;
result = RegEnumValue(hkey, i, portName, &dwLong, NULL, NULL, (LPBYTE)commName, &dwSize);
if (ERROR_NO_MORE_ITEMS == result)
{
// 枚举串口
break; // commName就是串口名字"COM2"
}
strComName = commName;
CCombox->AddString(strComName); //只添加串口名即可,不添加详细名
i++;
} while (1);
RegClos