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
}
}
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
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));
}
}
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)