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!!
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.
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.
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...
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
,,;
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;
,;
jegesd:
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]