以编程方式获取友好的端口名称

发布于 2025-01-07 15:52:06 字数 494 浏览 0 评论 0原文

这里有人知道我可以在哪里获取我的电脑中列出的端口名称吗?

设备管理器提供的端口列表

通过使用此代码:

For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
    cmbPort.Properties.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next

我可以获得 COM26 等。如果有的话,但这不是我想要的。我想要 USB-SERIAL CH340USB-SERIAL CH340 (COM26),而不是检索 COM26。我怎么能这么做呢?

Anyone here has an idea where I can get the ports name listed in my PC?

List of ports provided by Device Manager

By using this code:

For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
    cmbPort.Properties.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next

I could get COM26 and etc. if any, but that's not what I want. Instead of retrieving COM26, I want USB-SERIAL CH340 or USB-SERIAL CH340 (COM26). How could I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

两人的回忆 2025-01-14 15:52:06

我从其他答案中得到了好坏参半的结果。
我想出了这段代码,对我来说效果更好。

在应用程序中添加对 System.Management 的引用

using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'"))
{
    var portnames = SerialPort.GetPortNames();
    var ports = searcher.Get().Cast<ManagementBaseObject>().ToList().Select(p => p["Caption"].ToString());

    var portList = portnames.Select(n => n + " - " + ports.FirstOrDefault(s => s.Contains(n))).ToList();
    foreach (var i in portList)
    {
        Console.WriteLine(i);
    }
}

I got mixed results from other answers.
I've come up with this code working better for me.

Add Reference to System.Management in your application

using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM%'"))
{
    var portnames = SerialPort.GetPortNames();
    var ports = searcher.Get().Cast<ManagementBaseObject>().ToList().Select(p => p["Caption"].ToString());

    var portList = portnames.Select(n => n + " - " + ports.FirstOrDefault(s => s.Contains(n))).ToList();
    foreach (var i in portList)
    {
        Console.WriteLine(i);
    }
}
自控 2025-01-14 15:52:06

试试这个。

Public Shared Function ListFriendlyCOMPOrt() As List(Of String)

    Dim oList As New List(Of String)

    Try
        Using searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM26%'")
            For Each queryObj As ManagementObject In searcher.Get()
                oList.Add(CStr(queryObj("Caption")))
            Next
        End Using

        Return oList

    Catch err As ManagementException
        MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
    End Try

    Return oList

End Function

那应该有效..

Try this .

Public Shared Function ListFriendlyCOMPOrt() As List(Of String)

    Dim oList As New List(Of String)

    Try
        Using searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE Caption like '%(COM26%'")
            For Each queryObj As ManagementObject In searcher.Get()
                oList.Add(CStr(queryObj("Caption")))
            Next
        End Using

        Return oList

    Catch err As ManagementException
        MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
    End Try

    Return oList

End Function

That should work..

醉态萌生 2025-01-14 15:52:06

您可以使用 WMI...
在应用程序中添加对 System.Management 的引用
然后,

StackOverflow 上显示:获取串行端口信息

using System.Management;
using System.IO;

        string result = "";
        using (var searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SerialPort"))
        {
            string[] portnames = SerialPort.GetPortNames();
            var ports = searcher.Get().Cast<ManagementBaseObject>().ToList();
            var tList = (from n in portnames join p in ports on n equals p["DeviceID"].ToString() select n + " - " + p["Caption"]).ToList();

            foreach (string s in tList)
            {
                result = result + s;
            }
        }
        MessageBox.Show(result);

You could use WMI...
Add Reference to System.Management in your application
then,

shown on StackOverflow: Getting Serial Port Information

using System.Management;
using System.IO;

        string result = "";
        using (var searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SerialPort"))
        {
            string[] portnames = SerialPort.GetPortNames();
            var ports = searcher.Get().Cast<ManagementBaseObject>().ToList();
            var tList = (from n in portnames join p in ports on n equals p["DeviceID"].ToString() select n + " - " + p["Caption"]).ToList();

            foreach (string s in tList)
            {
                result = result + s;
            }
        }
        MessageBox.Show(result);
怼怹恏 2025-01-14 15:52:06

这不是串行端口的名称;而是串行端口的名称。它是COM26。设备管理器中列出的名称可能是提供模拟的设备的名称。

你为什么要这个名字?如果你更完整地描述你的问题,找出解决方案就会更容易。

That isn't the name of the serial port; it is COM26. The name listed in the device manager is probably the name of the device providing the emulation.

Why do you want that name? If you describe your problem more completely, figuring out the solution will be easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文