how to convert modbus 32 bit word into a float point in c#?
here is the code I have :
float Modbus_Floating(byte[] temp)
{
byte[] temp2 = new byte[4];
temp2[0] = temp[3];
temp2[1] = temp[2];
temp2[2] = temp[1];
temp2[3] = temp[0];
float f = System.BitConverter.ToSingle(temp, 0);
return f;
}
it is not working, I am getting a big negative number I should get 36.7
[0] 102 byte
[1] 102 byte
[2] 66 byte
[3] 22 byte
but I am not getting that .. why ?
Couples of things you need to check:
float
's norm IEEE-754 ?which endianness your device uses ?
you can see below that endianness can be per WORD
, opposed to DWORD
http://store.chipkin.com/articles/modbus-floating-point-encoding
When you're sure about all these and BitConverter
failed, you can always parse manually, it's fairly easy as in this example, providing you have the specifications.