简体   繁体   中英

Arduino: AT Commands - Read the last line of the serial output using Serial.read()

I constantly pass AT commands to get GSM Signal Strength My code copies the entire serial output Kindly advise how to read the latest serial output (last line)

find the output below, in which i need to assign the output from last line (21,0) to the variable "signal"

My Output:

AT 

OK
AT+CREG?
+CREG: 0,1

ok
AT+CSQ

+CSQ: 21,0

My code:

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);
 }

First of all, you must seriously redo your AT command handling to

  • Read and parse the every single response line given back from the modem until you get a final result code. This applies for every single command line invocation, no exceptions whatsoever. See this answer for more details.
  • Never ever call delay in any code that handles AT commands. See this answer for more details about the risk of aborting the next command.

Before fixing these fundamental issues you cannot expect any successful behaviour. Parsing AT command responses is not that complicated, have a look at the source code for atinout for an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM