I'm trying to read and write to a PLC via a gateway using modbus4j. I've gotten the read function to work, but I'm not getting anywhere on the setValue() method. I've tried various variations of this:


IpParameters params = new IpParameters();
params.setHost("192.168.0.100");
params.setPort(502);

//create modbus TCP master
ModbusMaster master = new ModbusFactory().createTcpMaster(params, false);
master.init();

int regValue=0;
regValue = regValue | 1 << 0;
ModbusLocator loc0 = new ModbusLocator(1,RegisterRange.HOLDING_REGISTER,0,DataType.TWO_BYTE_INT_UNSIGNED);
master.setValue(loc0,8);

Also the getValue() method only works sporadically maybe 30% of the time I call getValue() I get the response I expect, and the other times I get all 0s out.

System.out.println(master.getValue(33,RegisterRange.HOLDING_REGISTER,70,2));

Am I misunderstanding some aspect of the get and set value methods? Thanks.