Go Down

Topic: Need help with serial communication / substrings (Read 2403 times) previous topic - next topic


I would really appreciate some help. I am building a robot. The robot receives movement commands in the form of ascii text coming in through the arduino's serial port. So, for example, the commands are "move_straight", "move_right", and so on.
The problem is that the command strings aren't terminated with any sort of special character (such as ASCII 13) separating one command from another. It's just a flow of text that runs together one after the other.
I've written other sketches that read in serial data, look for ASCII 13 or whatever, know that it's the end of the string, and then act accordingly. In this case, I don't have that option because there is no separator between the commands.
Could someone write me a little example sketch to point me in the right direction?
One method I was playing with was to read the characters into a char buffer variable one by one. After each character is read, I was going to see if the buffer contained a substring that matched one of my known commands. If it did, then I was going to act upon the command, clear the buffer, and then continue reading characters. The problem is I don't know how to use Arduino C to check whether a char string contains a substring. And I don't have any familiarity with using String objects with respect to serial communication.

Coding Badly

Why can't the sender be changed to include a terminator?


Jan 21, 2011, 02:56 pm Last Edit: Jan 21, 2011, 02:56 pm by PaulS Reason: 1
The problem is I don't know how to use Arduino C to check whether a char string contains a substring.

Code: [Select]
char inData[24] = "move_right";
int index;

if(strcmp(inData, "hit_the_brakes") == 0)
  // Whoa, Nellie.
else if(strcmp(inData, "move_right") == 0)
  // Turn right
  inData[0] = '\0';
  index = 0;
else if(strcmp(inData, "something_else") == 0)
  // Do that, too
The art of getting good answers lies in asking good questions.


If you have some control over what is sending the command strings, then you could add an delimiter at the end of each string. If you can't change what is being sent, then you may have to key in on the "_" that is in each of the move commands you posted. When the "_" is encountered, capture the next characters until a "M" is encountered. then evaluate the captured string for the command it contains.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up