8
\$\begingroup\$

Im looking to integrate a command interface in a project that im making that will accept commands overs serial from a PC.

The idea is that the device will do its thing but it gets sent a command, it will perform it. This is different to most examples i've managed to find which is just an idling device waiting for serial data.

So for example, a device which controls LEDs and plays an animation. Lets say theres 3 preset animations, and when it turns on it always plays number 1. On the PC side I would send a command 'play preset 2', or 'change color to green' or sending a live real time animation to the device to play on the LEDs.

So my question is that I can not find much information on this "design pattern" ? I do not know where to start to look. The closest i've come is pre-made arduino serial parsing libraries. However im looking for more of C / non arduino / theoretical approach.

I have looking into serial protocols but that is just things such as SPI and I2C etc, not actually how to implement a control / command interface in software.

I also found out about HDLC, again though it seems to be more of a standard / a protocol. A similar thread is https://stackoverflow.com/questions/1445387/how-do-you-design-a-serial-command-protocol-for-an-embedded-system Which has some very nice information but again its to do with the protocol / communication itself.

Im trying to find how to code / implement this to create a command or control ability.

Do I use an infinite loop to monitor and catch the serial for an incoming command, and use a list of IF statements to choice what command to carry out, but then what happens when no commands and the system should be as normal, or if it misses a command?

Interrupt driven, state machine?

Is there an official name for this that can be used to research and find examples?

EDIT:

For anyone who has the same question, I found this really nice example while researching, http://www.adnbr.co.uk/articles/parsing-simple-usart-commands

\$\endgroup\$
  • \$\begingroup\$ Why not use a framework such as FreeMODBUS or Firmata? \$\endgroup\$ – Ignacio Vazquez-Abrams Dec 28 '13 at 19:43
5
\$\begingroup\$

There are many ways to implement a communication protocol. For example you may choose to use:

  • human readable format or binary
  • error checking such as a checksum or CRC
  • start and/or end message characters
  • message length, number etc payload information
  • acknowledge / negative acknowledge / retry mechanisms

Your example command is human readable and should use '\n' (enter) as the end of message character. Start / end of message characters make it easier to parse the input stream.

A typical program flow would be to wait for the start character to be received, then store subsequent bytes into a message buffer until the end character is received. Once that happens pass the buffer to another function to parse the message. If no start character, just start storing bytes immediately until the end character is received.

Most* embedded systems run in an infinite loop. Handling of serial transmit and receive is done via interrupts on the Arduino (see HardwareSerial.cpp). Received characters are placed into a buffer. On every iteration of the loop you check to see if there are any characters in the buffer (Serial.available()) and process them. By receiving characters using interrupts no characters should be missed. You don't do all the processing in these interrupt routines though, else they become too long and therefore miss subsequent interrupts.

If you don't want your device to sit idle looping, put it into a sleep state to 'pause' the loop until an interrupt or other event.

I really like this library for human readable messaging: http://playground.arduino.cc/Code/Messenger

* I can't think of a system which doesn't but perhaps there is one out there.

\$\endgroup\$
3
\$\begingroup\$

All your program has to do is:
1. frequently check the serial port for a new character
2. jump to appropriate routine/set status/whatever, depending on the character
3. go to 1

The program can act on single characters, or you can create your own message format - I often started a cammand with "$" and end with newline, and used readable ASCII characters for the command (makes for easier debugging!).

Make sure that the program won't get hung up if it receives an invalid command!

\$\endgroup\$
  • \$\begingroup\$ Some Automated Test Equipment (ATE) uses very similar serial links to do this. \$\endgroup\$ – Spoon Dec 28 '13 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.