LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I send real numbers through rs232?

I know this should be easy but I can't figure out how to do it. I have a real number of type FLOAT (ex. 3.14) and I want to send it through the serial port via rs232. How do I convert this number into a character string that I can use as a parameter in ComWrt()?

Also, on the other end, how would I read it using ComRd(), and convert the character string back into a real number?

Thanks in advance.
0 Kudos
Message 1 of 5
(3,607 Views)
To convert from a double/float to say a string I tend to use the following:

#define OUTPUT_STRING "pwr=%f dBm\n"

sprintf (pszMyOutputString,OUTPUT_STRING,"45.56");

The string "pszMyOutputString" should now contain "pwr = 45.56 dBm\n" which you can send to your rs232




--
To convert from string to say float (or double in this case) you can use the strtod function.

*pdValue = strtod (pszTmp, NULL);

pdValue is a pointer to a double to contain the result.

pszTmp is a point to a null terminated string that contains the floating point number.

the NULL is this case is used to indentify the first character in pszTmp that I do not want to convert
(so if I had 3.14dBM), I would point the 'd' and not a NULL).

aDouble= strtod ("3.14", NULL
);


Hope this helps, if not, let me know and I can elaborate further,

Regards

Chris
0 Kudos
Message 2 of 5
(3,607 Views)
Thank you so much!!! That was a big help!
0 Kudos
Message 3 of 5
(3,607 Views)
No problem...

Chris
0 Kudos
Message 4 of 5
(3,607 Views)
Don't forget that if you READ a string from RS232, there is no terminating
null.

"Shiner" wrote in message
news:506500000005000000C4890000-1023576873000@exchange.ni.com...
> To convert from a double/float to say a string I tend to use the
> following:
>
> #define OUTPUT_STRING "pwr=%f dBm\n"
>
> sprintf (pszMyOutputString,OUTPUT_STRING,"45.56");
>
> The string "pszMyOutputString" should now contain "pwr = 45.56 dBm\n"
> which you can send to your rs232
>
>
>
>
> --
> To convert from string to say float (or double in this case) you can
> use the strtod function.
>
> *pdValue = strtod (pszTmp, NULL);
>
> pdValue is a pointer to a double to contain the result.
>
> pszTmp is a point to a null terminated string that contains the
> floating point number
.
>
> the NULL is this case is used to indentify the first character in
> pszTmp that I do not want to convert
> (so if I had 3.14dBM), I would point the 'd' and not a NULL).
>
> aDouble= strtod ("3.14", NULL);
>
>
> Hope this helps, if not, let me know and I can elaborate further,
>
> Regards
>
> Chris
0 Kudos
Message 5 of 5
(3,607 Views)