This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

String tokenizing

I need to extract eight values from a string I'm receiving from my serial port. I have the following code:

unsigned char xdata *p_ch;
unsigned int  xdata ch = 0;

// Get command
   p_ch = strtok( com_buffer, "," );

// Get status
   while ( p_ch != NULL )
   {

//    Get string
      p_ch = strtok( NULL, "," );

//    Convert to float
      ad_value[ch] = atof( p_ch );
      ch++;

      if ( ch > 8 )
        break;

    } //__while ( p_ch != NULL )__

It works very intermittently. Can anyone suggest a better way to decode my serial string?

Thanks.

Parents
  • If it is just some code you "threw together", then you should wait until your code has some form of error handling before asking why it only works intermittently. The error handling will not only stop your code from processing bad data - it will also help you to find bugs in your code or incorrect assumptions about your data.

Reply
  • If it is just some code you "threw together", then you should wait until your code has some form of error handling before asking why it only works intermittently. The error handling will not only stop your code from processing bad data - it will also help you to find bugs in your code or incorrect assumptions about your data.

Children
No data