looking for sample SIM900 AT commandset

Hi,

I'm looking for some sample Sim900 AT commands, which can interpret the GSM responses, when a GSM shield makes a HTTP request.

I don't want to use the Sim900 libraries, due to the large amount of delay statements in the code.

As an example, if I take this code (taken from Arduino GPRS Shield - Geeetech Wiki):

void SubmitHttpRequest()
{
 mySerial.println("AT+CSQ");
 delay(100);
 ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too.
 mySerial.println("AT+CGATT?");
 delay(100);
 ShowSerialData();
 mySerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs
 delay(1000);
 ShowSerialData();
 mySerial.println("AT+SAPBR=3,1,\"APN\",\"CMNET\"");//setting the APN, the second need you fill in your local apn server
 delay(4000);
 ShowSerialData();
 mySerial.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT command mamual
 delay(2000);
 ShowSerialData();
 mySerial.println("AT+HTTPINIT"); //init the HTTP request
 delay(2000); 
 ShowSerialData();
 mySerial.println("AT+HTTPPARA=\"URL\",\"www.google.com.hk\"");// setting the httppara, the second parameter is the website you want to access
 delay(1000);
 ShowSerialData();
 mySerial.println("AT+HTTPACTION=0");//submit the request 
 delay(10000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
 //while(!mySerial.available());
 ShowSerialData();
 mySerial.println("AT+HTTPREAD");// read the data from the website you access
 delay(300);
 ShowSerialData();
 mySerial.println("");
 delay(100);
}

How do I run a "if statement" against the response from any of those commands?

You can change or remove the delay but I believe that they are there because of the slow response of the GSM network.
I think the delay can be changed in a loop that wait for data with a timeout, with delay you have to wait also if the network is responsive.

zoomx:
You can change or remove the delay but I believe that they are there because of the slow response of the GSM network.
I think the delay can be changed in a loop that wait for data with a timeout, with delay you have to wait also if the network is responsive.

Hi,

The Sim900 libraries is bloated with stuff which I don't need, like references to Softserial, and the delay functions. I don't want to re-write the whole library, but rather use simple AT commands. Sending AT commands via the Serial port to the Sim900 is fairly easy, and echo'ing it back to the Serial monitor is equally easy. but I don't know how to interpret the sim900 responses into the Arduino code.

i.e. I want to see if the sim900 has made a successful GPRS or 3G connection, and do something. Or, if it doesn't make a successful connection, do something else. or wait and retry a few minutes later.

Ok,
the commands are described here

a link that I found in the page you linked.

You can try these commands with a serial terminal, like Hyperterminal or RealTerm and see the response.

GPRS commands are on chapter 7, TCIP commands on chapter 8. I believe that these commands are a standard so you can use other libraries, maybe.

zoomx:
Ok,
the commands are described here
http://www.geeetech.com/Documents/SIM900_AT_Command_Manual_V1.03.pdf
a link that I found in the page you linked.

You can try these commands with a serial terminal, like Hyperterminal or RealTerm and see the response.

GPRS commands are on chapter 7, TCIP commands on chapter 8. I believe that these commands are a standard so you can use other libraries, maybe.

I think you misunderstood me.....

I need to know, in Arduino, how to interpret those commands.

I can't even show you a sketch, cause I don't know where to start.

In the serial monitor, I get:

AT+CSQ

+CSQ: 19,0

OK
AT+CGATT?

ERROR
AT+HTTPINIT

But, I want to, with code, be able to do something when a condition is met, or isn't met.

For example:

if(+CSQ == 19)
{
// run code
} else 
// run other code
}

Does it make sense?

But, I want to know when a PHP website gives me a response on code that I want to submit.

What would the Arduino code be to interpret the AT responses?

Let's take another example:

 Serial2.println("AT+CGATT?");
 delay(100);
 ShowSerialData();

Instead of just seeing the response from that command on the Serial Monitor, I want to process it in the Arduino Code.

i.e.

if (AT+CGATT? == "ERROR")
{
 Serial2.println(AT+CIPMUX);
// from here I want to read the response on the above command again
if(AT+CIPMUX==1)
{
// run next set of commands
}
}

P.S. My sim900 is connected to Serial2 on a Mega2560

Mathyewnaymer:
You can change it

I don't have the programming knowledge to re-write the whole libary and remove reference to Softserial....

You are already reading the reponse from the SIM900. Save the characters it returns until you get the 'OK' then act on it accordingly.

The response are ASCII strings. I believe you have 2 ways

  1. write code yourself to capture ASCII response process it.
  2. Using the library but instead of ShowSerialData() you can process what showserialdata should print.

someting like this

Serial2.println("AT+CGATT?");
 delay(100);
GetResponse();
Process MyBuffer  //this is pseudocode, it doesn't work

void GetResponse()
{
MyBuffer=""
 while(mySerial.available()!=0)
   MyBuffer+=mySerial.read();
}

where myBuffer is a string or you can use an array of chars.
But you have to improve your programming knowledge, manipulating strings is not a simple task.

zoomx:
The response are ASCII strings. I believe you have 2 ways

  1. write code yourself to capture ASCII response process it.
  2. Using the library but instead of ShowSerialData() you can process what showserialdata should print.

someting like this

Serial2.println("AT+CGATT?");

delay(100);
GetResponse();
Process MyBuffer  //this is pseudocode, it doesn't work

void GetResponse()
{
MyBuffer=""
while(mySerial.available()!=0)
   MyBuffer+=mySerial.read();
}



where myBuffer is a string or you can use an array of chars.
But you have to improve your programming knowledge, manipulating strings is not a simple task.

Thanx, this is probably what I'm looking for.

What kind of "declaration" should "MyBuffer" be set to, in order to accept the kind of characters returned from the Sim900?

If you dig out a copy of SerialRelay from the internet then that shows a simple way of achieving what you probably want to do. It reads incoming Serial from device, appends to a character array, and when full or no more incoming, send to monitor.

dannable:
If you dig out a copy of SerialRelay from the internet then that shows a simple way of achieving what you probably want to do. It reads incoming Serial from device, appends to a character array, and when full or no more incoming, send to monitor.

Are you referring to this one?

https://gitorious.org/arduino-sketches/arduino-sketches/source/a014db892b694cd81662573bf64d2d1391b4ee78:simple_relay02.ino

I really need some help with this, please. Who can I pay, to write me some code for this function?

For example:

#include <SoftwareSerial.h>

SoftwareSerial GSMSerial (3,4);

#define GSMBUFFER_LENGTH 32
char GSMbuffer[GSMBUFFER_LENGTH];

int ReadAnswer (unsigned long timeout) {
  int i=0;
  unsigned long starttime = millis();
  char c;
  while (millis() - starttime < timeout) {
    if (GSMSerial.available()) {
      c=GSMSerial.read();
      if ((c == '\n') | (c == '\r')) {
        GSMbuffer[i]=0;
        return 1;
      }
      else if (i<GSMBUFFER_LENGTH) GSMbuffer[i++]=c;
    }
  }   
  return 0;
}

void setup () 
{
}
void loop() 
{
  GSMSerial.println("AT+CGATT?");
  if (ReadAnswer (3000)) {  // time out = 3 sec
    if (strcmp (GSMbuffer,"OK")==0) {
      // TO DO code ok
    }
    else {
      // TO DO code error
    }
  }
  else {
    // TO DO code time out
  }
  
}

RudiAhlers:
Are you referring to this one?

https://gitorious.org/arduino-sketches/arduino-sketches/source/a014db892b694cd81662573bf64d2d1391b4ee78:simple_relay02.ino

Not by the sound of the name.

There's a copy to be found in the link in my signature. It's a fairly simple program and its purpose is to send commands from serial monitor to device (in this case modem) and send any replies back to serial monitor. It does this by writing any incoming data from the device to a character array, and when it is either full or there is no more data to be read, send it to serial monitor.

Load it into the Arduino, open serial monitor, type in AT and you will get the reply AT OK.

