Now, I know that this piece of code doesn't work and I know that the problem is due to the conversion from the string to byte. But how can I fi this?

PS: Feel free to improve this piece of code because I know it sucks (full of variables ). XD

Does the string have any end of packet marker? New line? carriage return?

Get rid of the String object and use null-terminated char arrays. A finite state machine should be used to keep track of where in the string you are.

Psuedo code:If serial information available{ read character if character is a colon or end-of-packet marker { null terminate the buffer convert string to number (atoi()) store number in EEPROM reset the buffer } else { store character in buffer }}

I keep appending chars to a global variable called command and when I receive a CR I process the command. I didn't used char arrays because I find them harder to handle but I'm willing to give it a try. Can you show me some example? Can I use string functions like substring with chars?

I keep appending chars to a global variable called command and when I receive a CR I process the command.

Instead, you should be appending characters until you receive a carriage return OR colon.

Quote

I didn't used char arrays because I find them harder to handle but I'm willing to give it a try.

They are slightly harder to handle when you don't know much experience with C, but microcontrollers don't have near the amount of memory available that the microprocessors do, so handling data intensive items, such as String objects, is not recommended. There is also a known bug with the String object in the Arduino environment that can cause memory issues. Best practice is not to use it.

Quote

Can you show me some example?

Simple example that will read numbers from the serial line until it gets a new line and convert to an int:

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 delimiter4th - Save each of those numbers in the EEPROM. Any of them will always be a number ranging from 0 to 255

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.

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

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.

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.

Thanks for the tip related to the variable name. You are right and it is much more clearer that way

But the strncpy is in fact called. If you look closely you will see that it is the first thing called after I check if the minimum number of colons is present.

Anyway thanks for the help. I was using wrong index values and now everything seems to work perfectly

But now I have another question:

I have my code protected agains buffer overrun, but how can I validate my code against invalid input?

i.e:

Lets imagine that the user pass the following argument:

Quote

1:2:3:1:1:a:!:9

How can I parse the content and be sure I will only read numbers (preferably between 0 and 255)?

What I'm currently doing is using the atoi() function to parse the string, and everytime atoi() returns 0 I compare this value to the original string just to make sure that the "zero" wasn't actually the real value being passed (due to the fact that atoi() returns 0 everytime it wasn't able to parse the string into an int and this may be confused the a real zero).