Search code examples
c#floatingmodbus

How to convert a modbus 32-bit word to a C# float?


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 ?


Solution

  • Couples of things you need to check:

    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.