Within the script if you keep monitoring the incoming character you can check for carriage returns and new line characters and act accordingly.

shev:
For example:

#include <SoftwareSerial.h>

SoftwareSerial GSMSerial (3,4);

#define GSMBUFFER_LENGTH 32
char GSMbuffer[GSMBUFFER_LENGTH];

int ReadAnswer (unsigned long timeout) {
 int i=0;
 unsigned long starttime = millis();
 char c;
 while (millis() - starttime < timeout) {
   if (GSMSerial.available()) {
     c=GSMSerial.read();
     if ((c == '\n') | (c == '\r')) {
       GSMbuffer[i]=0;
       return 1;
     }
     else if (i<GSMBUFFER_LENGTH) GSMbuffer[i++]=c;
   }
 }  
 return 0;
}

void setup ()
{
}
void loop()
{
 GSMSerial.println("AT+CGATT?");
 if (ReadAnswer (3000)) {  // time out = 3 sec
   if (strcmp (GSMbuffer,"OK")==0) {
     // TO DO code ok
   }
   else {
     // TO DO code error
   }
 }
 else {
   // TO DO code time out
 }
 
}

Hi,

Thanx for sample the code :slight_smile: :slight_smile:

This got me into the right direction, but I'm struggling a bit.

First I tried to get the IMEI number from the SIM900 module, as I want to store this into a DB - i.e. it's a unique identifier I could use to identify two different SIM900 modules.

The code I tried, was as follows:

void setup () 
{
  Serial.begin(19200);
  Serial2.begin(9600);
  Serial.println("GSM Test 01");
  Serial2.write("AT+GSN=?");
  if (ReadAnswer(500))
  {
    Serial.print("IMEI: ");
    Serial.println(GSMbuffer);
  }
}

And the Serial Monitor output:

GSM Test 01
IMEI: GATT?

So, the question is, how do I extract the returned output from the AT commands, in this case the IMEI number?
Later on I would like to extract the HTTP response from the website when I run the HTTP POST command.

The other issue I ran into, which I don't fully understand, is why would the " // TO DO code error" part run the whole time?
Let me explain, I put some Serial output at each of your "// TO DO ..." sections, to give me feedback of what's happening.
When it starts up, I get 3 "Signal OK" lines on the Serial Monitor, and then "Error" on every pass of the "loop()" code.
Surely if the module is enabled, it should stay enabled?

Here's the full code:

#define GSMBUFFER_LENGTH 500
char GSMbuffer[GSMBUFFER_LENGTH];

int ReadAnswer (unsigned long timeout) {
  int i=0;
  unsigned long starttime = millis();
  char c;
  while (millis() - starttime < timeout) {
    if (Serial2.available()) {
      c=Serial2.read();
      if ((c == '\n') | (c == '\r')) {
        GSMbuffer[i]=0;
        return 1;
      }
      else if (i<GSMBUFFER_LENGTH) GSMbuffer[i++]=c;
    }
  }   
  return 0;
}

void setup () 
{
  Serial.begin(19200);
  Serial2.begin(9600);
  Serial.println("GSM Test 01");
  Serial2.write("AT+GSN=?");
  if (ReadAnswer(500))
  {
    Serial.print("IMEI: ");
    Serial.println(GSMbuffer);
  }
}
void loop() 
{
  Serial2.println("AT+CGATT?");
  if (ReadAnswer (3000)) {  // time out = 3 sec
    if (strcmp (GSMbuffer,"OK")==0) {
      // TO DO code ok
      Serial.println("Signal OK");
      Serial.println(GSMbuffer);
    }
    else {
      // TO DO code error
    //Serial.println("Error");
    }
  }
  else {
    // TO DO code time out
    Serial.println("Timeout");
  }
  
}

I tried something new, with different code samples I got off the internet, but still don't get what I expect to be getting:

#include <String.h>

#define GSMBUFFER_LENGTH 500
char GSMbuffer[GSMBUFFER_LENGTH];


