【问题标题】:Arduino: AT Commands - Read the last line of the serial output using Serial.read()Arduino:AT 命令 - 使用 Serial.read() 读取串行输出的最后一行
【发布时间】:2013-12-04 14:30:17
【问题描述】:

我不断通过 AT 命令获取 GSM 信号强度 我的代码复制了整个串行输出 请告知如何读取最新的串行输出(最后一行)

找到下面的输出,我需要将最后一行 (21,0) 的输出分配给变量“signal”

我的输出:

AT 

OK
AT+CREG?
+CREG: 0,1

ok
AT+CSQ

+CSQ: 21,0

我的代码:

byte gsmDriverPin[3] = {
3,4,5};

char signal[10];

char inChar;
int index;
char inData[200];


void setup()
{    
//Init the driver pins for GSM function
for(int i = 0 ; i < 3; i++){
pinMode(gsmDriverPin[i],OUTPUT);
}
digitalWrite(5,HIGH);//Output GSM Timing 
delay(1500);
digitalWrite(5,LOW);  
digitalWrite(3,LOW);//Enable the GSM mode
digitalWrite(4,HIGH);//Disable the GPS mode
delay(2000);
Serial.begin(9600); //set the baud rate
delay(5000);//call ready
delay(5000);
delay(5000);
start_GSM();

}

void loop()
{  
Signal_Strength();
Serial.println("AT+CMGF=1");
delay(1000);
Serial.println("AT+CMGS=\"1234567890\"");//Change the receiver phone number
delay(1000);
Serial.println(signal);
delay(1000);
Serial.write(26);
}

void Signal_Strength(){
Serial.println("AT+CSQ");
delay(2000);
read_String();    
strtok(inData, ",");
strcpy(signal,strtok(NULL, ",")); 

}

void read_String() {
index=0;

while(Serial.available() > 0) // Don't read unless
// there you know there is data
 {
 if(index < 199) // One less than the size of the array
 {
 inChar = Serial.read(); // Read a character
 inData[index] = inChar; // Store it
 index++; // Increment where to write next
 inData[index] = '\0'; // Null terminate the string
 }
 }
 }

 void start_GSM(){
 //Configuracion GPRS Claro Argentina
Serial.println("AT");
delay(2000);
Serial.println("AT+CREG?");
delay(2000);
 }

【问题讨论】:

  • 尝试在每次收到换行符时重置您的收集例程,然后看看您从上一次收集到的内容是否值得解析。或者构建一个状态机,在它们到达时逐字符处理。
  • @chirs,您能否详细说明或提供一些示例代码来处理

标签: c++ c embedded arduino at-command


【解决方案1】:

首先,你必须认真地重做你的AT命令处理

  • 读取并解析调制解调器返回的每个响应行,直到获得最终结果代码。这适用于每一个命令行调用,没有任何例外。有关详细信息,请参阅this answer
  • 永远不要在任何处理 AT 命令的代码中调用delay。有关中止下一个命令的风险的更多详细信息,请参阅this answer

在解决这些基本问题之前,您不能期望任何成功的行为。解析 AT 命令响应并不复杂,以 atinout 的源代码为例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多