Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website

Modbus4j modbus/tcp client with Siemens s7 1200 slave "Test" code


  • Hi, I'm trying to run the Test code from the modbus4j-scr folder to communicate with my s7 1200. I wanna read the first 10 holding register values from the PLC and I know my modbus server configuration is OK because I've got those 10 values with the ICC modbus master tool.

    My Server's IP is 192.168.0.1 and its slaveID is 1. My code is:

    package modbusmastertest;

    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.ip.IpParameters;

    class MBMasterTest {
    public static void main(String[] args) throws Exception {
    ModbusFactory factory = new ModbusFactory();
    IpParameters params = new IpParameters();
    //params.setHost("127.0.0.1");
    params.setHost("192.168.0.1"); // Server's IP adress
    params.setPort(502);
    params.setEncapsulated(true);
    ModbusMaster master = factory.createTcpMaster(params, false);
    // master.setRetries(4);
    master.setTimeout(2000);
    master.setRetries(0);

        long start = System.currentTimeMillis();
        try {
            master.init();
            for (int i = 0; i < 10; i++) {
                //System.out.println(master.getValue(127, RegisterRange.HOLDING_REGISTER, 1220, DataType.TWO_BYTE_INT_UNSIGNED));
            	//System.out.print(master.getValue(1, RegisterRange.HOLDING_REGISTER, 1220, DataType.TWO_BYTE_INT_UNSIGNED));
            	System.out.println(master.getValue(1,RegisterRange.HOLDING_REGISTER, 1 , DataType.TWO_BYTE_INT_SIGNED));  // ERROR LINE  :?
                //System.out.println(master.getValue(1, 40001, 1 , DataType.TWO_BYTE_INT_SIGNED));
            }
        }
        finally {
            master.destroy();
        }
    
        System.out.println("Took: " + (System.currentTimeMillis() - start) + "ms");
    }
    

    }

    but when I press run i get a TimeoutException from the 28th code line (the one I comment as the ERROR LINE: "System.out.println..."). I'm a little noobie at modbus and to be honest.. I don't get what's going on in that loop.. If someone helps me I'd really appreciate it, thanks.


  • @Vicarious said:

    Hi, I'm trying to run the Test code from the modbus4j-scr folder to communicate with my s7 1200. I wanna read the first 10 holding register values from the PLC and I know my modbus server configuration is OK because I've got those 10 values with the ICC modbus master tool.

    My Server's IP is 192.168.0.1 and its slaveID is 1. My code is:

    package modbusmastertest;

    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.ip.IpParameters;

    class MBMasterTest {
    public static void main(String[] args) throws Exception {
    ModbusFactory factory = new ModbusFactory();
    IpParameters params = new IpParameters();
    //params.setHost("127.0.0.1");
    params.setHost("192.168.0.1"); // Server's IP adress
    params.setPort(502);
    params.setEncapsulated(true);
    ModbusMaster master = factory.createTcpMaster(params, false);
    // master.setRetries(4);
    master.setTimeout(2000);
    master.setRetries(0);

       long start = System.currentTimeMillis();
       try {
           master.init();
           for (int i = 0; i < 10; i++) {
               //System.out.println(master.getValue(127, RegisterRange.HOLDING_REGISTER, 1220, DataType.TWO_BYTE_INT_UNSIGNED));
           	//System.out.print(master.getValue(1, RegisterRange.HOLDING_REGISTER, 1220, DataType.TWO_BYTE_INT_UNSIGNED));
           	System.out.println(master.getValue(1,RegisterRange.HOLDING_REGISTER, 1 , DataType.TWO_BYTE_INT_SIGNED));  // ERROR LINE  :?
               //System.out.println(master.getValue(1, 40001, 1 , DataType.TWO_BYTE_INT_SIGNED));
           }
       }
       finally {
           master.destroy();
       }
    
       System.out.println("Took: " + (System.currentTimeMillis() - start) + "ms");
    

    }
    }

    but when I press run i get a TimeoutException from the 28th code line (the one I comment as the ERROR LINE: "System.out.println..."). I'm a little noobie at modbus and to be honest.. I don't get what's going on in that loop.. If someone helps me I'd really appreciate it, thanks.


  • I got help and figured it out. My PLC can't read encapsulated data.. at least with my current configuration I think.. It works perfectly.