void setup()
{
  Serial2.begin(9600);               // the GPRS baud rate   
  Serial.begin(19200);    // the GPRS baud rate 
  delay(500);
  Serial.println(F("GSM Test with delays and no control over GSM AT command output."));
}

void loop()
{
  //after start up the program, you can using terminal to connect the serial of gprs shield,
  //if you input 't' in the terminal, the program will execute SendTextMessage(), it will show how to send a sms message,
  //if input 'd' in the terminal, it will execute DialVoiceCall(), etc.

  if (Serial.available())
    switch(Serial.read())
    {
    case 'h':
      SubmitHttpRequest();
      break;
    } 
  if (Serial2.available())
    Serial.write(Serial2.read());
}

///SubmitHttpRequest()
///this function is submit a http request
///attention:the time of delay is very important, it must be set enough 
void SubmitHttpRequest()
{
  Serial2.println("AT+GSN"); // Get IMEI number
  delay(500);
  Serial.print("IMEI Expected: ");
  ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too.


  Serial2.println("AT+CSQ"); // Test signal quality
  delay(200);
  Serial.println();
  Serial.print("AT+CSQ output expected: ");
  ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too.  


  Serial2.println("AT+CGATT=1");
  delay(500);
  Serial.println();
  Serial.print("AT+CGATT=1 output expected: ");
  ShowSerialData();
  if (ReadAnswer (1000)) {
    if (strcmp (GSMbuffer,"OK")==1) {
      Serial.println();
      Serial.println("GSMbuffer == 1");
    }
  }
}


void ShowSerialData()
{
  while(Serial2.available()!=0)
    Serial.write(Serial2.read());
} // END ShowSerialData()


int ReadAnswer (unsigned long timeout) {
  int i=0;
  unsigned long starttime = millis();
  char c;
  while (millis() - starttime < timeout) {
    if (Serial2.available()) {
      c=Serial2.read();
      if ((c == '\n') | (c == '\r')) {
        GSMbuffer[i]=0;
        return 1;
      }
      else if (i<GSMBUFFER_LENGTH) GSMbuffer[i++]=c;
    }
  }   
  return 0;
} // END ReadAnswer

Serial Monitor output:

GSM Test with delays and no control over GSM AT command output.
IMEI Expected: AT+GSN

000000000000000000

OK

AT+CSQ output expected: AT+CSQ

+CSQ: 17,0

OK

AT+CGATT=1 output expected: AT+CGATT=1

OK

I would have expected to get a "GSMbuffer == 1" but didn't

this code was for example, what would you understand how to handle responses from SIM900.
I have not checked. The SIM900 have difficult answers, instead of answering can come service message. All these answers have correctly handle.
The next code fully working and tested with SIM900
If you want to get the full code for your task, send me email

#include <SoftwareSerial.h>

SoftwareSerial GSMSerial (2,3); //GSM
#define PIN_GSM_PWR 6 //PIN GSM POWER

#define GSMBUFFER_LENGTH 32
char GSMbuffer[GSMBUFFER_LENGTH];

void setup () 
{
  pinMode (PIN_GSM_PWR, OUTPUT);
  digitalWrite(PIN_GSM_PWR, LOW);
  GSMSerial.begin(4800);
  Serial.begin(9600); //monitor
}

void SendQuery (char * str) {
  while (GSMSerial.available()) GSMSerial.read(); //clear GSM buffer
  GSMSerial.println(str);
  //output monitor
  Serial.print ("GSM > ");
  Serial.println (str);
}

int ReadAnswer (unsigned long timeout) {
  int i=0;
  unsigned long starttime = millis();
  char c;
  while (millis() - starttime < timeout) {
    if (GSMSerial.available()) {
      c=GSMSerial.read();
      if (c == '\r') continue;
      if (c == '\n') {
        if (i ==0) continue;  //free line
        GSMbuffer[i]=0;
         //output monitor
        Serial.print ("GSM < ");
        Serial.println (GSMbuffer);
        return 1;
      }
      else if (i<GSMBUFFER_LENGTH) GSMbuffer[i++]=c;
    }
  }   
  return 0;
}




void GSMHandler() {
  int gsmon =0;
  int noerror;
  int regok=0;
  int retry;
  char GSMserialnumber[20];  
  
  //Enable GSM module
  Serial.println ("Go GSM handler...");
  SendQuery ("ATE0");  //echo off
  noerror=ReadAnswer (1000);
  if (!noerror) { //time out 
    Serial.println ("GSM OFF");
    Serial.println ("GSM POWER ON...");
    digitalWrite(PIN_GSM_PWR, HIGH);
    delay(1000);
    digitalWrite(PIN_GSM_PWR, LOW); 
    delay(1000);   
    SendQuery ("ATE0");  
    noerror=ReadAnswer (1000);
  }
  retry=10;
  while (retry>0) {
    if (noerror) {
      if (strcmp (GSMbuffer,"OK")==0) {
        Serial.println ("GSM ON");
        gsmon=1;
        break;
      }
    }  
    delay(100); 
    SendQuery ("ATE0");  
    noerror=ReadAnswer (1000);
    retry--;
  }
  if (!gsmon) {
    Serial.println ("GSM disconnect, error");
    return;
  }
  
  //Disable error mode
  SendQuery ("AT+CMEE=0");  
  noerror=ReadAnswer (1000);
  if (noerror) {
    if (strcmp (GSMbuffer,"OK")!=0) {  //query is not OK
      noerror=0;
    }
  }  
  if (!noerror) {
    Serial.println ("GSM error");
    return;
  }
  
  //Serial number
  SendQuery ("AT+GSN");  
  noerror=ReadAnswer (1000);
  if (noerror) {
    strcpy (GSMserialnumber, GSMbuffer);
  }
  noerror=ReadAnswer (1000);
  if (noerror) {
    if (strcmp (GSMbuffer,"OK")!=0) {  //queri is not OK
      noerror=0;
    }
  }  
  if (!noerror) {
    Serial.println ("GSM error");
    return;
  }
  Serial.print ("GSM serial: ");
  Serial.println (GSMserialnumber);
  
  //GSM wait net registration  
  retry=30;  //wait 30 sec
  while (retry>0) {
    SendQuery ("AT+CREG?");  
    noerror=ReadAnswer (1000);
    if (noerror) {
      if (strcmp (GSMbuffer,"+CREG: 0,1")==0) {  
        regok=1;
      }
    }
    noerror=ReadAnswer (1000);
    if (strcmp (GSMbuffer,"OK")==0) {  //query is OK
      if (regok) break;
    }
    delay(1000); 
    retry--;
  }
  if (!regok) {
    Serial.println ("GSM net not registration, error");
    return;
  }
  Serial.print ("GSM net registred");
  
  // TO DO ............ you commands
  
}

void loop() 
{
  char c;
  if (Serial.available()) {
    c=Serial.read();
    switch (c) {
      case 'h':
        GSMHandler();
        break;
    };
  }
}

I haven't had much time to work on this till now...

So I'm trying to send the IMEI number to the web server, but for some odd reason it gets "converted" but I don't know why.

Here's the serial console output:

Go GSM handler...
GSM > ATE0
GSM < OK
GSM ON
GSM > AT+CMEE=0
GSM < OK
GSM > AT+GSN
GSM < 013227000048516
GSM < OK
GSM serial: 013227000048516
GSM > AT+CREG?
GSM < +CREG: 0,1
GSM < OK
GSM net registred
IMEI :
IMEI2 : 013227000048516
AT+HTTPPARA="URL","http://www.rudiahlers.co.za\log.php?imei=1086

Here's the code:

#include <SD.h>

#define PIN_GSM_PWR 46 //PIN GSM POWER
#define GSMBUFFER_LENGTH 32
char GSMbuffer[GSMBUFFER_LENGTH];
char Serial2number[16];
char IMEI_no[16];


int gsmon =0;
int noerror;
int regok=0;
int retry;

