Go Down

Topic: How can I Split a string ? (Read 372 times) previous topic - next topic

theavocoder

Jun 18, 2018, 03:50 pm Last Edit: Jun 18, 2018, 04:05 pm by AWOL
I am trying to split a string.
I am building a small windows application, controlling the Arduino Robot.
I have to send data from the PC to the Robot by Serial.
data will sent like, for example :
A,90,R; A -> Angle; 90 -> the angle; R -> to the right
or S,255,0; S -> Speed; 255 -> the speed
or D,50,0; D -> Distance; 50 -> distance to walk ...

I have read something about the sscanf, and substring, but it wasn't enough!!

Moderator edit: email removed

septillion

I would just do it on the fly. The format of your commands (especially ending (resetting) with a unique character is good.

For example, if the first character was a 'A', remember you're setting an angle. Second char is a ',', fine, continue, third is a digit, fine, save it. It gets another digit, fine, multiply the already saved part by 10 and add the new one. etc. And if it receives a ';', just reset it.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

groundFungus

The Serial input basics thread may have information of use.

It is unwise to publish your email address on this public forum.  You should edit your OP to remove your email address.

ardly

It is a bad idea to post your email address on the internet.

You need to post the code you have tried, saying "it wasn't enough" is not helpful.
Put the code within the </> tags.

You need to clearly define the format of all the possible messages you might send as that will strongly affect your program structure.

Then basically you need to parse the text of individual meaasges looking for the commas that are used as separators and extracting and decoding the parameters beween the separators.
"Facts do not cease to exist because they are ignored" - Aldous Huxley

jremington

#4
Jun 18, 2018, 05:01 pm Last Edit: Jun 18, 2018, 05:03 pm by jremington
To parse "A,90,R; A -> Angle; 90 -> the angle; R -> to the right", use C-strings, and the function strtok(). Another strtok() tutorial.

I would use the function atoi() to convert the number string to an integer.

jegesd

Why not to try to read a first char with charAt() and then based on it result is A do this, if S do that

 e.g. if A read the second and third and that is the angle (in this case angle always must be 2 chars) then read 4 th char and it is a direction etc...

septillion

Because .charAt() is a String function....
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

ardly

Why not to try to read a first char with charAt() and then based on it result is A do this, if S do that

 e.g. if A read the second and third and that is the angle (in this case angle always must be 2 chars) then read 4 th char and it is a direction etc...
The OP should think clearly about the format he is inventing to make it easy to code.

For example if in the Angle example he used a numeric field to indicate left or right then all the messages would have the format
<character>,<integer>,<integer>;

He could parse out the parameters then branch depending on the character i.e. less code in fewer places.

I am not even sure though why he has three parameters in the Speed and Distance Messages?
Also why not just use 0...360 for the angle rather than introducing Left and Right?
The messages could probably be simplified to all just be;
<character>,<integer>;
"Facts do not cease to exist because they are ignored" - Aldous Huxley

jegesd

I agree charAt() is string function but you can create a new string which represent e.g. angle by adding two chars.. something like this

char A[2];
A[0]=0;
strcat(A, str.charAt(1)); //str is a string you received...
strcat(A, str.charAt(2));

and after this A should be 90...

do not say it work like this 100% (did not tried so can not state) but just to give an idea...


septillion

#9
Jun 20, 2018, 09:58 am Last Edit: Jun 20, 2018, 10:00 am by septillion
strcat(A, str.charAt(1)); //str is a string you received...
WRONG! str is a String object of the string you received ;)

Mixing cstrings and Strings is even a bigger mess... Especially if you do it wrong. strcat() expects two cstrings (aka char pointers) but .charAt() returns a char... And even if it would work, A need to have size 3 in order to be able to contain the string "90" (0x39 0x30 0x00)

If str was just a cstring, you could just get the second character with str[1] ;)
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

jegesd

well septillion you were right.

this one should work

  String s2 = "A90";
  String test = s2.substring(0,1);
  String rez2 = s2.substring(1,3);
 

https://www.arduino.cc/en/Tutorial/StringSubstring

septillion

Or, without stupid String:

Code: [Select]
char input[] = "A90";
char left = input[0];
char *right = &input[1];


Alright, I cheated a bit :D But that's why I would just do the parsing on the fly as the serial comes in rather then first collect it all.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

jegesd


Go Up