Friday 23 October 2015

Arduino: Read Instruction From Serial

When using Arduino, parsing serial data and converting it to something usable is way harder than it seems.


While this functionality is specially useful as it allows your computer to talk with the Arduino easily, a quick search on google will give you thousands of very different solutions. The one exposed here is just another one that is working perfectly for me.

The instruction:

The idea is to set the position of three servos by serial and also be able to change other parameters.
The instruction, then, must be composed from an instruction name and the parameters, for example, to move the three servos:

MOVE 120 23 90

And, if we also want to switch on a light, the instruction could be:

LIGHT ON

How do we get the Arduino to know that we want the servos to move and then switch on a light?

 

Arduino Read Instruction:

Code:
/*
JMG OCTOBER 2015
This script is made up from snippets found in stackoverflow and other
google results
*/

void setup()
{
  // open serial port at 115200 baud
  Serial.begin(115200);
}

String complete_instruction[4];  // will contain the decoded instruction (4 fields)
void loop()
{
  
  while(!Serial.available()) {}  // if there is nothing on serial, do nothing
  int  i = 0;
  char raw_instruction[25];
  while (Serial.available())
  {  // if something comes from serial, read it and store it in raw_instruction char array
    delay(10); // delay to allow buffer to fill
    if (Serial.available() > 0)
    {
      raw_instruction[i] = Serial.read();
      i++;
    }
  }
  if( strlen( raw_instruction ) > 0 )  // if a new raw_instruction has been read
  {
    // clean raw_instruction before decoding (overwrite non filled array positions with empty spaces)
    for( int n = i; n < 25; n++ ) { raw_instruction[n] = ' '; }
    // decode the instruction (4 fields) (iterator n = field, iterator j = character)
    int j = 0;
    for( int n = 0; n < 4; n++ )
    { 
      while( j < 25 )
      {
        if( raw_instruction[j] == ' ' )
        {
          j++;
          break;
        }
        else
        {
          complete_instruction[n] += raw_instruction[j];
        }
        j++;
      }
    }
    // print decoded instruction by serial
    for( int n = 0; n<4; n++ )
    {
      Serial.println( complete_instruction[n] );
      // clear field after using it
      complete_instruction[n]="";
    }
  }
  delay(50);
}

The summary is that you can send by serial something like "A B C D" (using space as delimiter) and the variable complete_instruction will contain {A, B, C, D}. A,B,C,D can be any kind of data of any length you want, for the instruction MOVE 120 23 90, it will return {"MOVE", "120", "23", "90"}. For the second instruction LIGHT ON, it will return {"LIGHT", "ON", "", "" }.

This way, now you just need to set several if conditions to check if the instruction says "MOVE", "LIGHT", "KILL" or whatever, and if it does, check the remaining parameters (you can use toInt() or toFloat() for numeric values).

My experience with this script is that it works as intended and does not cause memory problems. But I do not have enough knowledge about C to assure that it will not crash your script (from what I've read, String variables together with low RAM space seem to give problems.) Just take it into account if something very weird is happening. (code updated, memory problem solved?)


Bye!

2 comments:

  1. Your code works for a very limited use, just as a (very bad) example. If you tried more commands, let's say around hundreds Arduino would run out of memory and reboot...
    Proper way to do this is using char[] array as fixed length buffer, Google for Serial input basics topic on Arduino forum.
    Main problem of your example is declaring String readString; and then using it as a buffer of unknown length. On a 2kb of available ram it's sure to cause problems. On a PC it's OK.
    Serial input basics topic is a must read on Arduino forum if you want to use Serial for anything reliable...

    ReplyDelete
  2. Hello. Thanks for your comment, I have improved the code to use only one string variable with a declared length and using a char array for the function done previously by "String readString".

    I've tested it and did not find any errors, but as this code will be part from a CNC machine that I'll publishing soon (open source), I would like to hear any comments about possible problems with this new version.

    Thanks in advance.

    ReplyDelete