I'm trying to prevent against of any strange behaviour of users who use serial connection in my application.
My application connects to device over USB->serial converter. After I'm sure that port is available, connected and ready I'm seting enviroment to send data, but I also want to be ready if any user will unplug the communication cable so I use following code.
try
{
serialPort.WriteLine("BT\r");
}
catch (IOException ioe)
{
Console.WriteLine(ioe.Message);
currentCommunicationState = DEVICE_COMMUNICATION_STATES.IDLE;
// other stuff which set application in idle mode; buttons statuses, etc
}
The exception above is handled well. Applications goes to idle mode and I'm able to use it, but... when I close the application I get next exception without any more specified details. I cannot find place where this second exception is thrown or how to preserve this situation.
System.IO.IOException was unhandled
Message="Specified port doesn't exist"
Source="System"
StackTrace:
w System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
w System.IO.Ports.SerialStream.Dispose(Boolean disposing)
w System.IO.Ports.SerialStream.Finalize()
InnerException:
Log from console
Port 'COM29' nie istnieje. \\port COM28 doesn't exist - called from my exception handler
A first chance exception of type 'System.IO.IOException' occurred in System.dll
The thread 0x14d4 has exited with code 0 (0x0).
The thread 0x1e98 has exited with code 0 (0x0).