bool _SendATCommand(ATCommand atCommand) { lock (_lockSendATCommand) { currentATCommandStatus = StatusATCommand.Wait; currentATCommandResponse = ATResponse.UNKNOWN; expectedATCommandResponse = atCommand.ExpectedATResponse; int TimesToRepeat = atCommand.TimesToRepeat; bool result = false; while (IsOpen && TimesToRepeat-- > 0) { COMPort.Write(atCommand.ATRequest, atCommand.LineBreak); RiseEvent(this, new SendMessageEventArgs(EventType.Tx, MessageType.AT, "send at-command", atCommand)); //Ожидаем ответа //ожидаем таймаута или события о получении верного ответа из метода Event_Note (выше) waitForATCommandResponse.Reset(); waitForATCommandResponse.WaitOne(atCommand.WaitForResponse); if (currentATCommandStatus == StatusATCommand.Done) { result = true; break; } else if (currentATCommandStatus == StatusATCommand.Abort) { result = false; break; } if (atCommand.DelayBetweenRepeats.TotalMilliseconds > 0) { //пауза между повторной отправкой команды waitForATCommandResponse.Reset(); waitForATCommandResponse.WaitOne(atCommand.DelayBetweenRepeats); if (currentATCommandStatus == StatusATCommand.Done) { result = true; break; } else if (currentATCommandStatus == StatusATCommand.Abort) { result = false; break; } } } //обнуляем все регистры expectedATCommandResponse = ATResponse.UNKNOWN; currentATCommandResponse = ATResponse.UNKNOWN; currentATCommandStatus = StatusATCommand.None; return(result); } }
void StopATCommand(bool result) { if (currentATCommandStatus == StatusATCommand.Wait) { if (result) { currentATCommandStatus = StatusATCommand.Done; } else { currentATCommandStatus = StatusATCommand.Abort; } waitForATCommandResponse.Set(); } }