Pages: [1]   Go Down
Author Topic: Need help to splitt a string that comes from serieport  (Read 93 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
<BS>0<CR>
<BS><LF>
Sams Bar<CR>
<BS><LF>
<LF>
Liza
<CR>
<BS><LF>
<LF>
<LF>
Liza Sam<BS><LF>
<LF>
<LF>
<LF>
0,00<CR>
<BS><LF>
<LF>
<LF>
<LF>
<LF>
0<CR>

or
<BS><LF>
<LF>
<LF>
<LF>
1,00<CR>

or
<BS><LF>
<LF>
<LF>
<LF>
<LF>
<LF>
1<CR>

//End of string


<BS> = Hex 08
<LF> = Hex 0A
<CR> = 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??
Logged

Ontario
Offline Offline
God Member
*****
Karma: 25
Posts: 881
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 <BS><CR><LF> ("\b\r\n") as delimiters, then look at the strings found.  The ones that aren't "" are what you are looking for.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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,
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 99
Posts: 3605
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Example.
Code:
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.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Tesla Member
***
Karma: 124
Posts: 7105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Code:
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 <CR> 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
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 99
Posts: 3605
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so you can have this then.

Try this.

Code:
// 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 &&

Quote
It looks like <CR> marks the end of each sequence
If that is true, then it will make the code a little easier.

Code:
// 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.
« Last Edit: August 20, 2014, 02:43:07 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HazardsMind
I try this. Thanks for the help.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 99
Posts: 3605
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read the bottom note.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Pages: [1]   Go Up
Jump to: