Vaishnavi - 2016-08-13

Hi,

I'm using jamod library and trying to convert data read from Input Registers to float. Here is my code:

inputRegisterRequest = new ReadInputRegistersRequest(30512, 2);
inputRegisterResponse = new ReadInputRegistersResponse(); 

try {
            inputRegisterResponse = (ReadInputRegistersResponse) execute(connection, inputRegisterRequest);
            int tempData = 0;               

            for ( int i=0; i<inputRegisterResponse.getWordCount(); i++) {
                    tempData = tempData + inputRegisterResponse.getRegisterValue(i);
                }

                // Method 1:
                float f = Float.intBitsToFloat(tempData);
                System.out.println("The new float value: " + f);

                // Method 2:
                float f = ModbusUtil.registersToFloat(inputRegisterToByteArray(inputRegisterResponse.getRegisters()));

                // Method 3:
                DataInputStream dis = new DataInputStream (new ByteArrayinputStream(inputRegisterResponse.getRegisters()));
                float f = dis.readFloat();
        } 

Everytime I get some incorrect answer. Any help would be appreciated.

 

Last edit: Vaishnavi 2016-08-13