C# Console General

C# Get Available Serial Port Names

PortName property of the SerialPort class will list all serial port that is connectted the pc.

Code:

if you want to connect directly any port , you can chance portName variable and get connection.

In following example:

I entered com3 and connected it

 

 

string[] ports = SerialPort.GetPortNames();
foreach (string portName in ports)
{
try
{
Console.WriteLine(portName);//read available port names
var port = new SerialPort(portName, 256000);
port.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.