0

I have a string "Revision: BG96MAR02A07M1G" which is stored in char tempdata[512]

I'm trying to parse and copy only that part BG96MAR02A07M1G

this what I did, but I get garbage values in firmwareVersion

signed char updateBG96FirmwareVersion(int timeout)
{
    char firmwareVersion[24];
    char *ret = NULL;

if (GSMCommand("I", tempdata, timeout) != AT_OK)
        return FEHLER;

    ret = strstr(tempdata, "Revision:");
    if (ret)
    {
        strncpy(firmwareVersion, tempdata+9, 24);
        firmwareVersion[sizeof(firmwareVersion)] = '\0';
    }
    else
        return FEHLER;
}

test

John
  • 29
  • 4

1 Answers1

0

You can just do this:

#include <string.h>

#define FEHLER 123   // ?
#define SUCCESS 0

signed char updateBG96FirmwareVersion(int timeout)
{
  char firmwareVersion[24];
  char* ret = NULL;

#if 1
  char tempdata[] = "Revision: BG96MAR02A07M1G";  // Test data for my test
#else
  if (GSMCommand("I", tempdata, timeout) != AT_OK)
    return FEHLER;
#endif

  const char revision[] = "Revision: ";
  ret = strstr(tempdata, revision);
  if (ret)
  {
    strcpy(firmwareVersion, tempdata + strlen(revision));
    return SUCCESS;
  }
  else
    return FEHLER;
}

int main() // main function initializing here 
{
  updateBG96FirmwareVersion(0);
}

This assumes that the revision string BG96MAR02A07M1G is never longer than 24 characters. Otherways you probably should use strncpy and terminate the string with a 0 manually.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • BTW: `updateBG96FirmwareVersion` doesn't return anything in case of success. I edited the answer slightly – Jabberwocky Sep 08 '21 at 14:00
  • that's weird, without return SUCCESS; it corrupts the data – John Sep 08 '21 at 14:15
  • Maybe the code that calls `updateBG96FirmwareVersion` does something weird if un unexpected value is returned. Read this: https://stackoverflow.com/questions/6638963/checking-return-value-of-a-function-without-return-statement – Jabberwocky Sep 08 '21 at 14:18
  • @andreahmed the version without the `return SUCCESS` should compile with a warning: https://www.godbolt.org/z/3WhTYP8av – Jabberwocky Sep 08 '21 at 14:21