Good Day,
I have bit of a problem converting a 32-bit Float number from a Modbus slave into a Dynamic C float with the correct Value.
xmem float ET107_FLOATTEST(void)
{
auto char msg[4];
float f;
//Below msg array is a simulation of a part of a Modbus packet coming in from a weather station as a response to Modbus F03. Since the 32bit message is made up of two 16 bit Holding registers it will have to be read in the following order msg[2]msg[3]msg[0]msg[1], or 0x4156E147. the correct deci value should be 13.429…
msg[0]=0xE1;
msg[1]=0x47;
msg[2]=0x41;
msg[3]=0x56;
//SHIFT and UNIT the 8 bit binary packets into a 32 bit bit floating value of //‘\B01000001010101101110000101000111’ = 13.429
f=(((((msg[2]<<24))|(msg[3]<<16))|(msg[0]<<8))|msg[1]);
return f;
}
auto float crntValue;
auto char buff1[7]
crntValue=ET107_FLOATTEST();
sprintf(buff1, "%.1f", crntValue);
consolePuts(buff1);
I know I should be geting 13.429 or 13.4 (%1f) but i keep on getting -7849.0 instead??
I wonder can anybody point out my error in this logic? I’m sure its in the ET107_FLOATTEST() function.
Thanks:o