Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
hi, how can i write a code in c# for finding com port ((and automatically change it to com1))
Posted
Updated 28-Aug-12 21:03pm
v2
Comments
OriginalGriff 29-Aug-12 2:58am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

 
Share this answer
 
hiii,


C#
using System;
using System.IO.Ports;

namespace SerialPortExample
{
    class SerialPortExample
    {
        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();
        }

    }
}
 
Share this answer
 
How about that?

C#
System.IO.Ports.SerialPort port = new System.IO.Ports.SerialPort();
port.PortName = "COM1";


And you're done.

But are you sure that at all times COM1 will be the correct port? For everything but the simplest quick-and-dirty tools, I suggest you enable the user to select from the list of available ports.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900