Parse string / character array into chars and ints

I'm trying to convert a character array that is a string representing a command with parameters and I seem to be failing at using the built-in Strings functions. I figured that would be the easiest way to get at the parameters.

buffer is a character array that contains a command in the form of a single character followed by an integer, then two more integers separated by colons, ie. C23:300:100

Here's my code to try and parse it.

    String pinCommand = buffer;
    String value;
    
    int counterPos = pinCommand.indexOf(':');
    int levelPos   = pinCommand.indexOf(':', counterPos + 1);

    value = pinCommand.substring(0, 1);
    char type  = value[0];                                      

    value = pinCommand.substring(1, counterPos - 1);           
    int number = value.toInt;    

    value = pinCommand.substring(counterPos + 1, levelPos - 1); 
    int counter = value.toInt;

    value = pinCommand.substring(levelPos + 1);                 
    int level = value.toInt;

But that just gives me compile errors:

test.ino: In function 'void loop()':
test.ino:39:29: error: cannot convert 'String::toInt' from type 'long int (String::)()const' to type 'int'
test.ino:42:30: error: cannot convert 'String::toInt' from type 'long int (String::)()const' to type 'int'
test.ino:45:28: error: cannot convert 'String::toInt' from type 'long int (String::)()const' to type 'int'

What's the best way to parse out my values into separate variables?

I'm a total C++ newbie. I can barely write shell scripts, so any help appreciated.

I'm trying to convert a character array that is a string representing a command with parameters and I seem to be failing at using the built-in Strings functions.

Well, of course you will. Get a C book, and learn to do it right.

strtok() and atoi() are all you need.

Code that may be similar to what you are looking for. Does your data string have a delimiter to indicate the end of the packet?

//zoomkat 11-12-13 String capture and parsing  
//from serial port input (via serial monitor)
//and print result out serial port
//copy test strings and use ctrl/v to paste in
//serial monitor if desired
// * is used as the data string delimiter
// , is used to delimit individual data 

String readString; //main captured String 
String angle; //data String
String fuel;
String speed1;
String altidude;

int ind1; // , locations
int ind2;
int ind3;
int ind4;
 
void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 11-12-13"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like 90,low,15.6,125*
  //or 130,hi,7.2,389*

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == '*') {
      //do stuff
      
      Serial.println();
      Serial.print("captured String is : "); 
      Serial.println(readString); //prints string to serial port out
      
      ind1 = readString.indexOf(',');  //finds location of first ,
      angle = readString.substring(0, ind1);   //captures first data String
      ind2 = readString.indexOf(',', ind1+1 );   //finds location of second ,
      fuel = readString.substring(ind1+1, ind2+1);   //captures second data String
      ind3 = readString.indexOf(',', ind2+1 );
      speed1 = readString.substring(ind2+1, ind3+1);
      ind4 = readString.indexOf(',', ind3+1 );
      altidude = readString.substring(ind3+1); //captures remain part of data after last ,

      Serial.print("angle = ");
      Serial.println(angle); 
      Serial.print("fuel = ");
      Serial.println(fuel);
      Serial.print("speed = ");
      Serial.println(speed1);
      Serial.print("altidude = ");
      Serial.println(altidude);
      Serial.println();
      Serial.println();
      
      readString=""; //clears variable for new input
      angle="";
      fuel="";
      speed1="";
      altidude="";
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

I found a function that reads from serial and puts the characters in an array until it hits a CR, at which point it's supposed to return a complete array.

It's called from:

 if (readline(Serial.read(), buffer) > 0) {
int readline(int readch, char * buffer) {
  static int pos = 0;
  int rpos;

  if (readch > 0) {
    switch (readch) {
      case '\n': // Ignore new-lines
        break;
      case '\r': // Return on CR
        rpos = pos;
        pos = 0;  // Reset position index ready for next time
        return rpos;
      default:
        buffer[pos++] = readch;
        buffer[pos] = 0;
    }
  }
  // No end of line has been found, so return -1.
  return -1;
}

The reason for that is so that I make sure all the serial communication is complete before I start working on it, without tying up the cpu.

The reason for that is so that I make sure all the serial communication is complete before I start working on it, without tying up the cpu.

And your point is?