Read and save IMEI from serial port

Hello Gents, need your help
I have Mega2560 and SIM900 Shield connected on Serial2. My task is to catch IMEI from AT (AT+GSN) response. How I can do it?
I created this code, but it is not correct for this task/

// Buffer function
 void IMEI_get () {
sendATcommand("AT+GSN", "OK", "ERROR", 500);
 for (i=0;i < 64;i++){
  if (Serial2.available()) {
  c = Serial2.read();
  IMEI = String(IMEI + c);
 if (IMEI.length()>=64){
 Serial.println(IMEI);
 IMEI ="";
     }
   }
 }
}

To quote an often used statement "it's probably in the code you didn't post".

Paul

char char_buffer;
String string_buffer = "";
int buffer_space = 1000;

int onModulePin = 9;
int8_t answer;
String IMEI;

 int i =1;
 char c;

//==============================================================================
void setup()
{


Serial.begin(9600);           
Serial2.begin(9600);           
delay(2000);

   Serial.println("Reading IMEI...");
   void modem_power_on();
   IMEI_get ();
   void modem_power_off();
   Serial.println("IMEI=" + IMEI);

}
//=========================================================================
void loop()
{
}
// SendATcommand function
byte sendATcommand(String ATcommand, String answer1, String answer2, unsigned int timeout){
   byte reply = 1;
   String content = "";
   char character;
   //Clean the modem input buffer
   while(Serial2.available()>0) Serial2.read();

   //Send the atcommand to the modem
   Serial2.println(ATcommand);
   delay(100);
   unsigned int timeprevious = millis();
   while((reply == 1) && ((millis() - timeprevious) < timeout)){
     while(Serial2.available()>0) {
       character = Serial2.read();
       content.concat(character);
       Serial.print(character);
       delay(10);
     }
     //Stop reading conditions
     if (content.indexOf(answer1) != -1){
       reply = 0;
     }else if(content.indexOf(answer2) != -1){
       reply = 2;
     }else{
       //Nothing to do...
     }
   }
   return reply;
}
// Modem ON fuction
void modem_power_on(){

    uint8_t answer=0;

    // проверяем запущен ли уже модем
    answer = sendATcommand("AT", "OK", "ERROR", 2000);
    if (answer == 1)
    {
        digitalWrite(onModulePin,HIGH);
        delay(3000);
        digitalWrite(onModulePin,LOW);

        while(answer == 1){     // Отправляем запрос каждые 2 сек и ждем ответа модема
            answer = sendATcommand("AT", "OK", "ERROR", 2000);
        }
    }
}
// Modem OFF function
void modem_power_off(){

    uint8_t answer=0;

    // проверяем запущен ли уже модем
    answer = sendATcommand("AT", "OK", "ERROR", 2000);
    if (answer == 0)
    {
        digitalWrite(onModulePin,HIGH);
        delay(3000);
        digitalWrite(onModulePin,LOW);

        while(answer == 0){     // Отправляем запрос каждые 2 сек и ждем ответа модема
            answer = sendATcommand("AT", "OK", "ERROR", 2000);
        }
    }
}
// Get IMEI function
 void IMEI_get () {
sendATcommand("AT+GSN", "OK", "ERROR", 500);
 for (i=0;i < 64;i++){
  if (Serial2.available()) {
  c = Serial2.read();
  IMEI = String(IMEI + c);
 if (IMEI.length()>=64){
 Serial.println(IMEI);
 IMEI ="";
     }
    }
   }
 }
   void modem_power_on();
   IMEI_get ();
   void modem_power_off();

Guess what that code doesn't do.

