ChrisLi - 2015-11-17

hi there,

thanks for the library, but I have a problem with sending telegramms over TCP.
the communication works with a S7 PLC Modbus master and the Beckhoff/Wago slaves.
see the attachment 13a + 13b as request and response example.
if I disable the PLC and use your library as master, I only get 2 requests but no response.
see 13c + 13d.
what could I do?

thanks in advance
christof

this is my code

import com.ghgande.j2mod.modbus.io.ModbusTCPTransaction;
import com.ghgande.j2mod.modbus.msg.ReadInputRegistersRequest;
import com.ghgande.j2mod.modbus.msg.ReadInputRegistersResponse;
import com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersRequest;
import com.ghgande.j2mod.modbus.msg.ReadMultipleRegistersResponse;
import com.ghgande.j2mod.modbus.net.TCPMasterConnection;
import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author zho
 */
public class j2modTest {
    /* The important instances of the classes mentioned before */
    protected static TCPMasterConnection con = null; // the connection
    protected static ModbusTCPTransaction trans = null; // the transaction
    protected static ReadInputRegistersRequest req = null; // the request
    protected static ReadInputRegistersResponse res = null; // the response
    /* Variables for storing the parameters */
    protected static InetAddress addr = null; // the slave's address
    protected static final int port = 502;
    protected static final int ref = 0; // the reference number of the register to read from; offset where to start reading from
    protected static final int count = 1; // the number of words to be read
    protected static final int repeat = 1; // a loop for repeating the transaction

    /**
     * @param args
     *            the command line arguments
     */
    public static void callTCPModbus() {
        try {
            addr = InetAddress.getByName("x.x.x.x"); //hidden for the post
            // 2. Open the connection
            con = new TCPMasterConnection(addr);
            con.setPort(port);
            con.connect();
            req = new ReadInputRegistersRequest(ref, count);
            trans = new ModbusTCPTransaction(con);
            // for (int a = 0; a < 4; a++) {
            // req.setTransactionID(a + 1);
            // req.setUnitID(a + 1);
            // trans.setRequest(req);
            // trans.execute();
            // res = (ReadInputRegistersResponse) trans.getResponse();
            // for (int n = 0; n < res.getWordCount(); n++) {
            // System.out.println("Word " + a + "=" + res.getRegisterValue(n));
            // }
            req.setTransactionID(1);
            req.setUnitID(1);
            trans.setRequest(req);
            trans.execute();
            res = (ReadInputRegistersResponse) trans.getResponse();
            for (int n = 0; n < res.getWordCount(); n++) {
                System.out.println("Word " + 0 + "=" + res.getRegisterValue(n));
            }
            // 6. Close the connection
            con.close();
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
 

Last edit: ChrisLi 2015-11-17