SMS DECODER GUIDANCE NEEDED

Hi masters,

Im I'm trying to do sms alarme the sending parte of the sms is working fine and im lost in the reding-decoding one is use this code:

#include <SoftwareSerial.h>
SoftwareSerial phone(2, 3);

char incomingByte = 0;

void setup(){
phone.begin(9600);
delay(500);
Serial.begin(9600);
phone.println("AT");//text mode
delay(500);
phone.println("AT");//text mode
delay(500);
phone.println("AT+CMGF=1");//text mode
delay(500);
phone.println("AT+CPMS="SM","SM","SM"\r\n");
delay(500);
phone.println("AT+CMGL="ALL"\r");
//Serial.println("AT+CMGD=1");//delete sms
delay(500);
}
void loop() {
while(1){
if (phone.available() > 0) {
incomingByte = phone.read();
Serial.print(incomingByte);
}
}
}

and i got the 2 sms in sim card, this is the output result from serial monitor:

AT
OK
AT OK
AT+CMGF=1 OK
AT+CPMS="SM","SM","SM" +CPM+CMGL: 2,"REC READ","91",,"12/07/17,10:06:45+04",143,38
Plano de tarifas em vigor: Vita 91 SMS
+CMGL: 3,"REC READ","351xxxxxxxx","Meu91","12/07/23,23:35:47+04",145,11
Minha vida

OK

Now how can i do it to put the "phone.read()" in order to do something like this

if (incomingByte[0] == 'T' && incomingByte[1] == 'o' && incomingByte[2] == ' ' && incomingByte[3] == 'r')
{
.........
}

Im new at arduino (I come from PIC programing in PBP i now how to do it in PBP) please point me the way to do it.

Many thanks for care and share

Paulo

Now how can i do it to put the "phone.read()" in order to do something like this

You need to collect the data in a char array. You'll need an index variable, too.

Periodically, you'll need to reset the index variable, and empty the array (set the first byte to NULL). When? Whenever you start a new packet. When it that? When the old packet ends. When is that? I don't know. You'll need to figure that out.

I see carriage returns and line feeds that might be (OK, probably are) end of packet markers.

As for the "something like this part", I don't see "To r" anywhere in the output you showed. Do you?

PaulS:

Now how can i do it to put the "phone.read()" in order to do something like this

You need to collect the data in a char array. You'll need an index variable, too.

Periodically, you'll need to reset the index variable, and empty the array (set the first byte to NULL). When? Whenever you start a new packet. When it that? When the old packet ends. When is that? I don't know. You'll need to figure that out.

I see carriage returns and line feeds that might be (OK, probably are) end of packet markers.

As for the "something like this part", I don't see "To r" anywhere in the output you showed. Do you?

Hi,

First thanks for your reply. The "To r" it was just a example that i came in to explain what i want to do ....

Ive try this but no sucess

#include <SoftwareSerial.h>
SoftwareSerial phone(2, 3); 

char smspass [150];
int smsdata;

char pass [2];
int data; 


void setup(){
  phone.begin(9600);
  delay(500);
  Serial.begin(9600);
   phone.println("AT");//text mode
  delay(500);
   phone.println("AT");//text mode
  delay(500);
}
void loop() {
    
sms(); delay(5000);
    } 

void sms() {
  
   phone.println("AT");//text mode
  delay(500);
   phone.println("AT+CMGF=1");//text mode
  delay(500);
  phone.println("AT+CPMS=\"SM\",\"SM\",\"SM\"\r\n");
  delay(500);
  phone.println("AT+CMGL=\"ALL\"\r");
  delay(450);
  
if (phone.available() >1) { //assumir que o outro byte vem a caminho. 
    for (int i = 0; i<149; i ++) {
      smspass[i] = phone.read();
    }
    delay(1000);
    smspass[149] = '\0'; //end string
   Serial.print("******* smspass: ");Serial.println(smspass);
   Serial.println(" ");
  // Serial.print("smspass 57: ");Serial.print(smspass[57]);Serial.println(smspass[58]);
   
             if   ((strcmp(smspass,"Plano") == 0)) {
       Serial.println("Plano   OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK ");
}
}
}

Please Hellp

Thanks

Please Hellp

OK.

sms(); delay(5000);

It's ONE statement per line.

void loop() {
    
sms(); delay(5000);
    }

