Pages: [1]   Go Down
Author Topic: Parsin a serial string  (Read 755 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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><comma separated data><footer (!)>

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

@SDP2,1!

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?

Thanks,

FieldDoc
Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3287
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

strtok()??

Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: