Hi everyone,
I am programmed a parser for my Arduino Uno and beside an odd problem it is working fine.
With strtok I split a char but in some cases, I don't know why, the last token it extractes is damaged at the end. When printing it it shows that some characters of the string were replaced by a varying number of special characters. It looks like this:
WAT=OFFPER1=10200PER2=9800PI1=10PI2=10PTT=2~LIGHT RAT=30 COL=255,255,0~BLACK RAT=20~LIGHT RAT=30 COLB=255,255,0 COLT=255,255,0~BLACK RAT=20END
LIGHT RAT=30 COL=255,255,0
BLACK RAT=20
LIGHT RAT=30 COLB=255,255,0 COLT=255,255,0
BLACK RAT=2h⸮⸮⸮
The first line is the total string. The 4 bottom lines are the tokens. The last one actually has more weired symbols but it could only copy these four...
The backgrund is the following:
I use #define to at the begining of my code to store information about different modes of the device I am building.
#define DEFINITIONS_MODE_1 "\
WAT=OFF\
PER1=10200\
PER2=9800\
PI1=10\
PI2=10\
PTT=2\
~LIGHT\
RAT=30\
COL=255,255,0\
~BLACK\
RAT=20\
~LIGHT\
RAT=30\
COLB=255,255,0\
COLT=255,255,0\
~BLACK\
RAT=20\
"
Then I pass it to the parser function:
read_definitions(PSTR(DEFINITIONS_MODE_1));
what as far as I understand stores it in Flash and saves ram (what it definitly does).
In the parser the const char* is then copied to a char* in ram and is splitted by strtok.
void read_definitions(const char* str_flash) {
char str_ram[strlen_P(*str_flash)];
strcpy_P(str_ram, str_flash);
// str_ram[strlen(str_ram)-1] = NULL;
Serial.println(str_ram);
char* ptr = strtok(str_ram, "~");
char* mode_definitions = ptr;
char* wave_definitions[MAX_NUM_WAVES];
byte wave_counter = 0;
while((ptr = strtok(NULL, "~")) != NULL) {
wave_definitions[wave_counter] = ptr;
Serial.println(ptr);
wave_counter++;
}
As you see above it splits them fine but the end is damaged... it does not appear on other definitions and the error is reproducable as long as I dont change anything, but if I change a definition or add another one. I know this is probably not a beautiful way to program this but I have bad experiences with reading from PROGMEM either.
Thanks ahead!