08-24-2015 06:29 AM
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.
08-24-2015 07:50 AM
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
08-24-2015 01:34 PM
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.
08-25-2015 01:16 AM
:manhappy: RobertoBozzolo :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
08-25-2015 12:57 PM
I am now out of office without CVI to test with, but the idea I suggested was along this line:
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!).