简体   繁体   English

列出可用的COM端口

[英]List available COM ports

I have a very small code that shows available COM ports. 我有一个非常小的代码,显示可用的COM端口。

My question is: 我的问题是:

Is there an easy way to have the program to run in the tray and only popup when a new COM port is available and is it possible to add the name for the COM port that you can see in device manager ec "USB serial port"? 有没有一种简单的方法让程序在托盘中运行,只有在新的COM端口可用时才弹出,是否可以添加COM端口的名称,您可以在设备管理器ec“USB串口”中看到该名称?

I often add/remove a USB->RS232 comverter and find it a pain in the ass because I must go into the device manger to see what COM port it is assigned to. 我经常添加/删除一个USB-> RS232转换器并发现它很痛苦,因为我必须进入设备管理器才能看到它分配给它的COM端口。 It's not the same each time 每次都不一样

Maybe there already is a small app that can do this but I havent found it on Google yet 也许已经有一个小应用程序可以做到这一点,但我还没有在谷歌上找到它

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)
    {
    }
}

} }

 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();
    }

Take a look at this question . 看看这个问题 It uses WMI to find available COM ports. 它使用WMI查找可用的COM端口。 You could keep track of what COM ports exist, and only notify about new ones. 您可以跟踪存在的COM端口,并仅通知新端口。

To find out when devices are hot-plugged, you want to handle WM_DEVICECHANGE . 要找出何时热插拔设备,您需要处理WM_DEVICECHANGE Call RegisterDeviceNotification to enable delivery of these notifications. 调用RegisterDeviceNotification以启用这些通知的传递。

The code to get the COM number of certain device. 获取某些设备的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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM