如何在 Windows 中获取 COM 端口的友好名称?
-
08-07-2019 - |
题
我有一个通过 USB 连接的 GSM 调制解调器。调制解调器创建 2 个串行端口。第一个自动连接到调制解调器,第二个在设备管理器中显示为“HUAWEI Mobile Connect - 3G PC UI Interface (COM6)”
第二个端口用于从调制解调器获取重要信息,例如信号质量;发送和接收短信;以及一系列其他功能。
我正在编写一个应用程序,它将包含第二个端口提供的一些功能。我需要一种可靠的方法来识别哪个 COM 端口是备用端口。迭代端口并检查对“ATE0”的响应是不够的。调制解调器的端口通常是编号较低的端口,当拨号连接未激活时,它将与第二个端口相同地响应“ATE0”。
我想做的是迭代端口并检查它们的友好名称,如设备管理器中所示。这样我就可以将应用程序中的端口链接到设备管理器中标有“HUAWEI Mobile Connect - 3G PC UI Interface (COM6)”的端口。我只是还没有找到任何信息可以让我以编程方式获取该名称。
解决方案
很久以前我为客户写了一个实用程序来做这个,但对于GPS而不是调制解调器。
我刚看过它,跳出来可能有用的是:
GUID guid = GUID_DEVCLASS_PORTS;
SP_DEVICE_INTERFACE_DATA interfaceData;
ZeroMemory(&interfaceData, sizeof(interfaceData));
interfaceData.cbSize = sizeof(interfaceData);
SP_DEVINFO_DATA devInfoData;
ZeroMemory(&devInfoData, sizeof(devInfoData));
devInfoData.cbSize = sizeof(devInfoData);
if(SetupDiEnumDeviceInfo(
hDeviceInfo, // Our device tree
nDevice, // The member to look for
&devInfoData
))
{
DWORD regDataType;
BYTE hardwareId[300];
if(SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_HARDWAREID, ®DataType, hardwareId, sizeof(hardwareId), NULL))
{
...
(在循环中调用此位并增加nDevice)
然后
BYTE friendlyName[300];
if(SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_FRIENDLYNAME, NULL, friendlyName, sizeof(friendlyName), NULL))
{
strFriendlyNames += (LPCTSTR)friendlyName;
strFriendlyNames += '\n';
}
找到设备的名称。
希望这能帮助你朝着正确的方向前进。
其他提示
在确定串行端口设备是您想要的设备后(通过查看其友好名称、检查其父设备等),获取端口名称的正确方法可能是:
- 调用
SetupDiOpenDevRegKey(hDevInfo, devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ)
得到HKEY
所谓的设备密钥 - 查询此注册表项
REG_SZ
值“端口名称” - 不要忘记关闭
HKEY
:)
然而,这可能需要 C# 中的大量互操作,这一点都不好笑,所以如果您坚持使用字符串解析解决方案,我不会责怪您。
Will Dean 发布的信息最有帮助。这是最终为我工作的代码。 PInvoke课程中的所有内容都是从 http://www.pinvoke.net 中逐字记录下来的。我确实需要在这里或那里更改数据类型以使其工作(比如使用枚举而不是uint)但是应该很容易理解。
internal static string GetComPortByDescription(string Description)
{
string Result = string.Empty;
Guid guid = PInvoke.GUID_DEVCLASS_PORTS;
uint nDevice = 0;
uint nBytes = 300;
byte[] retval = new byte[nBytes];
uint RequiredSize = 0;
uint PropertyRegDataType = 0;
PInvoke.SP_DEVINFO_DATA devInfoData = new PInvoke.SP_DEVINFO_DATA();
devInfoData.cbSize = Marshal.SizeOf(typeof(PInvoke.SP_DEVINFO_DATA));
IntPtr hDeviceInfo = PInvoke.SetupDiGetClassDevs(
ref guid,
null,
IntPtr.Zero,
PInvoke.DIGCF.DIGCF_PRESENT);
while (PInvoke.SetupDiEnumDeviceInfo(hDeviceInfo, nDevice++, ref devInfoData))
{
if (PInvoke.SetupDiGetDeviceRegistryProperty(
hDeviceInfo,
ref devInfoData,
PInvoke.SPDRP.SPDRP_FRIENDLYNAME,
out PropertyRegDataType,
retval,
nBytes,
out RequiredSize))
{
if (System.Text.Encoding.Unicode.GetString(retval).Substring(0, Description.Length).ToLower() ==
Description.ToLower())
{
string tmpstring = System.Text.Encoding.Unicode.GetString(retval);
Result = tmpstring.Substring(tmpstring.IndexOf("COM"),tmpstring.IndexOf(')') - tmpstring.IndexOf("COM"));
} // if retval == description
} // if (PInvoke.SetupDiGetDeviceRegistryProperty( ... SPDRP_FRIENDLYNAME ...
} // while (PInvoke.SetupDiEnumDeviceInfo(hDeviceInfo, nDevice++, ref devInfoData))
PInvoke.SetupDiDestroyDeviceInfoList(hDeviceInfo);
return Result;
}
我认为Result = tmpstring.Substring(tmpstring.IndexOf("COM"),tmpstring.IndexOf(')') - tmpstring.IndexOf("COM"));
行有点笨拙,有关如何清理它的建议将不胜感激。
感谢您对此事的帮助。如果没有您,我仍然会搜索谷歌。
基于@Will Dean回答的C ++版本。
#include <windows.h>
#include <initguid.h>
#include <devguid.h>
#include <setupapi.h>
void enumerateSerialPortsFriendlyNames()
{
SP_DEVINFO_DATA devInfoData = {};
devInfoData.cbSize = sizeof(devInfoData);
// get the tree containing the info for the ports
HDEVINFO hDeviceInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS,
0,
nullptr,
DIGCF_PRESENT
);
if (hDeviceInfo == INVALID_HANDLE_VALUE)
{
return;
}
// iterate over all the devices in the tree
int nDevice = 0;
while (SetupDiEnumDeviceInfo(hDeviceInfo, // Our device tree
nDevice++, // The member to look for
&devInfoData))
{
DWORD regDataType;
DWORD reqSize = 0;
// find the size required to hold the device info
SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_HARDWAREID, nullptr, nullptr, 0, &reqSize);
BYTE* hardwareId = new BYTE[(reqSize > 1) ? reqSize : 1];
// now store it in a buffer
if (SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_HARDWAREID, ®DataType, hardwareId, sizeof(hardwareId) * reqSize, nullptr))
{
// find the size required to hold the friendly name
reqSize = 0;
SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_FRIENDLYNAME, nullptr, nullptr, 0, &reqSize);
BYTE* friendlyName = new BYTE[(reqSize > 1) ? reqSize : 1];
// now store it in a buffer
if (!SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_FRIENDLYNAME, nullptr, friendlyName, sizeof(friendlyName) * reqSize, nullptr))
{
// device does not have this property set
memset(friendlyName, 0, reqSize > 1 ? reqSize : 1);
}
// use friendlyName here
delete[] friendlyName;
}
delete[] hardwareId;
}
}
很高兴它有效。
你可以尝试:
Regex.Match(tmpstring,@ <!>; COM \ s \ d + <!> quot;)。ToString()
用于字符串匹配。
作为.NET样式点,我使用System.Text <!>添加了一个<!>;并且我不会用大写字母启动局部变量名称,如果我感觉真的很有道理,我可能会将SetupDiDestroyDeviceInfoList放在finally {}子句中。