const int CS_Pin = 4; // GBoard Pro use Pin 4 for CS
char IMEI_File[ ]="datalog3.txt"; // File to write to
File IMEI_Data;
//char IMEI_no[] = "";

void setup () 
{
  pinMode (PIN_GSM_PWR, OUTPUT);
  digitalWrite(PIN_GSM_PWR, LOW);
  Serial2.begin(4800);
  Serial.begin(115200); //monitor
  Serial.println("GSM Test 03");

 Save_IMEI();
 

} // END setup()




void SendQuery (char * str) {
  while (Serial2.available()) Serial2.read(); //clear GSM buffer
  Serial2.println(str);
  //output monitor
  Serial.print ("GSM > ");
  Serial.println (str);
} // END void SendQuery (char * str)

int ReadAnswer (unsigned long timeout) {
  int i=0;
  unsigned long starttime = millis();
  char c;
  while (millis() - starttime < timeout) {
    if (Serial2.available()) {
      c=Serial2.read();
      if (c == '\r') continue;
      if (c == '\n') {
        if (i ==0) continue;  //free line
        GSMbuffer[i]=0;
         //output monitor
        Serial.print ("GSM < ");
        Serial.println (GSMbuffer);
        return 1;
      }
      else if (i<GSMBUFFER_LENGTH) GSMbuffer[i++]=c;
    }
  }   
  return 0;
} // END int ReadAnswer (unsigned long timeout)


void GSMHandler() {    
  
  //Enable GSM module
  Serial.println ("Go GSM handler...");
  SendQuery ("ATE0");  //echo off
  noerror=ReadAnswer (1000);
  if (!noerror) { //time out 
    Serial.println ("GSM OFF");
    Serial.println ("GSM POWER ON...");
    digitalWrite(PIN_GSM_PWR, HIGH);
    delay(1000);
    digitalWrite(PIN_GSM_PWR, LOW); 
    delay(1000);   
    SendQuery ("ATE0");  
    noerror=ReadAnswer (1000);
  }
  retry=10;
  while (retry>0) {
    if (noerror) {
      if (strcmp (GSMbuffer,"OK")==0) {
        Serial.println ("GSM ON");
        gsmon=1;
        break;
      }
    }  
    delay(100); 
    SendQuery ("ATE0");  
    noerror=ReadAnswer (1000);
    retry--;
  }
  if (!gsmon) {
    Serial.println ("GSM disconnect, error");
    return;
  }
  
  //Disable error mode
  SendQuery ("AT+CMEE=0");  
  noerror=ReadAnswer (1000);
  if (noerror) {
    if (strcmp (GSMbuffer,"OK")!=0) {  //query is not OK
      noerror=0;
    }
  }  
  if (!noerror) {
    Serial.println ("GSM error");
    return;
  }
  
  //Serial number
  SendQuery ("AT+GSN");  
  noerror=ReadAnswer (1000);
  if (noerror) {
    strcpy (Serial2number, GSMbuffer);
  }
  noerror=ReadAnswer (1000);
  if (noerror) {
    if (strcmp (GSMbuffer,"OK")!=0) {  //queri is not OK
      noerror=0;
    }
  }  
  if (!noerror) {
    Serial.println ("GSM error");
    return;
  }
  Serial.print ("GSM serial: ");
  Serial.println (Serial2number);
  
  //GSM wait net registration  
  retry=30;  //wait 30 sec
  while (retry>0) {
    SendQuery ("AT+CREG?");  
    noerror=ReadAnswer (1000);
    if (noerror) {
      if (strcmp (GSMbuffer,"+CREG: 0,1")==0) {  
        regok=1;
      }
    }
    noerror=ReadAnswer (1000);
    if (strcmp (GSMbuffer,"OK")==0) {  //query is OK
      if (regok) break;
    }
    delay(1000); 
    retry--;
  }
  if (!regok) {
    Serial.println ("GSM net not registration, error");
    return;
  }
  Serial.println ("GSM net registred");
  
  char log_data[100];
  Serial.print("IMEI : ");
  Serial.println(IMEI_no);
  Serial.print("IMEI2 : ");
  Serial.println(Serial2number);
  
 sprintf(log_data, "AT+HTTPPARA=\"URL\",\"http://www.aaa.co.za\\log.php?imei=%d", Serial2number);

  Serial2.println("AT+HTTPINIT"); 
 Serial2.println(log_data);
  Serial2.println("AT+HTTPACTION=1");
  Serial2.println("AT+HTTPREAD");


 Serial.println(log_data);

  // TO DO ............ you commands
  
} // END void GSMHandler() 



