Parse serial input strings

Hello everyone,

I'd like to control my Arduino via serial interface.
Therefore the controller should parse some instructions e.g.

SET LED 1 ON
SET LED 2 OFF
GET VERSION
GET LED 1 STATUS

Is there an algorithm available, which parse each parameter:
Parameter 1: SET
Parameter 2: LED
Parameter 3: 1
Parameter 4: ON

BR,
Julian

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

There is no added value in sending a message like SET LED 1 ON. Just send <N,1> meaning oN led 1 and similarly short messages for the other actions. I would send <V,0> if I wanted the version - the 0 can be ignored but it will make the parsing easier.

...R

Robin2:
There is no added value in sending a message like SET LED 1 ON. Just send <N,1> meaning oN led 1 and similarly short messages for the other actions. I would send <V,0> if I wanted the version - the 0 can be ignored but it will make the parsing easier.

agree with the simplification of the protocol but Making it easy for the developer should never be a motivation to make it cumbersome for the human :slight_smile:

<N,1> or <N,0> for the LED but would argue that would be better than <V,0> and I would use just to query the state of LED N

And this is actually even easy for the developer... With your Serial reader code with start and end markers, the line reader will return anyway the full command, so it's actually easier to test if there is a comma or not in the buffer (if so this is a set value command) and if there is none then it's a query. if you have a 'V' it's for the version, if you have an integral number, then you are quarrying the status of that pin.

easy both for the programmer and the operator typing commands :slight_smile:

and that being said

<SET LED 1 ON>
<SET LED 2 OFF>

<GET LED 1 STATUS>

would not not be so difficult to parse either.. Check if starts with "GET " or "SET " (using strncmp()) then see if it's LED or VERSION etc...

important piece for robustness indeed would be the use of start and end markers

J-M-L:
agree with the simplification of the protocol but Making it easy for the developer should never be a motivation to make it cumbersome for the human :slight_smile:

I agree completely. I have been assuming that the data is being sent to the Arduino by a computer program.

If the instructions are being typed by a human finger (or two) then long-winded instructions such as SET LED 1 ON have great scope for errors and frustration at the input end, even if the trouble is taken to write code to parse them at the Arduino end :slight_smile:

(that's why Windows (and equivalents) has replaced the terminal for user input)

...R

J-M-L:
agree with the simplification of the protocol but Making it easy for the developer should never be a motivation to make it cumbersome for the human :slight_smile:

Hi,
As I see it, there would be arguments in every direction. Surely the format you propose ("SET LED 1 ON") is more readable, but, I guess, is fixed. I mean, the syntax has to be (or similar).
Another matter is to develop a kind of A.I. interface; something that would understand "set led one on", "set 1 on" (there's nothing else with number one that can be set), "set last off" and things like that. (By no means I am proposing to develop such a thing; this is other -different- matter).
Regards

This is an important project.
First of all you have to decide a protocol. How do you want transfer data? I think something like:
Start char
Function name
Separetor
Function datas separated
End char
You can add something like the number of message or a CRC.

After you have to think about if and hiw use feedback (messages from Arduino to master that tell "i did it") and about than protocol
You have also to think about hiw the master use than (if the master is not you)
And at the and about how to fix errors that cen be: non valid messages or non present function or parameter.

Done it you can start programming. There are many parts:
Message reading, that controls crc, start/finish or message numbers, and store the text into a string (char array, NEVER use String)
Message traduction: interpretation of the function name and the function data (like function 'set', data '1', data 'HIGH')
Eventually error procedur
Function working: this part call the function and make the real work.
Feedback writing.

It is a grate project