Thread: problem with strstr func...embedded system

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    problem with strstr func...embedded system

    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?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And if your println cval, what do you get?

    (And the string tokenizer in C is strtok. It appears to be strtok week this week here at CBoard.)

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Tabstop,
    It was funny: with one right question you guided me how to solve my problem
    This showed the power of putting right question, many thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strstr problem
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 09-08-2009, 03:46 AM
  2. strstr problem...
    By Sebastiani in forum C Programming
    Replies: 6
    Last Post: 10-09-2008, 07:11 AM
  3. Embedded system board
    By ssharish2005 in forum Tech Board
    Replies: 1
    Last Post: 08-12-2007, 03:03 PM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. System Hook Problem.
    By DutchStud in forum Windows Programming
    Replies: 0
    Last Post: 11-17-2001, 05:43 PM