Pages: [1]   Go Down
Author Topic: Need help with serial communication / substrings  (Read 1328 times)
0 Members and 1 Guest are viewing this topic.
Asheville, North Carolina
Offline Offline
Newbie
*
Karma: 3
Posts: 17
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 206
Posts: 12849
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49059
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The problem is I don't know how to use Arduino C to check whether a char string contains a substring.
Code:
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
}
« Last Edit: January 21, 2011, 08:56:57 am by PaulS » Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: