Vaishnavi - 2016-10-24

Hi All,

Here is my code:

private float getData (String  address, int count) {
float sum = 0.0f;

request = new ReadMultipleRegistersRequest(Integer.parseInt(address), count);
response = new ReadMultipleRegistersResponse();
request.setUnitID(1);
response.setUnitID(1);
request.setHeadless();

 try {

    response = (ReadMultipleRegistersResponse) execute(connection, holdingRequest);

    for ( int i=0; i<registerCount; i++) {
            sum = sum + response.getRegisterValue(i);
    }
    System.out.println("Sum: " + sum);
 } catch (Exception e) {
        e.printStackTrace();
 }
 return sum;
 }

private ModbusResponse execute(SerialConnection connection, ModbusRequest request) {        
    try {
        transaction = new ModbusSerialTransaction(connection);
        transaction.setRequest(request);
        transaction.execute();
        return transaction.getResponse();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

The above code works fine to get positive values whereas it generates all random values for negative value registers. Please help.

 

Last edit: Vaishnavi 2016-10-24