As well as enabling legacy exception handling, you need to subscribe to the Application.ThreadException and CurrentDomain.UnhandledException and then you can see whether the exception was caused by the serial port using something like:
if (!((Exception)e.ExceptionObject).StackTrace.Contains("System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent()"))
and do whatever you see fit. I also find that it helps if you get the SerialPort.BaseStream member as soon as you open the port, you can then close the stream within a try/catch and then close the port within a try/catch.
Partager