Page 1 of 1
convert two Int to one Float
Posted: Thu Mar 18, 2021 8:15 am
by George_B
Hello to everyone !
In modbus, sometimes, we need to obtain float variables to describe a value.
As i have understand so far there will be need to send two integer variables in order to describe this float variable.
My question is, if i have a float variable = 24.256, how it is possible to split that into two integers ?
Thank in advance !
George
Re: convert two Int to one Float
Posted: Thu Mar 18, 2021 9:15 am
by stefan.erni
Hi George
If you only have this value that you want to convert from float to integer then multiply the value by 1000 then you have an integer. You just have to divide by 1000 somewhere.
regards
Stefan
Re: convert two Int to one Float
Posted: Thu Mar 18, 2021 10:01 am
by George_B
Hi Stafan, nice to hear from you.
Maybe i was not describe the issue with details.
In Modbus, when you define the address you read as float type, it will automatically read two addresses with one integer on each address in order to obtain the float value.
For example:
If you want to read the register 40001 as float then,
you have to read address 40001 as int
you also have to read address 40002 as int
then you have to union those two to end up with the float value.
Since i want to implement my program from a slave modbus device perspective, i need to do the opposite from what i describe above.
I have the float value and i need to send the two integer values through modbus fo as a float value will be obtained in the modbus master device .
Another example is that:
Int_var_1 = 16992
Int_var_2 = -32768
Float_var = 56.125
I can not figure out how to calculate the Int_var_1 and Int_var_2 when i know the Float_var = 56.125
Best Regards
George
Re: convert two Int to one Float
Posted: Thu Mar 18, 2021 10:15 am
by Steve
It sounds like the FLOAT is being represented by 4 bytes. An INT is 2 bytes and so reading the 2 INTs gives you the 4 bytes representing the FLOAT.
Once you have these 4 bytes, you need to know how they represent the FLOAT. For example, see here:
https://en.wikipedia.org/wiki/Single-pr ... int_format
You will also have to understand how these 4 bytes are ordered. This is called "endianness":
https://en.wikipedia.org/wiki/Endianness
There's a discussion about this here:
viewtopic.php?f=7&t=21810
So it's complicated! But we have a component in Flowcode (v8 and v9) that should help with this conversion. It's called "TypeConversion".
Hope this helps.
Steve.
Re: convert two Int to one Float
Posted: Thu Mar 18, 2021 10:28 am
by George_B
Hi Steve, thanks for the reply and help.
I am using Flowcode 7, at the moment i can not use FC8.
I thought it would be easy somehow to implement this conversion using C code.
I have seen this post with 4 bytes but i could not figure out any result since in my case i need to use two integer values.
Regards
George
Re: convert two Int to one Float
Posted: Fri Mar 19, 2021 10:48 am
by Benj
Hi George,
This topic should help.
viewtopic.php?f=28&t=12561&p=49907
In your case the union could instead look like this.
Code: Select all
typedef union
{
MX_FLOAT AsFloat;
MX_UINT16 AsInt[2];
} MX_UnionFloat;
Re: convert two Int to one Float
Posted: Fri Mar 19, 2021 11:14 am
by George_B
hi Benj
Thank you very much for your reply.
This is the Union as you said.
In my case ineed to do the "Seperation" from one Float to two Integers.
Would it be the same as it is for bytes?
Code: Select all
Temp.AsByte[0] = FCV_BYTE0;
Temp.AsByte[1] = FCV_BYTE1;
Temp.AsByte[2] = FCV_BYTE2;
Temp.AsByte[3] = FCV_BYTE3;
FCV_FVAR = Temp.AsFloat;
Re: convert two Int to one Float
Posted: Tue Mar 23, 2021 11:57 am
by Benj
Hello,
In your case it would look like this.
Code: Select all
Temp.AsInt[0] = FCV_INT0;
Temp.AsInt[1] = FCV_INT1;
FCV_FVAR = Temp.AsFloat;
Re: convert two Int to one Float
Posted: Sat Dec 07, 2024 7:15 pm
by George_B
Hi everyone,
after a lot of time still need to use this function to convert the two integer into a float and reverse.
Would it be posible for someone to provide a valid C code converting the HighWord(integer1) and LowWord(integer2) into a single float ?
Thanks in advance !
George
Re: convert two Int to one Float
Posted: Sat Jan 04, 2025 10:20 pm
by viktor_au
Hi George
Please search this forum:
convert two Int to one Float