It's nice when everything is nicely indented, unlike your code. Use Tools + Auto Format, and fix the awful indenting.

if (phone.available() >1) { //assumir que o outro byte vem a caminho. 
    for (int i = 0; i<149; i ++) {
      smspass[i] = phone.read();
    }

If there are two or more bytes available, read all 150 of them. Got it. It's wrong, though.

You can't read more than are available.

             if   ((strcmp(smspass,"Plano") == 0)) {
       Serial.println("Plano   OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK ");
}

Hmmm.

This:

AT
OK
AT OK
AT+CMGF=1 OK
AT+CPMS="SM","SM","SM" +CPM+CMGL: 2,"REC READ","91",,"12/07/17,10:06:45+04",143,38
Plano de tarifas em vigor: Vita 91 SMS
+CMGL: 3,"REC READ","351xxxxxxxx","Meu91","12/07/23,23:35:47+04",145,11
Minha vida

is not equal to "Plano" in my world.

You need something like this:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

You can probably get rid of the started and SOP stuff, since you don't control what the phone sends. The EOP will be either a line feed or a carriage return. You decide which (better to use whichever the phone sends first). Then, you do nothing until the end of the packet has been received, unlike what you are trying to do now.

Hi Master PaulS,

I have made one change to your code char inData[80]; to char inData[280]; the 1st was to small to receive all the data.
Ive try instead off #define EOP '>' '/n' and '/r' but no changes

I try to read the letters "REC READ" but there is 2 problems:

This is the code i used:

#include <SoftwareSerial.h>
SoftwareSerial phone(2, 3); 

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[280];
byte index;

void setup(){
  phone.begin(9600);
  delay(500);
  Serial.begin(9600);
  phone.println("AT");
  delay(500);
}

void loop() 
 {
phone.println("AT");
  delay(500);
  phone.println("AT+CMGF=1");//text mode
  delay(500);
  phone.println("AT+CPMS=\"SM\",\"SM\",\"SM\"\r\n");
  delay(500);
  phone.println("AT+CMGL=\"ALL\"\r");
  //Serial.println("AT+CMGD=1");//delete sms
  delay(500);
   
 while(phone.available() > 0)
    {
    char inChar = phone.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 279)
    {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
    }
    }
    }

  if(started && ended)
  {
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
  
// READ  "REC READ" 
 Serial.print(inData[73]); Serial.print(inData[74]); Serial.print(inData[75]);Serial.print(" "); Serial.print(inData[77]); Serial.print(inData[78]); Serial.print(inData[79]); Serial.println(inData[80]); 
}

Below you can the outut data to terminal:

000 0000 - 1st reading (loop) i get 7 rectangles (I wrote zeros because cant copy the rectangles from terminal)
REC READ - from 2 to 4 reading (loop)got the correct data "REC READ"
REC READ
REC READ
REC READ
REA ","3  - from 5 wrong again in cycles of 4 same data then change to other cycle of 4 and so.....
REA ","3
REA ","3
REA ","3
"," 5191
"," 5191
"," 5191
"," 5191
519 0172
519 0172
519 0172
519 0172
017 170"
017 170"
017 170"
017 170"

This is the nw sms i got on sim:

AT

OK
AT

OK
AT+CMGF=1

OK
AT+CPMS="SM","SM","SM"

+CPM+CMGL: 2,"REC READ","351910172170","Meu91","12/07/24,22:38:14+04",145,4
Stop

OK

Please hellp me im lost...

Thanks

Paulo

None of your packets start with the SOP value (the start of packet marker).

Get rid of SOP and started. Change EOP to '\n'.

while(phone.available() > 0)
{
    char inChar = phone.read();
    if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
       if(index < 279)
       {
          inData[index] = inChar;
          index++;
          inData[index] = '\0';
       }
    }
}

if(ended)
{
    ended = false;
    index = 0;
    inData[index] = '\0';
}

None of the record is 279 characters long. The REC READ portion of the message does not start at position 73.

Hi Master,

Still not working big mess..
24 incorrect readings and then always ok

#include <SoftwareSerial.h>
SoftwareSerial phone(2, 3); 

#define EOP '/n'

bool started = false;
bool ended = false;

char inData[280];
byte index;

