Warm tip: This article is reproduced from serverfault.com, please click

Reading the right Schneider Momentum PLC register or coil address using JAMOD

发布于 2020-12-12 17:13:56

To save some money at work, I thought I would create a java program to right/write values to a Momentum PLC instead of having to purchase the WonderWare License from Schneider, but I can't seem to figure out the addressing on the PLC side. The Communication mode is Modbus/TCP and for testing purposes at home, I downloaded a PLC simulator, but I can't get it to work either. I connect to it fine, just, again, can't seem to poll the right address.

The coil output I am trying to read has the 400012 address on the plc side at work, and I am trying to replicate that.

Here is my javafx code built with Maven that connects to the PLC, I can read values, but I seem to never get the right one.

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadCoilsRequest;
import net.wimpi.modbus.msg.ReadCoilsResponse;
import net.wimpi.modbus.net.TCPMasterConnection;

import java.net.InetAddress;

public class PrimaryController {


@FXML
private Label labelValue;

@FXML
private Button primaryButton;

@FXML
void handleGetData(ActionEvent event) {
    try {
        getData();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void getData() throws Exception {

    // Connection
    TCPMasterConnection con = null;
    ModbusTCPTransaction trans = null;
    ReadCoilsRequest req = null;
    ReadCoilsResponse res = null;

    InetAddress ipAddress = null;

    try {
        ipAddress = InetAddress.getByName("localhost");
        con = new TCPMasterConnection(ipAddress);
        con.setPort(502);
        System.out.println("Connecting...");

        con.connect();

        req = new ReadCoilsRequest(12, 1);
        trans = new ModbusTCPTransaction(con);
        trans.setRequest(req);
        trans.execute();
        res = (ReadCoilsResponse) trans.getResponse();
        System.out.println("Response: " + res.getCoils().toString());
        con.close();


    } catch (Exception e) {
        e.printStackTrace();
    }


  }

}

Dependancy:

    <dependency>
        <groupId>net.wimpi</groupId>
        <artifactId>jamod</artifactId>
        <version>1.2</version>
    </dependency>

here is the PLC simulator I am using.

ModBus Simulator

I always get Response: 00000000

Is anyone experienced with communication between a PLC (Schneider Momentum) and Java?

Thanks!

Questioner
ADSquared
Viewed
0
Lluis Felisart 2020-12-13 02:12:48

Addresses of type 40012 or 400012 (when using 6-digit addressing) correspond to 16-bit registers in the "Holding Registers" field, they are not coils.

Also, as the first coil is at address 00001 when passing an offset to the read method you should pass 11 at not 12 to read coil 00012

Your code is most likely returning the value of coil 00013 in the "Coils" field.

enter image description here