Swam
Swam

Reputation: 49

Checking if Serial Port is Open

I want to check if a serial port is open, if it is, then I'll close it. I'm not sure how check if the port is or isn't open.

Basically, something like this..

SerialPort port1 = new SerialPort("COM4",9600,Parity.None,8);
port1.Open();

if(/*port is open*/)
{
    Console.WriteLine("Port is open");
    port1.Close();
}

Upvotes: 2

Views: 12785

Answers (1)

farbiondriven
farbiondriven

Reputation: 2468

If you need to check if connection is open use the class attribute IsOpen.

https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.isopen(v=vs.110).aspx

if (port1.IsOpen)
{
    Console.WriteLine("Port is open");
    port1.Close();
}

Upvotes: 3

Related Questions