Help in SMS Read function

Hello All,

I am trying to read sms by my own code. But my code is not working....
What is happened when a send the read sms command to GSM module it will send data

But my serialEvent never get any data...so it will not break the while loop.

Anyone have any idea what is going on why this is happened or what I am doing wrong .

Arduino board = Mega2560
GSM Module = sim900A
connection = Modem Rx to Board Tx, & Modem Tx to board Rx(USE serial1).

Thanks in advance

char sms_location = 0;
int sms_location1=0;
byte temp[512];
char sms_buffer[512]="";
uint16_t count=0;  
int flag = 0;
char new_msg=0;
uint16_t counter =0;


void serialEvent1()
{
   temp[count] = Serial1.read();
    if (temp[count] == 'K') {
      flag = 1;
      Serial.write(temp[count-1]);
    }
    if (temp[count-1] == ','){
      if (temp[count-3] == 'M'){
        if (temp[count-4] == 'S'){
        sms_location = temp[count];
        new_msg = 1;
        flag = 0;
      }
    }
  }
  if(temp[count] == 'R') {
    if (temp[count-1] == 'E'){
      Serial.println("GSM ERROR");
      }
    }
  count++;
}



void setup()
{
  Serial.begin(9600);                
  Serial1.begin(9600);             
  delay(100);
  Modem_Inti();
  Serial.println("GSM Init");
}
 
void loop()
{
  if (new_msg)
  {
    new_msg = 0;
    dataextract();
  }
}
void Modem_Inti()
{
  Serial1.println("AT+IPR=9600");
  delay(500);
  Serial1.println("AT");
  delay(500); 
  Serial1.println("ATE0");
  delay(500); 
  Serial1.println("AT+CMGF=1");
  delay(500);
  Serial1.println("AT+CNMI=2,1,0,0,0");
  delay(500);
  Serial1.println("AT&W");
  delay(500);
  Serial1.println("AT+CMGD=0,4");
  delay(1000);
  temp_clear();
}

void temp_clear()
{
  for (int a = 0; a<=sizeof(temp); a++)
  {
    temp[a] = 0;
  }
}

void dataextract()
{ 
   flag = 0;
   //Serial.write(temp,sizeof(temp));
   count = 0;
   Serial.println("SMS Process start");
   Serial1.print("AT+CMGR=");
   Serial1.println(sms_location1);
   delay(500);
   while(flag == 0){} // wait until K received 
   flag = 0;
   Serial.println("SMS Process stop");
   //strcpy(sms_buffer,temp);
   delay(1000);
   Serial.write(temp,sizeof(temp));
   Serial1.println("AT+CMGD=0,4");
   delay(1000);
   Serial.println("SMS delete");
   new_msg = 0;
   flag = 0;
   count = 0;
}

But my serialEvent never get any data...so it will not break the while loop.

SerialEvent is a big GotYa !!!!, it doesn't get called on an interrupt at all

Its a hang over from Processing where the Arduino IDE was derived from.

If you look in the core code which calls setup() and then repeadly calls loop()
all that happens is that after loop() exits, a call to SerialEvent is made if there is serial data

So basically its pointless. and you should use serial.available in your loop()

void temp_clear()
{
  for (int a = 0; a<=sizeof(temp); a++)
  {
    temp[a] = 0;
  }
}

If a function is not going to stop at the first stop sign, it's stupid to assume that it will stop at the 387th stop sign.

But my serialEvent never get any data...so it will not break the while loop.

How do you know? All that you know is that it is not getting the data you expect.

void serialEvent1()
{
   temp[count] = Serial1.read();

Print the character. Then, you just might have a clue.

@rogerClark,

SerialEvent is a big GotYa !!!!, it doesn't get called on an interrupt at all

That mean it's not serial interrupt at all? for that i have to set manually ?
I have another code which using two serials to operate GSM Modem manually it's working fine. that's why I suppose it's an serial interrupt.

void setup()
{
  // Initialize serial ports for communication.
  Serial.begin(9600);
  Serial1.begin(9600);

  Serial.println("Start Reset");
  Serial.println("End Reset");  

  Serial.println("Enter your AT commands (with CR & NL)...");
}

void loop()
{
 
}

void serialEvent1()
{
  if(Serial1.available() > 0){
    Serial.write(Serial1.read());
  }
}

void serialEvent()
{
    if(Serial.available() > 0){
    Serial1.write(Serial.read());
  }
}

@PaulS,

I Tried the same thing at first but it's never get any thing, I tried Print or write both function. Still did not get any data.

is it possible SerialEvent is not Serial interrupt? or may be I did not define properly? apart from that it should be work.

is it possible SerialEvent is not Serial interrupt? or may be I did not define properly? apart from that it should be work.

The call to serialEvent() does not happen because of interrupts. It happens because loop() ended AND there was serial data to be dealt with.

The fact that there is serial data to be dealt with happens because of interrupts.

There is no real advantage in using serialEvent(). You could just as easily have

   if(Serial.available() > 0)
      dealWithSerialData();

at the start, or end, of loop(), and deal with the serial data in dealWithSerialData().

Obviously, you'd need another test, and another function to deal with Serial1 data.

The advantage of dealing with serial data yourself, with your own functions is that you can handle software serial data in exactly the same way. That may not matter to you, now, but consistency is good. It makes coding much simpler to write and understand.