Search code examples
arduinoserial-portgsmat-commandsim800l

How to read GSM AT command response on arduino Serial port for long response?


I have Arduino UNO and a Sim800L module and I know the way to read the serial, it is something like this question, but when I perform this function :

    String GetRegData()
    {
      Serial.println("Get nearby antenna info ...");
      SIM800L.print("AT+CNETSCAN=1\r");
      delay(1000);

      SIM800L.print("AT+CNETSCAN\r"); 
      delay(1000);

      String buffer2;

      while (SIM800L.available())
       {
          char c = SIM800L.read();
          Serial.print(c);
          buffer2.concat(c);
          delay(10);
       }
       //Serial.println();
       return buffer2;
    }

The output is :

AT+CMGF=1

OK
AT+CNMI=2,2,0,0,0

OK
AT+CNETSCAN=1

OK
AT+CNETSCAN

Operator:"XXXX",MCC:XXX,MNC:XX,Rxlev:XX,Cellid:XX,Arfcn:XX,Lac:q5er32xlAair32xlAacrseifcca,Nvdc00

This AT command (AT+CNETSCAN) should scan all nearby antenna and print some information in multiple lines(according to datasheet), when I execute this command manually on this devices(SIM800L) ,I get multiple line such :

Operator:"XXXX",MCC:XXX,MNC:XX,Rxlev:XX,Cellid:XX,Arfcn:XX,Lac:XX,Bsic:XX
Operator:"XXXX",MCC:XXX,MNC:XX,Rxlev:XX,Cellid:XX,Arfcn:XX,Lac:XX,Bsic:XX
Operator:"XXXX",MCC:XXX,MNC:XX,Rxlev:XX,Cellid:XX,Arfcn:XX,Lac:XX,Bsic:XX
OK

But I don't know what happen when I do it programmatically, It is messed up, I tried to change baud rate and change receiving method and read char by char and I did put delaying between receives and I tried to do if or for instead of while but no luck.

I guess there is a delay between receiving each line and it breaks the serial availability but I don't know what to do ! any help would be appreciated.

By the way, my setup function:

void setup() {
  SIM800L.begin(9600);
  Serial.begin(9600); 
  delay(3000);

  SIM800L.print("AT+CMGF=1\r"); 
  delay(100);

  SIM800L.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
  GetRegData();

  delay(1000);
}

Ps: The whole thing(Arduino+Sim800L) works perfectly fine with no error and the Simcard is unlocked and it can send an receive SMS and Calls ,etc.


Solution

  • By spending hours of thinking the whole process through, I came up with an idea:

    Be at Serial port for 10 second and get what ever it spit out! NO Serial port breaking out NO while (SIM800L.available() != 0)

    The elapsedMillis is the library and this is the code:

    String buffer2;
    
    elapsedMillis timeElapsed;
    unsigned int interval = 10000; 
    
    while(timeElapsed < interval){
        if (SIM800L.available() != 0){ // for ignoring the NULLs
            char c = SIM800L.read();
            Serial.print(c);
            buffer2.concat(c);
          }
    }
    

    Although there are some highlights in this process :

    1- The main reason for NOT getting the whole output by the old loop is , the SIM800L.available() became Zero during multiple line receiving!

    2- The String data type or SIM800L.read(); Do NOT do anything wrong and they, theoretically have no limitation of reading device output(Like 64 byte ) characters , They read it all

    3- Some times electronic devices act strangely special Sim800L which is old and cheap!

    4- If you will to send this data(Network Antenna Scan) via SMS, be aware of SMS max character limit and consider that Service Providers will charge you for about every 150 char that send! and remove \r\n chars from response, and run AT+CSMP=17,167,0,0 before sending SMS on SIM800L otherwise it will send a blank SMS!

    5- Sim800L Datasheet said it work best on port 115200 BUT it is too fast for this device! set its baud rate on 9600.