Go Down

Topic: Read String from Serial Input, parse it, and write it to the EEPROM. (Read 1 time) previous topic - next topic

PaulS

Code: [Select]
    inByte[0] = '\0';
  }
  else
  {
    inByte[i++];  // concatanate the character
    inByte[i] = '\0'; // null terminate the array
  }

should be
Code: [Select]
    buffer[0] = '\0';
  }
  else
  {
    buffer[i++];  // concatanate the character
    buffer[i] = '\0'; // null terminate the array
  }


Is 6 characters enough for the buffer?

overflow

Hello guys,

I've changed all my code to use char arrays instead of strings but I'm still struggling to parse and store data inside the EEPROM.

This is my current code:

Code: [Select]

void writeConfig()
{

  char *myCommand = "saveConfig 2:2:2:2:2:2:2:2";
  char *tmpBuff;
  char configuration[64];
  int comma=0;
 

  for(int i=0; i < strlen(myCommand); i++)
  {
    if( myCommand[i] == ':' ) comma++;
  }

  if ( (comma < 7)  || (strlen(myCommand) < 26) )
  {
    Serial.println("Invalid configuration.");
    return;
  }
   
  strncpy(configuration, (myCommand+11), sizeof(myCommand) - 11);
 
  tmpBuff = strtok(configuration, ":");
    int counter=0;
    while (tmpBuff != NULL)
  {
    EEPROM.write(counter, int(tmpBuff));
    tmpBuff = strtok (NULL, ":");
    counter++;
  }
 

  Serial.println("Configuration saved!");
}


Please ignore the lines below "strncpy(configuration, (myCommand+11), sizeof(myCommand) - 11);" because they werent tested yet. This line is crashing the arduino and it is reseting everytime it is executed and I don't know why!

All i'm trying to do is:

1st - Receive a string (I've hardcoded it for testing purposes -> char *myCommand = "saveConfig 2:2:2:2:2:2:2:2";)
2nd - Cut the head leaving just the configuration argument.
3rd - Split the configuration argument using the commas as delimiter
4th - Save each of those numbers in the EEPROM. Any of them will always be a number ranging from 0 to 255

How can i accomplish this?

PaulS

Quote
for(int i=0; i < strlen(myCommand); i++)
  {
    if( myCommand == ':' ) comma++;
  }

This is a comma --> ,
This is a colon --> :

See the difference?

Code: [Select]
    EEPROM.write(counter, int(tmpBuff));
The int() macro is for people that think (int)tmpBuff looks weird. Personally, I'd have told those people to get over it. The int() macro does NOT convert a char array to an int. The atoi() function does that. All that it does is cast the address of the token to an int, which is hardly the data that you want to be storing in the EEPROM.

overflow


Quote
for(int i=0; i < strlen(myCommand); i++)
  {
    if( myCommand == ':' ) comma++;
  }

This is a comma --> ,
This is a colon --> :

See the difference?

Code: [Select]
    EEPROM.write(counter, int(tmpBuff));
The int() macro is for people that think (int)tmpBuff looks weird. Personally, I'd have told those people to get over it. The int() macro does NOT convert a char array to an int. The atoi() function does that. All that it does is cast the address of the token to an int, which is hardly the data that you want to be storing in the EEPROM.


Hellom PaulS, thank you for your reply :)

About the first part, please bear with me. I was actually thinking in colon ( as you can see, I'm using commas in the hardcoded command and also when I'm searching for it) but switched the names (I'm Portuguese).  ;)

I've also corrected the problem related to the cast but that still doesn't fixe the original issue.

Everytime the strncpy is executed my arduino is resetedand I have no idea why :(

PaulS

Quote
Everytime the strncpy is executed my arduino is resetedand I have no idea why

Nor do I, since the code you posted doesn't call strncpy(). The usual culprit, though, is copying outside the bounds of the destination array. Copy 12 bytes into a 2 element array is not a good thing. Nor is copying to a location pointed to be a non-initialized pointer.

The comma variable should be named delimterCounter, so that it doesn't matter what the delimiter is.

Go Up