Why does SerialPort.IsOpen always return False?

Darryl Hoar 116 Reputation points
2021-09-10T14:34:07.127+00:00

Greetings,
I am using VS 2019 c# for a custom windows service. This service uses the serialport to communicate with a custom piece of hardware.

I have observed that in my code, the SerialPort.IsOpen method always returns False. Even if the line prior is an open statement.

ie,
The SerialPort is defined in the graphical designed (ie UI). Its properties are set there. its name is serialPort1

In a function named PollStation,
I have a debug statement

if (!(serialPort1.IsOpen))
appLog.WriteEntry("Serial Port is not open. Count: " + Convert.ToString(counter));

set breakpoint on appLog statement. IsOpen is always returning false. As a test, I added a serialport open statement right before the IsOpen test. the serialPort1.Open() throws a serial port already open exception.
I know its alway False as I have a counter value which is incremented in my for loop.

If I modify the code to :
if (!(serialPort1.IsOpen))
serialPort1.Write(txblock,0,length);
it will write the values to the serial port. How it does this when serialPort1.IsOpen returns false, I have no ideas.

any ideas ?
I know this is not super clear, but I would have to provide all the code to make it clear and I can't do that.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes