Parse Serial.read() into Strings

Hello,

I'm actualy developing a gaming simulator and I need to parse a Serial.read() into diferents Strings between a ','

The data is send like this "00,0000,0000,0000" from a C++ program in my PC and i need to separate first 00 into char 1; second 0000 into char 2 etc...

Ive searched and I didnt found any tutorial that works in my project.

Could anyone help?

Thanks!!

No sé si prefieres que te hable en español o inglés, pero tienes dos opciones:

  • Llamar repetidamente a parseInt().
  • Guardar todo en un vector de char para así separar los datos mediante strtok.

I'm actualy developing a gaming simulator and I need to parse a Serial.read() into diferents Strings between a ','

No, you don't. You should know that maximum length of string that you will send, and create a char array large enough to hold that string. Note the use of a lower case s. A string and a String are NOT the same thing.

Once you have a string (a NULL terminated array of chars), you can use strtok() to parse it.

You can look at GitHub - WestfW/parser: Simple serial command line parser for Arduino/etc
It does more than you want (line editing, keywords), but it also does numbers as well, the it has both blocking and non-blocking "assemble serial characters into a buffer" code that is probably generally useful.

If the 00 or 0000 ar actually representing integral values like 32 or 4557 (comma separated) then you can just build thos on the fly every time you receive an input until you get the comma. No need for a char array/buffer

If you really need Strings (or better c-strings) then indeed you need a small buffer large enough for the longest single input and add into the buffer until a comma comes in (or may be any other terminating character like a new line or a timeout depending on when the comma gets sent)

Have a look at the parse example in Serial Input Basics

...R