我有Arduino GSM屏蔽,我有一個名爲SimpleGSM的類來發送短信。 SimpleGSM類繼承了SoftwareSerial。如何解析Arduino中AT命令的響應?
我有以下方法:
bool
SimpleGSM::sendSMS (const String phoneNumber, const String message)
{
const byte CONTROL_Z_HEX_CODE = 0x1A;
const unsigned long SERIAL_DELAY_TIME = 500;
this->flush();
this->print(F("AT+CMGS=\""));
this->print(phoneNumber);
this->print(F("\"\r"));
delay(SERIAL_DELAY_TIME);
this->flushInput();
this->print(message);
this->write(CONTROL_Z_HEX_CODE);
delay(SERIAL_DELAY_TIME);
this->flushInput();
}
上述方法成功地發送短信,但我不檢查AT命令的響應。
我有用於禁用ECHO和調制解調器設置爲SMS文本模式一些其他方法:
bool
SimpleGSM::setEcho (const bool state)
{
this->flush();
this->print(F("ATE"));
this->print(state);
this->print(F("\r"));
return this->find(OK_RESPONSE_FORMAT);
}
bool
SimpleGSM::setSMSMode (const byte mode)
{
this->flush();
this->print(F("AT+CMGF="));
this->print(mode);
this->print(F("\r"));
return this->find(OK_RESPONSE_FORMAT);
}
以上兩種方法成功地處理的AT命令的響應。
我的問題是,我想處理這樣的sendSMS方法的AT命令的響應:
bool
SimpleGSM::sendSMS (const String phoneNumber, const String message)
{
const byte CONTROL_Z_HEX_CODE = 0x1A;
this->flush();
this->print(F("AT+CMGS=\""));
this->print(phoneNumber);
this->print(F("\"\r"));
if (!this->find("\r\n> "))
{
return false;
}
this->print(message);
this->write(CONTROL_Z_HEX_CODE);
return this->find(OK_RESPONSE_FORMAT);
}
但是,由於某種原因,這是行不通的。 'this-> find(「\ r \ n>」)'總是返回false。
請考慮也是OK_RESPONSE_FORMAT是:
char * SimpleGSM::OK_RESPONSE_FORMAT = "\r\nOK\r\n";
有沒有人能真正幫助我? – 2015-02-24 22:54:52
您可以添加一個循環來簡單地打印返回的所有內容,然後檢查結果。這可能是因爲調制解調器沒有發送您期望的消息,或者發送的格式稍有不同。 – Mick 2015-02-26 10:18:20