アプリケーションでシリアル接続を使用するユーザーの奇妙な動作を防止しようとしています。
私のアプリケーションはUSB->serial converter経由でデバイスに接続します。ポートが利用可能で、接続され、準備ができていることを確認したら、データを送信する環境を設定していますが、ユーザーが通信ケーブルを抜いた場合に備えたいので、次のコードを使用します。
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
}
上記の例外は適切に処理されます。アプリケーションはアイドル モードになり、使用できますが、アプリケーションを閉じると、詳細が指定されていない次の例外が発生します。この 2 番目の例外がスローされる場所や、この状況を維持する方法が見つかりません。
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:
コンソールからのログ
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).