SerialPort ComPort = new SerialPort();
string[] ports = SerialPort.GetPortNames();
foreach(string portName in ports)
{
ComPort.PortName = portName;
ComPort.BaudRate = 115200;
ComPort.Parity = Parity.None;
ComPort.StopBits = StopBits.One;
ComPort.ReadTimeout = 500;
ComPort.WriteTimeout = 500;
try
{
ComPort.Open();
...
}
catch
{
ComPort.Close()
}
}