C# 如何定位特定的串行端口?

C# 如何定位特定的串行端口?,c#,serial-port,firmware,C#,Serial Port,Firmware,以下代码仅返回三个串行端口com3、com4和com5。我想访问的固件位于USB插头乘法器上。如何访问此多路复用器的串行端口,以及如何识别包含要向其发送信息的固件的特定USB using System; using System.IO.Ports; namespace SerialPortExample { class SerialPortExample { public static void Main() { stri

以下代码仅返回三个串行端口com3、com4和com5。我想访问的固件位于USB插头乘法器上。如何访问此多路复用器的串行端口,以及如何识别包含要向其发送信息的固件的特定USB

using System;
using System.IO.Ports;

namespace SerialPortExample
{
    class SerialPortExample
    {
        public static void Main()
        {
            string[] ports = SerialPort.GetPortNames();
            Console.WriteLine("The following serial ports were found:");
            foreach (string port in ports)
            {
                Console.WriteLine(port);
            }
            Console.ReadLine();
        }
    }
}

非常感谢

这是一个相当大的可用性问题,是由USB驱动程序采用快捷方式并模拟串行端口以便于与它们进行接口而引起的。串行端口是非常原始的设备,这使得它们的api非常易于使用。但是,由于缺乏对即插即用的任何支持,因此无法为他们获得适当的通知。驱动程序只是选择一个任意的端口号,由用户决定它可能是哪一个。尝试和错误的东西。这过去不是一个问题,串行端口有一个连接器安装在机器的后面板上,上面清楚地标有COM端口名


您可能会从WMI中获得一些好处,它允许您使用枚举串行端口设备。你得到的是相当不可预测的,它完全依赖于驱动程序来提供数据。最好的实验方法是使用WMI代码创建者实用程序,它还可以自动生成所需的C代码。不幸的是,我再也找不到下载位置了,这似乎在过去几周内被删除了。希望您能找到替代方案。

下面的代码很好地找到了特定端口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Windows.Forms;
namespace MyNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass x = new MyClass();
            var com = x.GetCOMs();
            foreach (string port in com) 
            {
                Console.WriteLine(port);
            }
            Console.ReadLine();
        }

    }

    class MyClass
    {
        public List<string> GetCOMs()
        {
            List<string> coms = new List<string>();
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2",
                "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");

                foreach (ManagementObject obj in searcher.Get())
                {
                    object captionObj = obj["Caption"];
                    if (captionObj != null)
                    {
                        string caption = captionObj.ToString();
                        if (caption.Contains("(COM"))
                        {
                            coms.Add(caption);
                        }
                    }
                }

                m_ParseCOMs(ref coms);
            }
            catch (ManagementException ex)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + ex.Message);
                return coms;
            }

            return coms;
        }

        private void m_ParseCOMs(ref List<string> comPorts)
        {
            string[] temp;
            List<string> temp2 = new List<string>();
            int index = 0;
            foreach (string s in comPorts)
            {
                string temp3 = "";
                temp = s.Split(' ');
                temp3 += temp[temp.Length - 1] + " - ";
                for (int i = 0; i < temp.Length - 1; i++)
                {
                    temp3 += temp[i] + " ";
                }
                temp2.Insert(index, temp3);
                index++;
            }
            comPorts = temp2;
        }
    }
}

这是USB驱动器吗?比如H:或者类似的东西?我想你需要先用软件来创建usb虚拟COM端口。看看这个问题是否对你有帮助——C标签确实是你标签的一部分。你不需要也不应该把它放在你的标题里=@arthurmani System.IO.Ports非常感谢您,您见过这种替代方案吗?模拟可移动驱动器的USB设备与串行端口模拟器非常不同。驱动器确实会生成即插即用事件。谢谢你,我想使用driveinfo.getdrives方法可能是最好的?我已经开始了一场新的斗争,新的解决方案。