繁体   English   中英

WMI:获取所有串行(COM)端口(包括虚拟端口)的列表

[英]WMI: Get list of all serial (COM) ports including virtual ports

我目前正在研究一个C#程序,以与Arduino微控制器进行交互。 该程序有一个组合框,您可以在其中选择COM端口。 µc通过USB和虚拟COM端口(CH340)连接。

我使用下面的代码将可用的COM端口填充到组合框。

private void Form1_Load(object sender, EventArgs e)
    {
        string[] PortNames = SerialPort. GetPortNames();
        comboBoxPort.Items.AddRange(PortNames);
    }

不利的一面是,您必须查看“设备管理器”以查看哪一个是适用于µc的正确方法。 例如,我的PC有4个活动的COM端口,一个为物理端口,两个为虚拟端口,另一个为µc提供虚拟端口。 我正在寻找的是一种显示带有关联COM端口的设备完整名称的方法(就像您可以在“设备管理器”中找到它一样)

设备管理器中的COM端口

经过一些研究,我发现使用WMI还有另一种可能性。 在使用“ WMI代码创建器”进行了大量测试之后,我不知道我还能尝试完成我所从事的工作。 我尝试过的所有名称空间和类都只生成COM端口,例如COM1,COM2…或它们生成对程序用户没有用的硬件ID。 下面的代码或多或少完全是我要搜索的代码,但仅适用于COM端口中的“真实”构建。

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
    {
        public class MyWMIQuery
        {
            public static void Main()
            {
                try
                {
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_SerialPort");

                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Win32_SerialPort instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Name: {0}", queryObj["Name"]);
                    }
                }
                catch (ManagementException e)
                {
                    MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
                }
            }
        }
    }

是否还有其他可能的方法来获取所有COM端口的列表,例如设备管理器的一个索引? 是否有可能使用设备的硬件ID以某种方式识别它们,然后在第二步中为它们获取正确的名称?

如果能对此有所帮助,我将感到非常高兴。 一定有办法做到这一点,但我找不到。

如果听起来有些奇怪,请为我的英语感到抱歉:)

如前所述,这里是完整的工作代码,可以用所有可用的COM端口填充组合框,并在选择后设置关联的端口。

(原始答案如何获取端口名称-> 链接

感谢@o_O提供的链接,希望有人会发现此代码有用。

private void Form1_Load(object sender, EventArgs e)
{
    // Get all serial (COM)-ports you can see in the devicemanager
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\cimv2",
        "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\"");

    // Sort the items in the combobox 
    CmdBoxPort.Sorted = true;

    // Add all available (COM)-ports to the combobox
    foreach (ManagementObject queryObj in searcher.Get())
        CmdBoxPort.Items.Add(queryObj["Caption"]);
}

private void CmdBoxPort_SelectedIndexChanged(object sender, EventArgs e)
{
    // Set the right port for the selected item.
    // The portname is based on the "COMx" part of the string (SelectedItem)
    string item = CmdBoxPort.SelectedItem.ToString();

    // Search for the expression "(COM" in the "selectedItem" string
    if (item.Contains("(COM"))
    {
        // Get the index number where "(COM" starts in the string
        int indexOfCom = item.IndexOf("(COM");

        // Set PortName to COMx based on the expression in the "selectedItem" string
        // It automatically gets the correct length of the COMx expression to make sure 
        // that also a COM10, COM11 and so on is working properly.
        serialPort1.PortName = item.Substring(indexOfCom + 1, item.Length - indexOfCom - 2);
    }
    else
        return;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM