question

DarrylHoar-5879 avatar image
0 Votes"
DarrylHoar-5879 asked Viorel-1 commented

Why does SerialPort.IsOpen always return False?

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.

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Note that according to the sources of SerialPort, Write calls IsOpen and throws an exception if it is false.

Did you show the fragments of real code?

0 Votes 0 ·

0 Answers