【问题标题】:List available COM ports列出可用的 COM 端口
【发布时间】:2011-03-26 22:42:56
【问题描述】:

我有一个显示可用 COM 端口的非常小的代码。

我的问题是:

有没有一种简单的方法可以让程序在托盘中运行,并且仅在有新的 COM 端口可用时才弹出,是否可以添加您可以在设备管理器中看到的 COM 端口名称 ec“USB 串行端口”?

我经常添加/移除一个 USB->RS232 转换器,发现它让我很头疼,因为我必须进入设备管理器查看它被分配到哪个 COM 端口。每次都不一样

也许已经有一个小应用可以做到这一点,但我还没有在 Google 上找到它

using System;
using System.Windows.Forms;
using System.IO.Ports;

namespace Available_COMports

{
    public partial class Form1 : Form
    {
        public Form1()
    {
        InitializeComponent();

        //show list of valid com ports
        foreach (string s in SerialPort.GetPortNames())
        {
            listBox1.Items.Add(s);
        }  
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
}

}

【问题讨论】:

    标签: c# windows forms


    【解决方案1】:
     public static void Main()
        {
            // Get a list of serial port names.
            string[] ports = SerialPort.GetPortNames();
    
            Console.WriteLine("The following serial ports were found:");
    
            // Display each port name to the console.
            foreach(string port in ports)
            {
                Console.WriteLine(port);
            }
    
            Console.ReadLine();
        }
    

    【讨论】:

      【解决方案2】:

      看看this question。它使用 WMI 来查找可用的 COM 端口。您可以跟踪存在哪些 COM 端口,并且只通知新的。

      【讨论】:

        【解决方案3】:

        要了解设备何时热插拔,您需要处理 WM_DEVICECHANGE。致电 RegisterDeviceNotification 以启用这些通知的传递。

        【讨论】:

          【解决方案4】:

          获取某个设备的COM号的代码。

          List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
          ManagementObjectSearcher searcher =
              new ManagementObjectSearcher("root\\CIMV2",
              "SELECT * FROM Win32_PnPEntity");
          foreach (ManagementObject queryObj in searcher.Get())
          {
              devices.Add(new USBDeviceInfo(
                  (string)queryObj["DeviceID"],
                  (string)queryObj["PNPDeviceID"],
                  (string)queryObj["Name"]
              ));
          }
          
          foreach (USBDeviceInfo usbDevice in devices)
          {
              if (usbDevice.Description != null)
              {
                  if (usbDevice.Description.Contains("NAME OF Device You are Looking for")) //use your own device's name
                  {
                      int i = usbDevice.Description.IndexOf("COM");
                      char[] arr = usbDevice.Description.ToCharArray();
                      str = "COM" + arr[i + 3];
                      if (arr[i + 4] != ')')
                      {
                          str += arr[i + 4];
                      }
                      break;
                  }
              }
          }
          
          mySerialPort = new SerialPort(str);
          

          【讨论】:

            猜你喜欢
            • 2012-08-18
            • 2013-11-08
            • 1970-01-01
            • 2010-11-15
            • 2012-05-20
            • 1970-01-01
            • 1970-01-01
            • 2011-02-21
            相关资源
            最近更新 更多