public ATCommand(string atRequest, ATResponse atResponse, TimeSpan waitForAnswer, int timesToRepeat) { this.Init(); this.ATRequest = atRequest; this.ExpectedATResponse = atResponse; this.WaitForResponse = waitForAnswer; this.TimesToRepeat = timesToRepeat; }
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); } }
public void TestSms() { GenericModem genericModem = new GenericModem(port); genericModem.Connect(); ATResponse response = genericModem.SendSms(new Sms("+79206239079", "Hello Yury!"), TimeSpan.FromSeconds(5)); Assert.AreEqual(ATComandResult.Ok, response.Result); Console.WriteLine(response.Response); genericModem.Disconnect(); }
public void TestATCommand() { GenericModem genericModem = new GenericModem(port); genericModem.Connect(); ATResponse response = genericModem.ATCommand(new ATRequest("at+cgmi\r"), TimeSpan.FromSeconds(5)); Assert.AreEqual(ATComandResult.Ok, response.Result); Console.WriteLine(response.Response); response = genericModem.ATCommand(new ATRequest("at+unknown\r"), TimeSpan.FromSeconds(5)); Assert.AreEqual(ATComandResult.Error, response.Result); genericModem.Disconnect(); }
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)); } } } }
ReceiveData RecognizeIncomingData(byte[] IncomingBytes) { ReceiveData RecData = null; if (IncomingBytes is byte[] && IncomingBytes.Length > 0) { string IncomingString = Encoding.UTF8.GetString(IncomingBytes).ToLower(); //обнуляем пришедшую АТ-команду ATResponse atResponse = ATResponse.UNKNOWN; if (IncomingString.Contains("ok")) { atResponse = ATResponse.OK; } else if (IncomingString.Contains(">")) { atResponse = ATResponse.SMS; } else if (IncomingString.Contains("connect")) { atResponse = ATResponse.CONNECT; } else if (IncomingString.Contains("ring")) { atResponse = ATResponse.RING; } else if (IncomingString.Contains("no carrier")) { atResponse = ATResponse.NO_CARRIER; } else if (IncomingString.Contains("error")) { atResponse = ATResponse.ERROR; } else if (IncomingString.Contains("no dialton")) { atResponse = ATResponse.NO_DIALTONE; } else if (IncomingString.Contains("busy")) { atResponse = ATResponse.BUSY; } else if (IncomingString.Contains("no answer")) { atResponse = ATResponse.NO_ANSWER; } if (atResponse == ATResponse.UNKNOWN) { if (IncomingString.Contains("+++")) { //atResponse = ATResponse.PLUSPLUSPLUS; } else if (IncomingString.Contains("^sysstart")) { //atResponse = ATResponse.SYSSTART; Init(false); } else if (IncomingString.Contains("+csca")) { //phone number sms-center //+CSCA: "+79168999100",145 //@"\+csca: ""\+(\d{11})"",\d+" Regex rgxPhoneNumberSMSCenter = new Regex(@"\+(\d{11})"); Match match = rgxPhoneNumberSMSCenter.Match(IncomingString); if (match.Success) { string test = match.Value; } } } if (atResponse != ATResponse.UNKNOWN) { RecData = new ReceiveATResponse(IncomingBytes, atResponse); } else { RecData = new ReceiveData(IncomingBytes); } } return(RecData); }
public ReceiveATResponse(byte[] Bytes, ATResponse atResponse) { this.ATResponse = atResponse; this.Bytes = Bytes; }