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); } }
bool Open() { try { COMPort.Open(); Init(true); RiseEvent(this, new SendMessageEventArgs(EventType.Sys, MessageType.Note, "port opened")); return(true); } catch (Exception ex) { RiseEvent(this, new SendMessageEventArgs(EventType.Sys, MessageType.Error, "failed to open the port", ex)); return(false); } }
bool Close() { try { //stop task open port if (!cancellationTokenOpenAsync) { cancellationTokenOpenAsync = true; } lock (lockOpenClose) { COMPort.Close(); } RiseEvent(this, new SendMessageEventArgs(EventType.Sys, MessageType.Note, "port closed")); return(true); } catch (Exception ex) { RiseEvent(this, new SendMessageEventArgs(EventType.Sys, MessageType.Error, "failed to close the port", ex)); return(false); } }
private void GSMModem_OnDataReceived(object sender, DataEventArgs e) { if (e is DataEventArgs) { byte[] data = (e as DataEventArgs).GetData(); IEnumerable <byte[]> ReceiveBytes = COMPort.BytesSplit(data, (byte)'\r', (byte)'\n'); foreach (byte[] Bytes in ReceiveBytes) { ReceiveData receiveData = RecognizeIncomingData(Bytes); if (receiveData is ReceiveATResponse) { currentATCommandResponse = (receiveData as ReceiveATResponse).ATResponse; if (currentATCommandResponse != ATResponse.UNKNOWN && currentATCommandResponse == expectedATCommandResponse) { StopATCommand(true); } if (currentATCommandResponse == ATResponse.NO_ANSWER || currentATCommandResponse == ATResponse.NO_CARRIER || currentATCommandResponse == ATResponse.ERROR || currentATCommandResponse == ATResponse.BUSY || currentATCommandResponse == ATResponse.NO_DIALTONE) { StopATCommand(false); } RiseEvent(this, new SendMessageEventArgs(EventType.Rx, MessageType.AT, "incoming at-command", (receiveData as ReceiveATResponse).ATResponse)); } else if (receiveData is ReceiveData) { RiseEvent(this, new SendMessageEventArgs(EventType.Rx, MessageType.Data, "other data", receiveData.String)); } } } }