samedi 1 août 2015

Conversion of Byte array to double on receiving of integer values from modbus simulator?

Anyone knows about modbus simulator, I am stucked in converting the values. I am using wimpi modbus api to connect to the device and read and write register values. So I am having a modbus simulator where I got some values in each register based on that one register is of 2 bytes. so i am having 4 register values together give me one measurement value. So using wimpi readregister i am getting integer values for particular register. I have voltage measurement as (16492 35578 10726 22350). These 4 register values makes one double values. So the thing i need is to convert these 4 integer values to a single double value for which modbusutil.registersToDouble() method is available which accepts byte array values.

If anyone help me out for the conversion will be appreciable.

Right now i am trying like this :

byte[] bytes = {(byte) 16492,(byte) 35578,(byte) 10726,(byte) 22350};
        Double d = ModbusUtil.registersToDouble(bytes);
System.out.println(d);

it is throwing exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at net.wimpi.modbus.util.ModbusUtil.registersToDouble(ModbusUtil.java:343)

Please let me know if anybody did the conversion of integer values received from registers to double.

I am getting response values using :

ReadInputRegistersResponse registersResponse = (ReadInputRegistersResponse) this.modbusTCPTransaction.getResponse();

registersResponse.getRegister(1).getValue();
registersResponse.getRegister(2).getValue();
registersResponse.getRegister(3).getValue();
registersResponse.getRegister(4).getValue();

Aucun commentaire:

Enregistrer un commentaire