一般来说,PC机上都有串口。如何获取这些串口的数目以及串口的编号呢?我以前就遇到过这样的问题。当时是编了一个小模块,专门用来查看机器上的串口。首先假设机器上有某个串口(例如COM1),然后尝试着的打开。如果失败,则说明假设是失败的,即这个串口不存在。否则这个串口存在。就这样一个个的试。是不是有点像数学里的反证法?!现在想起来这是多么笨的方法呀!
串口是一种系统资源。由操作系统统一管理。可想而知,系统注册表(windows,其他系统大同小异)里肯定有相应的记录。的确如此。下面就给出根据这个思想写的程序:
DWORD GetAllPorts(CComboBox
*
pComBox)
... {
CString strPath;
strPath = _T("HARDWARE/DEVICEMAP/SERIALCOMM");
HKEY hKey;
// The RegOpenKeyEx function opens the specified registry key.
// Note that the key names is not case sensitive.
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, strPath, 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS)
...{
RegCloseKey(hKey);
return 0;
}
TCHAR achClass[MAX_PATH] = _T(""); // 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.
if(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)!=ERROR_SUCCESS) // last write time
...{
RegCloseKey( hKey );
return 0;
}
CString sList;
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)
...{
sList += (LPTSTR)(achBuff);
pComBox->AddString(LPTSTR(achBuff));
}
}
}
RegCloseKey( hKey );
return cValues;
}
... {
CString strPath;
strPath = _T("HARDWARE/DEVICEMAP/SERIALCOMM");
HKEY hKey;
// The RegOpenKeyEx function opens the specified registry key.
// Note that the key names is not case sensitive.
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, strPath, 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS)
...{
RegCloseKey(hKey);
return 0;
}
TCHAR achClass[MAX_PATH] = _T(""); // 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.
if(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)!=ERROR_SUCCESS) // last write time
...{
RegCloseKey( hKey );
return 0;
}
CString sList;
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)
...{
sList += (LPTSTR)(achBuff);
pComBox->AddString(LPTSTR(achBuff));
}
}
}
RegCloseKey( hKey );
return cValues;
}