question

DarrylHoar-5879 avatar image
0 Votes"
DarrylHoar-5879 asked TimonYang-MSFT answered

Is it ok to leave a serialport open for long periods of time

C# project in visual studio 2019.
I have created a multi-threaded windows service that continuously monitors and issues commands to a serial port.
I am using the standard serialport component provided in VS 2019.

This service when started, opens the serial port and never closes it. The service can be running for months at a time.

Is this a bad practice ? Should I be closing the serial port and then reopening it periodically ?

Thoughts on this greatly appreciated.

dotnet-csharp
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.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

I don't think it is necessary.

After calling the SerialPort.Close() method, the port will not be closed immediately. It takes a certain amount of time. If you call the Open() method before it is actually closed, it may cause unexpected exceptions.

Moreover, keeping SerialPort open will not cause too much burden, which is worth it in your case.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

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.