Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List available COM ports

Tags:

c#

forms

windows

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

}

like image 229
user302935 Avatar asked Mar 26 '11 22:03

user302935


People also ask

How many COM ports are there?

There are 65,535 possible port numbers, although not all are in common use. Some of the most commonly used ports, along with their associated networking protocol, are: Ports 20 and 21: File Transfer Protocol (FTP).

How do I list all serial ports in Windows?

Open Windows Device Manager. Find "Ports (COM & LPT)" in the list. Expand "Ports (COM & LPT)" to see the names of all serial ports.


1 Answers

 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();
    }
like image 123
Md.Rakibuz Sultan Avatar answered Sep 18 '22 08:09

Md.Rakibuz Sultan