Thank you Matthew.
I was able to use the factory and create the RTU master. The issue I was having was related with the definition of the serial parameters .. This is the code I came up with. It works .. knowing that the slave I'm reading from has the ID 7, and the connection over RS485 (COM6 here) is of "N81" type, at 9600 baudrate .
package appliancemb;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.code.RegisterRange;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.io.serial.*;
// declare variables
public class appliance {
public static void main(String[] args) throws Exception {
ModbusFactory factory = new ModbusFactory();
SerialParameters params = new SerialParameters();
params.setCommPortId("COM6");
params.setBaudRate(9600);
params.setDataBits(8);
params.setStopBits(1);
params.setParity(0);
ModbusMaster master = factory.createRtuMaster(params);
// master.setRetries(4);
master.setTimeout(1000);
master.setRetries(0);
long start = System.currentTimeMillis();
// Don't start if the RTU master can't be initialized.
try {
master.init();
} catch (ModbusInitException e) {
System.out.println( "Modbus Master Init Error: " + e.getMessage());
return;
}
try {
System.out.println("Reg. 1001 Value:" + master.getValue(7, RegisterRange.HOLDING_REGISTER, 1000, DataType.FOUR_BYTE_FLOAT_SWAPPED));
// more like the above until all required register values are read.
// ..
}
finally {
master.destroy();
}
System.out.println("Time elapsed: " + (System.currentTimeMillis() - start) + "ms");
}
}
Thanks again.