0

My application is handling communication via serial ports. The problem of mine is that it has some chance that the the communication cable is unplugged from the computer.

I use USB to Serial cables, which means if they unplug from the computer the COM port disappear. I could restart the service (the read application is a windows service) every time when I lose a com port, but detecting newly connected comports is not as fast as it could be with:

SerialPort.GetPortNames()

I thought I will just let the application to continue the read; as as soon as some one plugs back the cable the comport will be visible again for the service. When I do that I keep getting an error like this:

first chance exception of type 'system.invalidoperationexception' occurred in system.dll

I have two different locations where I catch exception when I read without the comport existence:

//sp is the disconnected serial port
sp.DiscardOutBuffer();
sp.Open();

Even with catching the exceptions my application still crashes, and I have no source to determine where the error is.

I guess I will go with stopping and restarting my service, I just thought I will ask your opinion guys as you might have a better solution.

Edit: I would not say it is a duplicate for the marked question, as it still does not tell me why I am getting the exceptions. Or am I wrong?

Daniel
  • 383
  • 1
  • 5
  • 20
  • @HansPassant I read that post before, it is more then 1.5 years old and one of the answers suggest that this problem does not occurs in .Net 4 while I still have it:P As i mentioned it is much more just asking for more opinions as there could have been some changes after the linked post – Daniel Nov 25 '13 at 15:08
  • But I guess I should just go with the answer given in the post you have linked:P... – Daniel Nov 25 '13 at 15:10

0 Answers0