Reputation: 333
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();
Upvotes: 0
Views: 911
Reputation: 5989
From the docs:
registersToDouble
public static final double registersToDouble(byte[] bytes)
Converts a byte[8] binary double value into a double primitive.
Parameters:
bytes - a byte[8] to be converted.
Returns:
a double value.
So I guess you need to have an array of 8 elements.
Moreover from the Oracle Java Docs:
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive)
So I think you'll lose the information about your measurement converting 35578
fo byte
. It gives: -6
and this is not what you're probably need.
Upvotes: 0