01-17-2012 08:53 AM
I have an instrument with a serial output. The output string is X=+03.22 CR LF Y=-04.22 CR LF. I need to extract the two numbers.
I have been trying to use strtod in the stdlib. Is there an example code showing how to make and use character strings?
Solved! Go to Solution.
01-17-2012 08:57 AM
Hi,
you might consider scanf instead. Refer to userint\standardio.cws for an example of using the scanf function.
01-17-2012 10:32 AM
If this is the only thing your instrument sends out, X and Y values one after the other, then you can do this:
char buffer[16];
double valX, valY;
ComRdTerm (port, buffer, 15, 13); // 13 = CR byte value
valX = atof (&buffer[2]);
ComRdTerm (port, buffer, 15, 13);
valY = atof (&buffer[2]);
01-17-2012 12:49 PM
Thanks for the help. Atof fixed the prob
Jim R
01-18-2012 03:33 AM
I need to correct my code a little. In order to atof work correctly it must see a null character to understand where the string ends.
Here is the corrected version.
char buffer[16];
int len;
double valX, valY;
len = ComRdTerm (port, buffer, 15, 13); // 13 = CR byte value
buffer[len] = 0;
valX = atof (&buffer[2]);
ComRdTerm (port, buffer, 15, 13);
buffer[len] = 0;
valY = atof (&buffer[2]);
01-18-2012 03:58 AM
Sorry... but I would suggest one more (minor) change
char buffer[16];
int len;
double valX, valY;
len = ComRdTerm (port, buffer, 15, 13); // 13 = CR byte value
buffer[len] = 0;
valX = atof (&buffer[2]);
len = ComRdTerm (port, buffer, 15, 13);
buffer[len] = 0;
valY = atof (&buffer[2]);
01-18-2012 05:28 AM - edited 01-18-2012 05:29 AM
Thank you Wolfgang, I had forgotten to put it there...
Now we have a complete code!... 😉