在Java(Jamod库)中使用协议Modbus,“主人”不会看到“奴隶”

时间:2018-01-27 20:42:00

标签: java intellij-idea modbus

使用协议Modbus,我需要模拟简单的“slave”和“master”,并在它们之间交换数据。我使用java,我选择了一个Jamod库。我找到了“slave”here和“master”here的简单示例,但我无法正确运行它。

这是我的“奴隶”代码:

import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.net.*;
import net.wimpi.modbus.procimg.*;
import net.wimpi.modbus.ModbusCoupler;

public class UDPSlaveTest { // UDP Slave
public static void main(String[] args) {
    /* The important instances and variables */
    ModbusUDPListener listener = null;
    SimpleProcessImage spi = null;
    int port = Modbus.DEFAULT_PORT;

    //1. Set port number from commandline parameter
    if(args != null && args.length ==1) {
        port = Integer.parseInt(args[0]);
    }

    //2. Prepare a process image
    spi = new SimpleProcessImage();
    spi.addDigitalOut(new SimpleDigitalOut(true));
    spi.addDigitalOut(new SimpleDigitalOut(false));
    spi.addDigitalIn(new SimpleDigitalIn(false));
    spi.addDigitalIn(new SimpleDigitalIn(true));
    spi.addDigitalIn(new SimpleDigitalIn(false));
    spi.addDigitalIn(new SimpleDigitalIn(true));
    spi.addRegister(new SimpleRegister(251));
    spi.addInputRegister(new SimpleInputRegister(45));

    //3. Prepare coupler holding image reference
    ModbusCoupler.getReference().setProcessImage(spi);
    ModbusCoupler.getReference().setMaster(false);
    ModbusCoupler.getReference().setUnitID(15);

    //4. Create a listener with 3 threads in pool
    listener = new ModbusUDPListener();
    listener.setPort(port);
    listener.start();
}
}

这是我的“主人”代码:

import java.net.*;
import net.wimpi.modbus.*;
import net.wimpi.modbus.msg.*;
import net.wimpi.modbus.io.*;
import net.wimpi.modbus.net.*;

public class UDPDITest { // UDP Master
public static void main(String[] args) throws Exception {
   /* The important instances of the classes mentioned before */
    UDPMasterConnection con = null; //the connection
    ModbusUDPTransaction trans = null; //the transaction
    ReadInputDiscretesRequest req = null; //the request
    ReadInputDiscretesResponse res = null; //the response

    /* Variables for storing the parameters */
    InetAddress addr = null; //the slave's address
    int port = Modbus.DEFAULT_PORT;
    int ref = 0; //the reference; offset where to start reading from
    int count = 0; //the number of DI's to read
    int repeat = 1; //a loop for repeating the transaction

    //1. Setup the parameters
    if (args.length < 3) {
        System.exit(1);
    } else {
        try {
            String astr = args[0];
            int idx = astr.indexOf(':');
            if(idx > 0) {
                port = Integer.parseInt(astr.substring(idx+1));
                astr = astr.substring(0,idx);
            }
            addr = InetAddress.getByName(astr);
            ref = Integer.decode(args[1]).intValue();
            count = Integer.decode(args[2]).intValue();
            if (args.length == 4) {
                repeat = Integer.parseInt(args[3]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(1);
        }
    }

    //2. Open the connection
    con = new UDPMasterConnection(addr);
    con.setPort(port);
    con.connect();

    //3. Prepare the request
    req = new ReadInputDiscretesRequest(ref, count);

    //4. Prepare the transaction
    trans = new ModbusUDPTransaction(con);
    trans.setRequest(req);

    //5. Execute the transaction repeat times
    int k = 0;
    do {
        trans.execute();
        res = (ReadInputDiscretesResponse) trans.getResponse();
        System.out.println("Digital Inputs Status=" + res.getDiscretes().toString());
        k++;
    } while (k < repeat);

    //6. Close the connection
    con.close();
}//main
}//class UDPDITest

我使用参数"5555"从IntelliJ IDEA运行UDPSlaveTest。当它运行时,我使用参数"localhost:5555 0 4 3"从IntelliJ IDEA运行ModbusTCPMaster,但我得到了:

Exception in thread "main" java.lang.NullPointerException
at jamod_official.UDPDITest.main(UDPDITest.java:65)

因此“奴隶”没有任何回应。

请帮助我,如何正确连接?

1 个答案:

答案 0 :(得分:0)

在我看来,开发人员在使用某些协议库时,不应编写太多代码来连接设备。

尝试使用Modbus Modern API - 来自www.daacoworks.com的Java。