1

以下代码仅返回三个串行端口(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();
        }
    }
}

提前谢谢了!

4

2 回答 2

2

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

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

于 2012-08-14T13:18:17.620 回答
0

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

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;
        }
    }
}
于 2012-08-14T18:15:19.410 回答