WMI:获取所有串行(COM)端口(包括虚拟端口)的列表 [英] WMI: Get list of all serial (COM) ports including virtual ports

查看:105
本文介绍了WMI:获取所有串行(COM)端口(包括虚拟端口)的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently working on a little C# program to interact with an Arduino microcontroller. The program has a combobox where you can choose the COM-port. The µc is connected by USB and a virtual COM-port (CH340).

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

I use the code below to fill in the avaible COM-ports to the combobox.

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

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

The downside of this is, you have to take a look into the Device Manager to see which one is the correct one for the µc. My PC for example has 4 active COM-ports, one physical, 2 virtual and another virtual one for the µc. What I'm searching for is a way to display the complete Name of the device with the associated COM-port (like you can find it in the Device Manager)

设备管理器中的COM端口

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

After a bit of research I found out that there is another possibility by using the WMI. After a lot of testing with the "WMI Code Creator" I don't know what else I can try to accomplish what I’ve attended to do. All the namespaces and classes I’ve tried are only generating the COM-port like COM1, COM2… or they generate the hardware-id which is not useful for the user of the program. The code below is more or less exactly what I'm searching for but it only works for "real" build in COM-ports.

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以某种方式识别它们,然后在第二步中为它们获取正确的名称?

Is there any other possible way to get a list of all the COM-ports like there is one insinde of the Device Manager? Is it maybe possible to use the hardware-id of the devices to identify them somehow and then, in a second step get the correct name for them?

如果能得到一些帮助,我将感到非常高兴.一定有办法做到这一点,但我找不到.

I would be very pleased if I could get some help with this. There must be a way to do this but I can't find it.

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

And sry for my english if something sounds wierd :)

推荐答案

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

As mentiont before here is the complete working code to fill a combobox with all avaible COM-ports and set the associated port after seletion.

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

(The original answer how to get the port names --> link)

感谢@o_O提供的链接,希望有人会觉得此代码有用.

Thanks @o_O for the link and I hope someone will find this code useful.

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;
}

这篇关于WMI:获取所有串行(COM)端口(包括虚拟端口)的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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