5

I am working with serial communication and I'm wondering whether I should keep the SerialPort open or not?

I'm creating a queue of commands and only one will be running at a time. Should I create a SerialPort and open/close it in each command, or should I have another class which holds the port open and is called from the commands? Or does it really matter?

Max Schmeling
  • 12,363
  • 14
  • 66
  • 109
  • Does this answer your question? [using a COM port - Close after each use, or leave always open?](https://stackoverflow.com/questions/16777516/using-a-com-port-close-after-each-use-or-leave-always-open) – Zac Apr 19 '21 at 13:26

2 Answers2

8

Keep it open. No point to have the overhead of opening and closing it.

C. Ross
  • 31,137
  • 42
  • 147
  • 238
4

In addition to C. Ross's answer, keeping it open will prevent anything else from opening the SerialPort and blocking you later. I would suggest keeping it open.

Dan Lorenc
  • 5,376
  • 1
  • 23
  • 34