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

Darryl Hoar 116 Reputation points
2021-07-28T13:02:48.377+00:00

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.

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,936 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-07-29T02:12:04.447+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.