如何在 Windows 中获取 COM 端口的友好名称? [英] How do I get the friendly name of a COM port in Windows?

查看:40
本文介绍了如何在 Windows 中获取 COM 端口的友好名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 USB 连接的 GSM 调制解调器.调制解调器创建 2 个串行端口.第一个自动连接到调制解调器,第二个在设备管理器中显示为HUAWEI Mobile Connect - 3G PC UI Interface (COM6)"

I have a GSM modem connected via USB. The modem creates 2 serial ports. The first is automatically attached to the modem, the second shows in Device Manager as "HUAWEI Mobile Connect - 3G PC UI Interface (COM6)"

第二个端口用于从调制解调器获取重要信息,例如信号质量;发送和接收短信;以及一系列其他功能.

The second port is used to get vital information from the modem, such as signal quality; to send and receive text messages; and a whole host of other functions.

我正在编写一个应用程序,它将包含第二个端口提供的一些功能.我需要的是一种确定哪个 COM 端口是备用端口的可靠方法.迭代端口并检查对ATE0"的响应是不够的.调制解调器的端口通常是编号较低的端口,当拨号连接未激活时,它将响应ATE0",与第二个端口相同.

I am writing an application that will wrap up some of the features provided by the second port. What I need is a sure fire method of identifying which COM port is the spare one. Iterating the ports and checking a response to "ATE0" is not sufficient. The modem's port is usually the lower numbered one, and when a dial up connection is not active, it will respond to "ATE0" the same as the second port.

我想做的是迭代端口并检查它们的友好名称,如设备管理器中所示.这样我就可以将应用程序中的端口链接到设备管理器中标有HUAWEI Mobile Connect - 3G PC UI Interface (COM6)"的端口.我只是还没有找到任何可以让我以编程方式获取该名称的信息.

What I was thinking of doing is iterating the ports and checking their friendly name, as it shows in Device Manager. That way I can link the port in my application to the port labelled "HUAWEI Mobile Connect - 3G PC UI Interface (COM6)" in Device Manager. I've just not found any information yet that will allow me to get that name programmatically.

推荐答案

Will Dean 发布的信息是最有帮助.这是最终对我有用的代码.PInvoke 类中的所有内容均来自 http://www.pinvoke.net .我确实必须在这里或那里更改数据类型才能使其工作(例如使用枚举而不是 uint 时),但应该很容易弄清楚.

The information posted by Will Dean was most helpful. This is the code that eventually worked for me. Everything in the PInvoke class was taken verbatim from http://www.pinvoke.net . I did have to change a data type here or there to make it work (like when using an enum instead of a uint) but it should be easy to figure out.

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")); 是一个有点笨拙,将不胜感激有关如何清理它的建议.

I think the line Result = tmpstring.Substring(tmpstring.IndexOf("COM"),tmpstring.IndexOf(')') - tmpstring.IndexOf("COM")); is a little clumsy, suggestions on how to clean it up would be appreciated.

感谢您对此事的帮助 Will,没有您,我仍然会在 google 上搜索.

Thanks for your help with this matter Will, without you, I'd still be searching google.

这篇关于如何在 Windows 中获取 COM 端口的友好名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