stanleyhuangyc / Freematics

Official source code repository for Freematics
https://freematics.com
418 stars 345 forks source link

SIM800::getSignalQuality always level -114 #15

Open AlexAldoshin opened 8 years ago

AlexAldoshin commented 8 years ago

function getSignalQuality always returns the value -114, although a direct challenge "AT + CSQ" works correctly

mikebolt commented 7 years ago

I think I have figured out the issue. Here's the code for getSignalQuality:

int CGPRS_SIM800::getSignalQuality()
{
  sendCommand("AT+CSQ");
  char *p = strstr(buffer, "CSQ: ");
  if (p) {
    int n = atoi(p + 2);
    if (n == 99 || n == -1) return 0;
    return n * 2 - 114;
  } else {
   return 0;
  }
}

The only way that this function can return -114 is if p is nonzero and n is zero. This is probably happening because atoi is returning 0 because it cannot parse the number. It looks like it adds 2 to the value of p in order to skip the first field in the CSQ. However, this means that p will point to e.g. "Q:14,0" and that won't parse. Simply changing the p + 2 to p + 4 might fix the problem.

For reference: atoi, strstr, AT+CSQ, AT Commands.