LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

i want to get float input from numeric control and then save it in four bytes

i want to get float type input from my numeric control for example (34.7462732738 ) and then save it in 4 bytes for sending 4 bytes to serial port kindly guide me, i have idea about integers in which shifting right 8 bits and storing data to byte by byte but is this possible for float ,  i want simpler and smarter solution so that i dont loose precision of my float.

thanks in advance.

 

0 Kudos
Message 1 of 5
(4,338 Views)

Hi,

 

You need to investigate the use of unions wrapped around a typedef. Something like this I would suggest:

 

typedef union
{
 float Float;
 unsigned char Bytes[4];
}FloatByte_t;


void FloatByteTest(float Float, unsigned char *Byte1, unsigned char *Byte2, unsigned char *Byte3, unsigned char *Byte4)
{
 FloatByte_t FloatByte;

 FloatByte.Float = Float;
 *Byte1 = FloatByte.Bytes[0];
 *Byte2 = FloatByte.Bytes[1];
 *Byte3 = FloatByte.Bytes[2];
 *Byte4 = FloatByte.Bytes[3];
 
}

 

 

Kevin

0 Kudos
Message 2 of 5
(4,325 Views)

I can't test it now but I would try something like that:

 

float  f;

f = 1.23456;
ComWrt (port, (char *)&f, 4); 

Of course you will receive a IEEE float on the other side of the line so you will need to cast it to the appropriate format to obtain the float.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 5
(4,313 Views)

:manhappy: Proven Zealot :manhappy: please can you guide me in detail i am unable to understand , first i want to store in byte array and then write on com port

0 Kudos
Message 4 of 5
(4,303 Views)

I am now out of office without CVI to test with, but the idea I suggested was along this line:

  • (char *)&f    treat the memory pointed to by &f as a char array, and
  • ComWrt   send it to the com port

 

If you want to split this it two tasks you can use memcpy to fill a string with the raw content of the float variable, then write that string to the com port (WITHOUT using strlen operator!).



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 5 of 5
(4,283 Views)