Serial.read() Latest output

Hi,

i’m doing my final year university project with SIM908 Module
where 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)

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

OUTPUT

AT 

OK
AT+CREG?
+CREG: 0,1

ok
AT+CSQ

+CSQ: 21,0

You'll have first to explain what you mean by last - its not possible to predict the future. For instance do you mean last line in the currently available chars, but you are allowed to wait for the next newline?

Hi Mark,

thanks for your reply, find the output below, in which i need to assign the output from last line (21,0) to the variable "signal" kindly advise

AT 

OK
AT+CREG?
+CREG: 0,1

ok
AT+CSQ

+CSQ: 21,0

Will the line that you are interested in always start with +CSQ: and is the format of the line fixed ? Is the line terminated in any way, perhaps by a carriage return or linefeed or both ?

Yes, particular output always starts with +CSQ: and the format is fixed

Assuming that there is no option of not getting CSQ at all you can try:

  1. Reading one line at a time into a buffer. The '\n' character means end of line.
  2. Use strncmp function to test against the "+CSQ: " string.
  3. Extract the 21,0 from that buffer. If you need these number as integers, then you know that the first starts at position 6 of the buffer, and you can find the other by searching for the location of the ',' character (using strchr function). If everything is fixed, i.e 21 will not become 121 or 1 then it is even easier.
  4. Conversion of an array of character to integer can be done with atoi function.

OP: please don't bump threads after less than an hour - this isn't a chatroom.