Search code examples
javaipslavemodbus

How to set slave IP Modbus TCP with modbus4j?


I am programming a java application using modbus4j.

I would read %MW of M258 controller from PC java application.

In the modbus4j project I found this sample:

public class Test2 {
    public static void main(String[] args) throws Exception {
        IpParameters params = new IpParameters();
        params.setHost("localhost");
        params.setPort(502);

        ModbusMaster master = new ModbusFactory().createTcpMaster(params, false);
        master.init();

        System.out.println(master.testSlaveNode(5));

        // Define the point locator.
        ModbusLocator loc = new ModbusLocator(1, RegisterRange.HOLDING_REGISTER, 0, DataType.TWO_BYTE_INT_UNSIGNED);

        // Set the point value
        master.setValue(loc, 1800);

        // Get the point value
        System.out.println(master.getValue(loc));
    }
}

But I do not know how can assign the IP to the slave.

In this sample I only can see a slaveID (int), but not the slave IP.

Do you know how can I write the slave IP?


Solution

  • The following 2 lines set the IP a Port of the slave device

    params.setHost("localhost");
    params.setPort(502);