开发者

open and close com port

I have a small application to open the door using a relay controller. I have to open and close the COM port all time if someone comes in. In the morning, when lots of people come together controller responds very slow and i need to solve this problem fast.

My question is, will i run in to memory issues if i keep it open all time time? will open port re开发者_如何学Gocycle if application crashes and restart ?


I don't think you'll run into memory problems. I wrote a program that uses a bar code scanner that uses a COM port to listen to events from the scanner. I open it when the program launches and keep it open until the program closes with no issues at all.

The only thing to worry about is that the COM port can only be used by one object at a time, so if your program already has it open and tries to open it again, that's a problem.

The program may leave the COM port open, so you're best off to try opening it, and if that fails, try closing it (two separate try...catch blocks) and then try opening it again. It's a hassle, but in my app it turned out to reduce the number of errors I had to troubleshoot. I just open/close/open every time the program loads to be safe.


  • no issue with keeping it open
  • it will close the com port if the app dies

only thing is, sometimes if you don't shut down the com port properly it can keep your app alive as a process in a lingering non dying state which keeps the com port.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