スレッドがシリアルポートをロックし、コマンドを継続的に送信し、特定のコマンドに対して「OK」応答を取得するまでループでSerialPort.Write(command)とSerialPort.ReadLine()を使用して応答を取得するマルチスレッドアプリケーションを作成しています。
ReadLine()呼び出し中にスレッドが切り替わり、TimeOutExceptionが発生する可能性はありますか?
タイムアウトを2000msに設定しています。
プログラムを数分間実行した後、ランダムなTmeOutExceptionsが発生します(約5〜10)
23332 10:14:23 AM AutoLoader.WinS IRP_MJ_WRITE Serial0 SUCCESS Length 9: STG|4.5..
23336 10:14:23 AM AutoLoader.WinS IRP_MJ_READ Serial0 SUCCESS Length 1: O
23339 10:15:54 AM AutoLoader.WinS IRP_MJ_WRITE Serial0 SUCCESS Length 7: GTSNS..
23342 10:15:54 AM AutoLoader.WinS IRP_MJ_READ Serial0 SUCCESS Length 3: K..
23347 10:15:54 AM AutoLoader.WinS IRP_MJ_WRITE Serial0 SUCCESS Length 7: GLSTS..
23351 10:15:54 AM AutoLoader.WinS IRP_MJ_READ Serial0 SUCCESS Length 1: O
23355 10:15:54 AM AutoLoader.WinS IRP_MJ_READ Serial0 SUCCESS Length 3: K..
10:14:23 AMの後、抑制したTimeOutExceptionが発生します。
while (response != "OK")
{
Thread.Sleep(1000);
_alComm.SendString("SSD|" + CurrentData.MeasuredDepthReturns + Environment.NewLine);
response = _alComm.ReceiveString(2000);
_alComm.SendString("STG|" + CurrentData.AvgGas + Environment.NewLine);
response = _alComm.ReceiveString(2000);
_alComm.SendString("GTSNS\r\n");
response = _alComm.ReceiveString(2000);
SetSamplingTimePercent(response);
}
と
public void SendString(string sDataToSend)
{
if (sDataToSend == "")
return;
try
{
AlPort.Write(sDataToSend);
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString(), "Error");
}
}
public string ReceiveString(int timeOut)
{
string inString = null;
try
{
// set read timeout
AlPort.ReadTimeout = timeOut;
inString = AlPort.ReadLine();
}
catch (TimeoutException ex)
{
return string.Empty;
}
return inString;
}