LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

convert string to a number

Solved!
Go to solution

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?

0 Kudos
Message 1 of 7
(4,095 Views)

Hi,

 

you might consider scanf instead. Refer to userint\standardio.cws for an example of using the scanf function.

0 Kudos
Message 2 of 7
(4,094 Views)
Solution
Accepted by topic author Jim Rogers

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

S. Eren BALCI
IMESTEK
0 Kudos
Message 3 of 7
(4,085 Views)

Thanks for the help. Atof fixed the prob

 

Jim R

0 Kudos
Message 4 of 7
(4,073 Views)

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

 

S. Eren BALCI
IMESTEK
0 Kudos
Message 5 of 7
(4,053 Views)

Sorry... Smiley Wink 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]);

0 Kudos
Message 6 of 7
(4,049 Views)

Thank you Wolfgang, I had forgotten to put it there...

 

Now we have a complete code!... 😉

S. Eren BALCI
IMESTEK
0 Kudos
Message 7 of 7
(4,044 Views)