Hi,
Via a peripheral device I receive a string consisting of 8 temperatures. The string has a leading character (">"), and all temperatures are parsed as 5-digit, 2-decimal float with a leading sign, so something like this:
>+0023.3+0029.3+0650.3+0029.3+0029.3+0029.3+0029.3+0029.3
I'm trying read all values from the string into a double array, see code below. The output I'm getting though is that for each iteration, it prints the first value of temp 47 times (equal to the amount of characters left in ptr). If I print the ptr-string, I can see the first value was taken off correctly before the while-loop, but it doesn't update from there on.
The first double is read correctly when I print it, so it is able to read the doubles from the string.
Using strtok() is not an option since the sign can change (+ or -).
I'm sure it's a simple mistake, but I don't see it. Any ideas why this fails?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
char data[65] = ">+0023.3+0029.3+0650.3+0029.3+0029.3+0029.3+0029.3+0029.3"; // example input string
char * numData = data + 1; // remove the first character
char * ptr; // pointer
double temp;
double temperatures[8]; // array of 8 doubles
int n = 0; // increment counter
temp = strtod(numData, &ptr);
temperatures[n] = temp;
while (ptr != NULL) {
temp = strtod(numData, &ptr);
n++;
temperatures[n] = temp;
Serial.println(temp); // Arduino command to print output to serial
}
|