如何解析 Arduino 中 AT 命令的响应?

How to parse the response of an AT command in Arduino?

我有 Arduino GSM shield,我有一个名为 SimpleGSM 的 class 来发送 SMS。 SimpleGSM class 继承了SoftwareSerial.

我有以下方法:

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> ")'总是returns false。

请注意 OK_RESPONSE_FORMAT 是:

char * SimpleGSM::OK_RESPONSE_FORMAT = "\r\nOK\r\n";

AT 命令响应通常由模块通过相同的连接发送。也就是说,如果你串行连接到模块,那么你发送到传输引脚,并从接收引脚读取(tx 与 rx)。