Hi,
I have a router working on Linux that must send commands to an Atmega168 chip. I need to write a command analyzer that gets commands from the serial port and analyze them.
A command may come like "#1p200*', meaning "motor on position 1 must be pulsed 200", or '#1p200*#A*#B*' meaning "motor on position 1 must be pulsed 200 and read the sensor on position A and read the sensor on position B".
I decided to use strstr() function to extract strings inside '#' and '*', but I failed to use it in my configuration.
I first tested the function in my pc to be sure I know how it works:
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *p;
p = strstr("this is a test", "is");
if(p)
printf(p);
else
printf("it is null\n");
return 0;
}
Which returns "is is a test", as expected.
Next I tried to make a test in my real context to see if it works there too:
Code:
if(Serial.available()>0)
{
int val = Serial.read();
char *cval = (char*)val;
char *cmd;
cmd = strstr(cval, "my test");
if(cmd )
Serial.println("done");
else
Serial.println("failed");
}
Finally to test it, in my Linux command line I issue:
Code:
echo 'this is my test!' > /dev/tts/1
and I get a failed!
The serial connection is tested, every other thing works fine. The only problem is with strstr.
In Java I have done this very easy this way:
Code:
//inputLine is of String class, and I used '@' for separating commands of pattern: xx@yyy
strSplit = new StringTokenizer(inputLine, "@");
strCmd = strSplit.nextToken();
strCmdVal = strSplit.nextToken();
but this is C and I don't have such libs like in Java and I don't have enough knowledge of the available techniques.
Please give some ideas or may be there are better solutions for this situation in C?