void setup(){
  phone.begin(9600);
  delay(500);
  Serial.begin(9600);
   phone.println("AT");//text mode
  delay(500);
   phone.println("AT");//text mode
  delay(500);
   phone.println("AT+CMGF=1");//text mode
  delay(500);
  phone.println("AT+CPMS=\"SM\",\"SM\",\"SM\"\r\n");
  delay(500);
  phone.println("AT+CMGL=\"ALL\"\r");
  //Serial.println("AT+CMGD=1");//delete sms
  delay(500);
}

    void loop()
{
  while(phone.available() > 0)
{
    char inChar = phone.read();
    if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
       if(index < 279)
       {
          inData[index] = inChar;
          index++;
          inData[index] = '\0';
       }
    }
}

if(ended)
{
    ended = false;
    index = 0;
    inData[index] = '\0';
}
Serial.print(inData[73]); Serial.print(inData[74]); Serial.print(inData[75]);Serial.print(" "); Serial.print(inData[77]); Serial.print(inData[78]); Serial.print(inData[79]); Serial.println(inData[80]); 
}

Terminal output

000 0000 - 1st reading (loop) i get 7 rectangles (I wrote zeros because cant copy the rectangles from terminal)
000 0000 - and 24 reading (loop) more get  get 7 rectangles !!!!!!!!!
REC READ - 25 reading ok and  from here got the correct data "REC READ" 
REC READ
REC READ
REC READ

Please hellp me im lost...

Thanks

Paulo

Please hellp me im lost...

In the if(ended) block, print out inData. It is a NULL terminated array of chars. What is in the array?

By the way, the carriage return is a \n, not a /n.

#define EOP '\n'

if(ended)
{
    ended = false;
    index = 0;
    inData[index] = '\0';
 Serial.print(inData);
}

No data to terminal

if(ended)
{
    ended = false;
    index = 0;
    inData[index] = '\0';
   // Serial.print(inData);
 Serial.print(inData[73]); Serial.print(inData[74]); Serial.print(inData[75]);Serial.print(" "); Serial.print(inData[77]); Serial.print(inData[78]); Serial.print(inData[79]); Serial.println(inData[80]); 
}

Terminal

000 0000
000 0000
000 0000
000 0000
000 0000
000 0000
000 0000
,40000

,40000

,40000

,40000

then stop incoming

No data to terminal

Then print every damned character that comes in, to figure out what the EOP IS!

This income data using

Serial.print(inChar);

AT
OK
AT
OK
AT+CMGF=1
OK
AT+CPMS="SM","SM","SM"
+CPM+CMGL: 2,"REC READ","351910172170","Meu91","12/07/24,22:38:14+04",145,4
Stop

OK

This income data using

Serial.print(inChar);

Where does one string start and end? Use this:

Serial.print("inData: [");
Serial.print(inData);
Serial.println("]");

so you can tell.

#include <SoftwareSerial.h>
SoftwareSerial phone(2, 3); 

#define EOP '\n'

bool started = false;
bool ended = false;

char inData[280];
byte index;

void setup(){
  phone.begin(9600);
  delay(500);
  Serial.begin(9600);
   phone.println("AT");//text mode
  delay(500);
   phone.println("AT");//text mode
  delay(500);
   phone.println("AT+CMGF=1");//text mode
  delay(500);
  phone.println("AT+CPMS=\"SM\",\"SM\",\"SM\"\r\n");
  delay(500);
  phone.println("AT+CMGL=\"ALL\"\r");
  //Serial.println("AT+CMGD=1");//delete sms
  delay(500);
}

    void loop()
{
  while(phone.available() > 0)
{
   
    char inChar = phone.read();
    if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
       if(index < 279)
       {
          inData[index] = inChar;
          index++;
          inData[index] = '\0';
          
Serial.print("inData: [");
Serial.print(inData);
Serial.println("]");

       }
    }
}

if(ended)
{
    ended = false;
    index = 0;
    inData[index] = '\0';
   
}
//Serial.print(inData[73]); Serial.print(inData[74]); Serial.print(inData[75]);Serial.print(" "); Serial.print(inData[77]); Serial.print(inData[78]); Serial.print(inData[79]); Serial.println(inData[80]); 
}

Terminal:

inData: [A]
inData: [AT]
inData: [AT]
inData: [AT]
inData: [O]
inData: [OK]
inData: [OK]
inData: [A]
inData: [AT]
inData: [AT]
inData: [AT]
inData: [O]
inData: [OK]
inData: [OK]
inData: [A]
inData: [AT]
inData: [AT+]
inData: [AT+C]
inData: [AT+CM]
inData: [AT+CMG]
inData: [AT+CMGF]
inData: [AT+CMGF=]
inData: [AT+CMGF=1]
inData: [AT+CMGF=1]
inData: [AT+CMGF=1]
inData: [O]
inData: [OK]
inData: [OK]
inData: [A]
inData: [AT]
inData: [AT+]
inData: [AT+C]
inData: [AT+CP]
inData: [AT+CPM]
inData: [AT+CPMS]
inData: [AT+CPMS=]
inData: [AT+CPMS="]
inData: [AT+CPMS="S]
inData: [AT+CPMS="SM]
inData: [AT+CPMS="SM"]
inData: [AT+CPMS="SM",]
inData: [AT+CPMS="SM","]
inData: [AT+CPMS="SM","S]
inData: [AT+CPMS="SM","SM]
inData: [AT+CPMS="SM","SM"]
inData: [AT+CPMS="SM","SM",]
inData: [AT+CPMS="SM","SM","]
inData: [AT+CPMS="SM","SM","S]
inData: [AT+CPMS="SM","SM","SM]
inData: [AT+CPMS="SM","SM","SM"]
inData: [AT+CPMS="SM","SM","SM"]
inData: [AT+CPMS="SM","SM","SM"]
inData: [+]
inData: [+C]
inData: [+CP]
inData: [+CPM]
inData: [+CPM+]
inData: [+CPM+C]
inData: [+CPM+CM]
inData: [+CPM+CMG]
inData: [+CPM+CMGL]
inData: [+CPM+CMGL:]
inData: [+CPM+CMGL: ]
inData: [+CPM+CMGL: 2]
inData: [+CPM+CMGL: 2,]
inData: [+CPM+CMGL: 2,"]
inData: [+CPM+CMGL: 2,"R]
inData: [+CPM+CMGL: 2,"RE]
inData: [+CPM+CMGL: 2,"REC]
inData: [+CPM+CMGL: 2,"REC ]
inData: [+CPM+CMGL: 2,"REC R]
inData: [+CPM+CMGL: 2,"REC RE]
inData: [+CPM+CMGL: 2,"REC REA]
inData: [+CPM+CMGL: 2,"REC READ]
inData: [+CPM+CMGL: 2,"REC READ"]
inData: [+CPM+CMGL: 2,"REC READ",]
inData: [+CPM+CMGL: 2,"REC READ","]
inData: [+CPM+CMGL: 2,"REC READ","3]
inData: [+CPM+CMGL: 2,"REC READ","35]
inData: [+CPM+CMGL: 2,"REC READ","35,]
inData: [+CPM+CMGL: 2,"REC READ","35,,]
inData: [+CPM+CMGL: 2,"REC READ","35,,]

Move the 3 Serial.print() statements after the if(ended) curly brace.

Add 2 more after the char inChar statement.

Serial.print("Read: ");
Serial.println(inChar, HEX);

We need to confirm that your understanding of what constitutes an end of packet marker is true. It appears to be, since the index value gets reset at what appears to be appropriate times.

If the EOP value is correct, you need to look for "REC READ" in inData, after the if(ended) curly brace, and, if present, extract the data of interest from inData.

#include <SoftwareSerial.h>
SoftwareSerial phone(2, 3); 

#define EOP '\n'

bool started = false;
bool ended = false;

char inData[280];
byte index;

void setup(){
  phone.begin(9600);
  delay(500);
  Serial.begin(9600);
   phone.println("AT");//text mode
  delay(500);
   phone.println("AT");//text mode
  delay(500);
   phone.println("AT+CMGF=1");//text mode
  delay(500);
  phone.println("AT+CPMS=\"SM\",\"SM\",\"SM\"\r\n");
  delay(500);
  phone.println("AT+CMGL=\"ALL\"\r");
  //Serial.println("AT+CMGD=1");//delete sms
  delay(500);
}

    void loop()
{
  while(phone.available() > 0)
{
   
    char inChar = phone.read();
    if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
       if(index < 279)
       {
          inData[index] = inChar;
          index++;
          inData[index] = '\0';
       }
       
      Serial.print("inData: [");
      Serial.print(inData);
      Serial.println("]"); 
      Serial.print("Read: ");
      Serial.println(inChar, HEX);
    }
}

if(ended)
{
    ended = false;
    index = 0;
    inData[index] = '\0';
}
}

Terminal:

inData: [A]
Read: 41
inData: [AT]
Read: 54
inData: [AT]
Read: D
inData: [AT]
Read: D
inData: [O]
Read: 4F
inData: [OK]
Read: 4B
inData: [OK]
Read: D
inData: [A]
Read: 41
inData: [AT]
Read: 54
inData: [AT+]
Read: 2B
inData: [AT+C]
Read: 43
inData: [AT+CM]
Read: 4D
inData: [AT+CMG]
Read: 47
inData: [AT+CMGF]
Read: 46
inData: [AT+CMGF=]
Read: 3D
inData: [AT+CMGF=1]
Read: 31
inData: [AT+CMGF=1]
Read: D
inData: [AT+CMGF=1]
Read: D
inData: [O]
Read: 4F
inData: [OK]
Read: 4B
inData: [OK]
Read: D
inData: [A]
Read: 41
inData: [AT]
Read: 54
inData: [AT+]
Read: 2B
inData: [AT+C]
Read: 43
inData: [AT+CP]
Read: 50
inData: [AT+CPM]
Read: 4D
inData: [AT+CPMS]
Read: 53
inData: [AT+CPMS=]
Read: 3D
inData: [AT+CPMS="]
Read: 22
inData: [AT+CPMS="S]
Read: 53
inData: [AT+CPMS="SM]
Read: 4D
inData: [AT+CPMS="SM"]
Read: 22
inData: [AT+CPMS="SM",]
Read: 2C
inData: [AT+CPMS="SM","]
Read: 22
inData: [AT+CPMS="SM","S]
Read: 53
inData: [AT+CPMS="SM","SM]
Read: 4D
inData: [AT+CPMS="SM","SM"]
Read: 22
inData: [AT+CPMS="SM","SM",]
Read: 2C
inData: [AT+CPMS="SM","SM","]
Read: 22
inData: [AT+CPMS="SM","SM","S]
Read: 53
inData: [AT+CPMS="SM","SM","SM]
Read: 4D
inData: [AT+CPMS="SM","SM","SM"]
Read: 22
inData: [AT+CPMS="SM","SM","SM"]
Read: D
inData: [AT+CPMS="SM","SM","SM"]
Read: D
inData: [+]
Read: 2B
inData: [+C]
Read: 43
inData: [+CP]
Read: 50
inData: [+CPM]
Read: 4D
inData: [+CPMS]
Read: 53
inData: [+CPMS:]
Read: 3A
inData: [+CPMS: ]
Read: 20
inData: [+CPMS: 2]
Read: 32
inData: [+CPMS: 2,]
Read: 2C
inData: [+CPMS: 2,3]
Read: 33
inData: [+CPMS: 2,30]
Read: 30
inData: [+CPMS: 2,30,]
Read: 2C
inData: [+CPMS: 2,30,2]
Read: 32
inData: [+CPMS: 2,30,2+]
Read: 2B
inData: [+CPMS: 2,30,2+C]
Read: 43
inData: [+CPMS: 2,30,2+CM]
Read: 4D
inData: [+CPMS: 2,30,2+CMG]
Read: 47
inData: [+CPMS: 2,30,2+CMGL]
Read: 4C
inData: [+CPMS: 2,30,2+CMGL:]
Read: 3A
inData: [+CPMS: 2,30,2+CMGL: ]
Read: 20
inData: [+CPMS: 2,30,2+CMGL: 2]
Read: 32
inData: [+CPMS: 2,30,2+CMGL: 2,]
Read: 2C
inData: [+CPMS: 2,30,2+CMGL: 2,"]
Read: 22
inData: [+CPMS: 2,30,2+CMGL: 2,"R]
Read: 52
inData: [+CPMS: 2,30,2+CMGL: 2,"RE]
Read: 45
inData: [+CPMS: 2,30,2+CMGL: 2,"REC]
Read: 43
inData: [+CPMS: 2,30,2+CMGL: 2,"REC1]
Read: 31
inData: [+CPMS: 2,30,2+CMGL: 2,"REC1:]
Read: 3A
inData: [+CPMS: 2,30,2+CMGL: 2,"REC1:K]
Read: 4B

f i do :

if(ended)
{
    ended = false;
    index = 0;
    inData[index] = '\0';
    
}
Serial.print("inData: [");
      Serial.print(inData);
      Serial.println("]"); 
      Serial.print("Read: ");
      Serial.println(inChar, HEX);
}

or

if(ended)
{
    ended = false;
    index = 0;
    inData[index] = '\0';

    Serial.print("inData: [");
      Serial.print(inData);
      Serial.println("]"); 
      Serial.print("Read: ");
      Serial.println(inChar, HEX);
}
}

Gives this error "inchar is not declarered in this scope"

Go back and read the 1st line of my previous reply. Then, read the 2nd line. None of your print statements are where they should be.

    void loop()
{
  while(phone.available() > 0)
{
   
    char inChar = phone.read();
      Serial.print("Read: ");
      Serial.println(inChar, HEX);

    if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
       if(index < 279)
       {
          inData[index] = inChar;
          index++;
          inData[index] = '\0';
       }
    }
}

if(ended)
{
      Serial.print("inData: [");
      Serial.print(inData);
      Serial.println("]"); 

    ended = false;
    index = 0;
    inData[index] = '\0';
}
}

And, use Tools + Auto format, to fix this bizarre indenting.

Nice tool thanks

  if(ended)
  {
    Serial.print("inData: [");
    Serial.print(inData);
    Serial.println("]"); 
    Serial.print("Read: ");
    Serial.println(inChar, HEX);
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Gives this error "inchar is not declarered in this scope"

Sory :frowning:

#include <SoftwareSerial.h>
SoftwareSerial phone(2, 3); 

#define EOP '\n'

bool started = false;
bool ended = false;

char inData[280];
byte index;

void setup(){
  phone.begin(9600);
  delay(500);
  Serial.begin(9600);
  phone.println("AT");//text mode
  delay(500);
  phone.println("AT");//text mode
  delay(500);
  phone.println("AT+CMGF=1");//text mode
  delay(500);
  phone.println("AT+CPMS=\"SM\",\"SM\",\"SM\"\r\n");
  delay(500);
  phone.println("AT+CMGL=\"ALL\"\r");
  //Serial.println("AT+CMGD=1");//delete sms
  delay(500);
}

void loop()
{
  while(phone.available() > 0)
  {

    char inChar = phone.read();
    Serial.print("Read: ");
    Serial.println(inChar, HEX);

    if(inChar == EOP)
    {
      ended = true;
      break;
    }
    else
    {
      if(index < 279)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  if(ended)
  {
    Serial.print("inData: [");
    Serial.print(inData);
    Serial.println("]"); 

    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Terminal:

Read: 41
Read: 54
Read: D
Read: D
Read: A
inData: [AT]
Read: 4F
Read: 4B
Read: D
Read: A
inData: [OK]
Read: 41
Read: 54
Read: D
Read: D
Read: A
inData: [AT]
Read: 4F
Read: 4B
Read: D
Read: A
inData: [OK]
Read: 41
Read: 54
Read: 2B
Read: 43
Read: 4D
Read: 47
Read: 46
Read: 3D
Read: 31
Read: D
Read: D
Read: A
inData: [AT+CMGF=1]
Read: 4F
Read: 4B
Read: D
Read: A
inData: [OK]
Read: 41
Read: 54
Read: 2B
Read: 43
Read: 50
Read: 4D
Read: 53
Read: 3D
Read: 22
Read: 53
Read: 4D
Read: 22
Read: 2C
Read: 22
Read: 53
Read: 4D
Read: 22
Read: 2C
Read: 22
Read: 53
Read: 4D
Read: 22
Read: D
Read: D
Read: A
inData: [AT+CPMS="SM","SM","SM"]
Read: 2B
Read: 43
Read: 50
Read: 4D
Read: 2B
Read: 43
Read: 4D
Read: 47
Read: 4C
Read: 3A
Read: 20
Read: 32
Read: 2C
Read: 22
Read: 52
Read: 45
Read: 43
Read: 20
Read: 52
Read: 45
Read: 41
Read: 44
Read: 22
Read: 2C
Read: 22
Read: 33
Read: 32
Read: 2C
Read: 32
Read: 22
Read: 2C
Read: 70

OK. So, you are seeing an end of packet marker, as evidenced by the inData stuff being printed after the if(ended) statement.

Comment out the first set of Serial.print() statements.

After the if(ended) statement, you now need to parse the replies from the device. Since the reply is a string (a NULL terminated array of chars), all the normal string functions (strstr(), for instance) can be used.