STRLEN() doesnt return a expected number

I know this function cant go wrong so I must be doing something wrong.

bool newData = true;
char *AXpos;
char *EYpos;
const char *delimiter = ",";
//char inData[numChars];  // an array to store the received data
char inData = "123.45,678.90\n";  // an array to store the received data

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);  // initialize serial communication
  while (!Serial) {
    delay(0);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  int length = strlen(inData);
  Serial.println(length);//should be 14 but returns 9
  extractData();
  exit(0);
}

void extractData() {
  //Extract two numbers from received data
  if (newData == true) {
    AXpos = strtok(inData, delimiter);
    EYpos = strtok(NULL, delimiter);
    Serial.print(AXpos);
    int length = strlen(AXpos);
    Serial.println(length); // should be 6 but returns 3
  }
}

So what do I mis??

char inData[] = "123.45,678.90\n";  // an array to store the received data

char inData[], not char inData.

Dollars to donuts if you turned your warning level up to all, the compiler would have told you that.

Edit: yes, with warning=all, the compiler complained at length.

char is a datatype that can contains only a single letter (char).
To save a string you need an array of chars, so
char inData[] = "123.45,678.90\n";

Overflow.

This equals 9.

char inData[] = "12345678\n";  // an array to store the received data

This equals 1.

char inData[] = "12345678xn";  // an array to store the received data

It removes the escape character and overflows.

Try...

char inData[] = {"123.45,678.90\n"};
int length = sizeof(inData)/sizeof(inData[0]);

void setup() {
  Serial.begin(115200);
  Serial.println(length);
}

void loop() {
}

OK thanx, for the first section it works:

bool newData = true;
char *AXpos;
char *EYpos;
const char *delimiter = ",";
//char inData[numChars];  // an array to store the received data
char inData[] = "123.45,678.90\n";  // an array to store the received data

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);  // initialize serial communication
  while (!Serial) {
    delay(0);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  int length = strlen(inData);
Serial.println(length);
  extractData();
  delay(1);
  while(true);
}

void extractData() {
  //Extract two numbers from received data
  if (newData == true) {
    AXpos = strtok(inData, delimiter);
    EYpos = strtok(NULL, delimiter);
    Serial.println(AXpos);
 //   int length = sizeof(AXpos);
 //   Serial.println(strlen(AXpos));  // should be 6 but returns 3
   int length1 = sizeof(AXpos);
 Serial.println(length1); 
 Serial.println(EYpos);
   int length2 = sizeof(EYpos);
 Serial.println(length2);
  }
}

But how should I declace AXpos and EYpos becourse boht lengths is 2

The size of a pointer is 2 bytes for an R3.

If you want the length of the string it points to, you use strlen of course.

I can see from your code that you know how to spell strlen(). So why did write STRLEN() in the title or this thread?

Problem solved,

bool newData = true;
char *AXpos;
char *EYpos;
const char *delimiter = ",";
//char inData[numChars];  // an array to store the received data
char inData[] = "123.45,678.90\n";  // an array to store the received data

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);  // initialize serial communication
  while (!Serial) {
    delay(0);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  extractData();
  delay(1);
  while (true)
    ;
}

void extractData() {
  int length;
  //Extract two numbers from received data
  if (newData == true) {
    AXpos = strtok(inData, delimiter);
    EYpos = strtok(NULL, delimiter);
    // prepare data for further processing
    length = strlen(AXpos);
    AXpos[length] = '\0';// make a zero terminated string
    length = strlen(EYpos) - 1;// this string contains a '\n' so length-1
    EYpos[length] = '\0';
    Serial.println(AXpos);
    //    Serial.println(strlen(AXpos));
    Serial.println(EYpos);
    //    Serial.println(strlen(EYpos));
  }
}

now on to the next part thnx

You might want to revisit this

strtok() will add a null char where the delimiter is found and that's a good thing otherwise strlen(AXpos) would not works since strlen() relies on the presence of the null char to determine the length ➜ so AXpos[length] = '\0';// make a zero terminated string is useless.

for EYpos, if you were to look for \n as a separator too, then you would not have to get rid of the trailing line feed ➜ use const char *delimiter = ",\n";

➜ try this

const char *delimiter = ",\n";
char inData[] = "123.45,678.90\n";  // an array to store the received data

void setup() {
  Serial.begin(115200);
  const char * AXpos = strtok(inData, delimiter);
  const char * EYpos = strtok(NULL, delimiter);
  Serial.print("AXpos = ["); Serial.print(AXpos); Serial.println("]");
  Serial.print("EYpos = ["); Serial.print(EYpos); Serial.println("]");
}

void loop() {}

if I typed that right, you should see (console at 115200 bauds)

AXpos = [123.45]
EYpos = [678.90]

note that your pointers do point inside the inData buffer, so don't rely on these pointers once you start listening for new incoming data - either make a copy or extract the value into a float or double (you can use strtod() for that)

the exit(0) is messing up the output. Basically kills it before you see anything. As far as I can tell.

-jim lee

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.