cristiansteib / Sim800l

Library sim800l for Arduino UNO (maybe sim900l work)
226 stars 79 forks source link

Update crash prevention in the function _readSerial() #32

Closed AyushMarsian closed 4 years ago

AyushMarsian commented 4 years ago

Hello,

I face one issue in _readSerial() function, and given solution below.

In detail, the function's return data type is String, you are returning data when available from serial port. But when GSM not working or not connected with serial port at that time function return nothing. We do not get compilation error, but when we call function for to perform certain String operation at that time controller get hang or crash.

Old function

String Sim800l::_readSerial()
{
   _timeout=0;
   while  (!SIM.available() && _timeout < 12000  )
   {
         delay(13);
         _timeout++;
   }
   if (SIM.available())
   {
        return SIM.readString();
   }
} 

New function(Added one line)

String Sim800l::_readSerial()
{
   _timeout=0;
   while  (!SIM.available() && _timeout < 12000  )
   {
         delay(13);
         _timeout++;
   }
   if (SIM.available())
   {
        return SIM.readString();
   }
   return "timeOut";
} 

I personally tested so, you can update and commit

cristiansteib commented 4 years ago

@AyushMarsian Thanks for your collaboration. For timeout operations use millis() or micros()

unsigned long start = millis();
while (!SIM.available() && millis()-start < TIMEOUT){
  // KEEP WAITING,
};

Improve the timeout and do the Pull Request.

regards.