void loop() 
{
  char c;
  if (Serial.available()) {
    c=Serial.read();
    switch (c) {
      case 'h':
        GSMHandler();
        break;
    };
  }
  

} // END loop()
 
void Save_IMEI()
{
//  Serial.println("Initialize SD Card.");
  pinMode(CS_Pin, OUTPUT);

  if (!SD.begin(CS_Pin)) // Test if SD Card initialize 
  {
    Serial.println("SD Card missing, or corrupt.");
    return;
  } // END if (!SD.begin(CS_Pin))
 // Serial.println("SD Card initialized.");
  IMEI_Data = SD.open(IMEI_File);

  if (IMEI_Data.available()) // Test if IMEI_File is available
  {
    Serial.print(IMEI_File);
    Serial.println(" available. Content below: ");
    Serial.println();

    while (IMEI_Data.available()) 
    {
      Serial.write(IMEI_Data.read()); // Read contents of file
//   char   IMEI_no = IMEI_Data.read();
      
    } // END while (IMEI_Date.available())
    IMEI_Data.close(); // Close file
  } 
  else 
  {
    Serial.print(IMEI_File);
    Serial.println(" not available. Creating and adding content...");

    IMEI_Data = SD.open(IMEI_File, FILE_WRITE); // Open file for writing
    if (IMEI_Data)
    {

  SendQuery ("ATE0");  //echo off
  noerror=ReadAnswer (1000);
  if (!noerror) { //time out 
    digitalWrite(PIN_GSM_PWR, HIGH);
   delay(400);
    SendQuery ("ATE0");  
    noerror=ReadAnswer (1000);
  }
  retry=10;
  while (retry>0) {
    if (noerror) {
      if (strcmp (GSMbuffer,"OK")==0) {
 //       Serial.println ("GSM ON");
        gsmon=1;
        break;
      }
    }  
    delay(100); 
    SendQuery ("ATE0");  
    noerror=ReadAnswer (1000);
    retry--;
  }
  if (!gsmon) {
    Serial.println ("GSM disconnect, error");
    return;
  } 
  SendQuery ("AT+CMEE=0");    //Disable error mode
  noerror=ReadAnswer (1000);
  if (noerror) {
    if (strcmp (GSMbuffer,"OK")!=0) {  //query is not OK
      noerror=0;
    }
  }  
  if (!noerror) {
    Serial.println ("GSM error");
    return;
  }
  
  SendQuery ("AT+GSN");    // Get Serial number
  noerror=ReadAnswer (1000);
  if (noerror) {
    strcpy (Serial2number, GSMbuffer);
    Serial.print("IMEI No: ");
    Serial.println(GSMbuffer);
  }
  noerror=ReadAnswer (1000);
  if (noerror) {
    if (strcmp (GSMbuffer,"OK")!=0) {  // query is not OK
      noerror=0;
    }
  }  
  if (!noerror) {
    Serial.println ("GSM error");
    return;
  }
    
//  Serial.print ("GSM serial: ");
//  Serial.println (Serial2number);

    Serial.print("Writing data to ");
      Serial.print(IMEI_Data);
      Serial.println(" ...");
      IMEI_Data.println(Serial2number); // Writing data to file
      IMEI_Data.close(); // Close file when done.
      Serial.println("Done.");
      strcpy (IMEI_no, Serial2number);
//      IMEI_no = Serial2number;
    } // END if (IMEI_File)
  } // END if (SD.available(IMEI_File))

} // END void Save_IMEI()

Can anyone help me with this, please?

Why is the IMEI number being converted?