Parsin a serial string

Although I consider myself a competent programmer in Real Studio, I’m the first to admit I’m a terrible C coder. One of the reasons I use Real Studio is because I don’t have to worry about the complexities of strings/character arrays, etc.

I need some help. I’m trying to control my arduino via messages sent over serial. The messages are in the format:

<Header(@)><3 letter command id><footer (!)>

E.g. to turn on digital pin 2 I’d send (SDP = Set Digital Pin):


I haven’t got access to my actual arduino sketch at the moment (am at work) but in the SerialEvent() function I had been concatenating the incoming bytes into a string and setting a boolean flag when it’s all received. The main loop just keeps checking if this flag is set. If it is, it calls the function handleCommand().

My problem is that I don’t know how to take the command string (e.g SDP2,1) and split it into one string called ID that would contain the three letter code (in this case SDP) and an array of integers containing the passed data (in this case 2 and 1). The number of integers sent may be anything from 0 to about 10).

Could anyone help me out please?




Are there always 2 integers or a variable amount of integers ?
Is the code "SDP" a limited number of strings you have act on, or can it be anything ?

sscanf can be used to extract data from a string.
Or using strchr for the the string (here "SDP") and using strchr (which searches for a ',') and atoi for the integers.

While writing this, bubulindo said "strtok()??". That's also possible.
So check the functions mentioned here, and see if you feel comfortable with one of the solutions.

SDP comes from a limited set of 3 character codes. The number of integers is variable (may be as few as 0 or as many as 10).

I'll have a look at this strtok() function.