如何找到特定的串口?

时间:2012-08-14 12:49:40

标签: c# serial-port firmware

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

非常感谢提前!

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分: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;
        }
    }
}