找了根很久不用的USB转串口线插上再试,没问题了,果然是没检测串口……
送你一段代码:
//通过访问注册表的方式获取当前所有串口号
void CAmoPort::GetAllUartList(CString &UartList)
{
CString str;
HKEY hKey;
str=_T("HARDWARE\\DEVICEMAP\\SERIALCOMM");
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, str,0,KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS)
{
RegCloseKey( hKey );
AfxMessageBox("访问注册表错误!");
return;
}
CHAR achClass[MAX_PATH] = ""; // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
TCHAR achValue[MAX_PATH];
DWORD cchValue = MAX_PATH;
BYTE achBuff[80];
DWORD chValue = 60;
DWORD type = REG_SZ;
// Get the class name and the value count.
RegQueryInfoKey( hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData,// longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
if (cValues)
{
for (DWORD j = 0, retValue = ERROR_SUCCESS;j < cValues; j++)
{
chValue = 60;
cchValue = 60;
retValue = RegEnumValue( hKey,
j,
achValue,
&cchValue,
NULL,
&type, // &dwType,
achBuff, // &bData,
&chValue); // &bcData
if(retValue == ERROR_SUCCESS)
{
UartList += CString(achBuff);//找到一个有效的串口号
m_Port.AddString(CString(achBuff));//添加到串口号ListBox
}
}
}
RegCloseKey( hKey );
return;
} |