Need help to splitt a string that comes from serieport

Hi,
I have a problem to splitt a string that I received from the serieport.
Can someone help me with some code how I shall split the string?

The string I get:
0

Sams Bar


Liza




Liza Sam



0,00





0

or




1,00

or






1

//End of string

= Hex 08
= Hex 0A
= Hex 0D

It is the values in red that I want to place in seperate string so I can use it later in the program so I can send it to a display.

Anyone that can show me a code example??

Where does the string come from? If your reading it character-by-character from a stream, I would say your best bet s to parse it on the fly. If you have to parse the string in memory, have a look at strtok()

http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html#gac0dbc25e8b202114031a4aa2a7c5177b

You can google for zillions of examples. Your code would give ("\b\r\n") as delimiters, then look at the strings found. The ones that aren't "" are what you are looking for.

The string comes throu the usb-port from a computer.
So I can look for one letter at the time.
The porblem that I have not solved is how I shall know how many LF's I got and then get the letters after that,

You can have the code discard those delimiters. it should be as simple as

Example.

char c = Serial.read();
if( c != '\b' || c != '\r' || c != '\n') // check if incoming char is NOT any of the 3 delimiters
{
  // not a delimiter, store the char in an array
}

Now if there is a distinct pattern to the delimiters as to what comes in first and last, then you can have the code split the chars into there own index in the array, instead of them all together.

That only check what letter that I received.
There are no pattern how the string is received so I need to count the LFs and after I count them I need to store that parameter.
Then do a new count on LFs for store in another parameter. Thats my problem, hox to count and then store the letters after counting.

HazardsMind:

char c = Serial.read();

if( c != '\b' || c != '\r' || c != '\n') // check if incoming char is NOT any of the 3 delimiters
{
  // not a delimiter, store the char in an array
}

I think this test need && rather than || in each case.

It looks like marks the end of each sequence so I suggest you use that as an end-marker and save everything up to that in a char array and then parse the array for the piece you want. The code in Chapter 8 of this Demo shows how to use an end-marker.

...R

Ok, so you can have this then.

Try this.

// a the top of the code, above setup()
char data[10][10]; // declared globally
byte i = 0, j = 0, idx = 0;

//within the loop()
char c = Serial.read();
if( c != '\b' || c != '\r' || c != '\n') // check if incoming char is NOT any of the 3 delimiters
{
  data[i] [j]= c;
  j++;
}
else if(c == '\n') // if the incoming char is '\n', start a counter until another(different) delimiter is found
{
  j = 0;
  idx++;
}
else if(c == '\b' || c == '\r') // if the char is '\b' or '\r', then change the array index based on the counter
{
  i = idx;
  idx=0;
}

@Robin, I was thinking that too, but I can't test it on my end to be sure. If it doesn't work as it is now, then he can change it to use &&

It looks like marks the end of each sequence

If that is true, then it will make the code a little easier.

// a the top of the code, above setup()
char data[10][10]; // declared globally
byte i = 0, j = 0;

//within the loop()
char c = Serial.read();
if( c != '\b' || c != '\r' || c != '\n') // check if incoming char is NOT any of the 3 delimiters
{
  data[i] [j]= c;
  j++;
}
else if(c == '\r') // if the incoming char is '\r', increment the index 
{
  j = 0;
  i++;
}

Note: nothing in the string indicates when the code is getting new strings to split, so i++ will continue to increment = not good.

HazardsMind
I try this. Thanks for the help.

Read the bottom note.