24

I have a very small code that shows available COM ports.

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"?

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

}

user302935
  • 407
  • 3
  • 6
  • 11

5 Answers5

28
 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();
    }
Md.Rakibuz Sultan
  • 759
  • 1
  • 8
  • 13
11

Take a look at this question. It uses WMI to find available COM ports. You could keep track of what COM ports exist, and only notify about new ones.

Community
  • 1
  • 1
7

To find out when devices are hot-plugged, you want to handle WM_DEVICECHANGE. Call RegisterDeviceNotification to enable delivery of these notifications.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
3

The code to get the COM number of certain device.

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);
cramopy
  • 3,459
  • 6
  • 28
  • 42
wwjih123
  • 1,076
  • 9
  • 7
0

I'm using .NET 6. I got Md.Rakibuz Sultan's answer to work after adding the nuget package System.IO.Ports.

enter image description here

Program.cs:


using System.IO.Ports;

internal class Program
{
    private static void Main(string[] args)
    {
        // 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();
    }
}
datchung
  • 3,778
  • 1
  • 28
  • 29