Guess also what the code not in loop() doesn't do.

 for (i=0;i < 64;i++){
  if (Serial2.available()) {

I can almost guarantee that every time you call Serial2.available, it will return zero.

previous error (function call) was corrected... thank you

char char_buffer;
String string_buffer = "";
int buffer_space = 1000;

int onModulePin = 9;
int8_t answer;
String IMEI;

 int i =1;
 char c;

//==============================================================================
void setup()
{

Serial.begin(9600);           
Serial2.begin(9600);           
delay(2000);

   Serial.println("Reading IMEI...");
   modem_power_on();
   IMEI_get ();
   modem_power_off();
   Serial.println("IMEI=" + IMEI);

}
//=========================================================================
void loop()
{
}
// SendATcommand function
byte sendATcommand(String ATcommand, String answer1, String answer2, unsigned int timeout){
   byte reply = 1;
   String content = "";
   char character;
   //Clean the modem input buffer
   while(Serial2.available()>0) Serial2.read();

   //Send the atcommand to the modem
   Serial2.println(ATcommand);
   delay(100);
   unsigned int timeprevious = millis();
   while((reply == 1) && ((millis() - timeprevious) < timeout)){
     while(Serial2.available()>0) {
       character = Serial2.read();
       content.concat(character);
       Serial.print(character);
       delay(10);
     }
     //Stop reading conditions
     if (content.indexOf(answer1) != -1){
       reply = 0;
     }else if(content.indexOf(answer2) != -1){
       reply = 2;
     }else{
       //Nothing to do...
     }
   }
   return reply;
}
// Modem ON fuction
void modem_power_on(){

    uint8_t answer=0;

    // проверяем запущен ли уже модем
    answer = sendATcommand("AT", "OK", "ERROR", 2000);
    if (answer == 1)
    {
        digitalWrite(onModulePin,HIGH);
        delay(3000);
        digitalWrite(onModulePin,LOW);

        while(answer == 1){     // Отправляем запрос каждые 2 сек и ждем ответа модема
            answer = sendATcommand("AT", "OK", "ERROR", 2000);
        }
    }
}
// Modem OFF function
void modem_power_off(){

    uint8_t answer=0;

    // проверяем запущен ли уже модем
    answer = sendATcommand("AT", "OK", "ERROR", 2000);
    if (answer == 0)
    {
        digitalWrite(onModulePin,HIGH);
        delay(3000);
        digitalWrite(onModulePin,LOW);

        while(answer == 0){     // Отправляем запрос каждые 2 сек и ждем ответа модема
            answer = sendATcommand("AT", "OK", "ERROR", 2000);
        }
    }
}
// Get IMEI function
 void IMEI_get () {
sendATcommand("AT+GSN", "OK", "ERROR", 500);
 for (i=0;i < 64;i++){
  if (Serial2.available()) {
  c = Serial2.read();
  IMEI = String(IMEI + c);
 if (IMEI.length()>=64){
 Serial.println(IMEI);
 IMEI ="";
     }
    }
   }
 }

TolpuddleSartre:

 for (i=0;i < 64;i++){

if (Serial2.available()) {


I can almost guarantee that every time you call Serial2.available, it will return zero.

it seems that this is so... every time I got "IMEI = "

So, what I wrote was true.
"Yay!" me.

Robin2 has a good serial input basics tutorial.
It works for Serial2 as well as for Serial.

#Example5 is more like my task, but I don't understand how to apply it in setup section (not in loop).

loop() repeats forever, not just 64 times.

yes, but in my case I need only once send AT+GSN and only once parse response.

So, loop enough times to fill your IMEI.

Suppose that, in your for loop, you only incremented i when you actually read a character. How many times would the loop iterate, then?

How do you KNOW that you will get exactly 64 characters back?

I can't figure out what that code should look like. Could you be so kind to help me?

You apparently need to read 64 characters.
A for loop will run as quickly as possible, through all non-blocking iterations.
So, a for loop is not what you want.

You need to loop until you have received 64 characters.
I wrote a clue there.

I wrote a clue there.

Perhaps a bit too subtle...

   byte i=0;
   while(i < 64)
   {
      if(Serial2.available() > 0) // available() does not return true or false
      {
         someArray[i++] = Serial2.read();
      }
   }