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:
Which returns "is is a test", as expected.
p = strstr("this is a test", "is");
printf("it is null\n");
Next I tried to make a test in my real context to see if it works there too:
Finally to test it, in my Linux command line I issue:
int val = Serial.read();
char *cval = (char*)val;
cmd = strstr(cval, "my test");
and I get a failed!
echo 'this is my test!' > /dev/tts/1
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:
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.
//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();
Please give some ideas or may be there are better solutions for this situation in C?