如何通过jamod读取控制器的输入状态?

时间:2020-03-05 14:10:08

标签: java modbus modbus-tcp

我正在使用控制器进行项目开发,我想通过Java读取输入状态,因此我添加了一个支持modbusTCP / IP协议的jamod库。

但是,当您运行程序时,会出现此异常。 此异常是什么意思,我如何读取输入状态?

那是程序:

    public static void main(String[] args) throws IOException {

        //JAMOD

        TCPMasterConnection con = null; //the connection
        ModbusTCPTransaction 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 = 10; //the number of DI's to read
        int repeat = 1; //a loop for repeating the transaction

        try {
            addr = InetAddress.getByName("192.168.1.5");
            port = 50002; 
            // (TCP server port from the ChatServer example, run the ChatServer to connect to a random TCP server)
        } catch(UnknownHostException e) {
            System.out.println(e);
        }

        try {
            con = new TCPMasterConnection(addr);
            con.setPort(port);
            con.connect();
        } catch (Exception e) {
            System.out.println(e);
        }

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

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

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

        while (k < repeat);
            //6. Close the connection
            con.close();
            System.out.println("done");
        }

例外:

    java.io.EOFException
    at java.io.DataInputStream.readUnsignedByte(Unknown Source)
    at net.wimpi.modbus.io.BytesInputStream.readUnsignedByte(BytesInputStream.java:153)
    at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:182)
    at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193)
    at Port.portscanner.main(portscanner.java:62)

net.wimpi.modbus.ModbusIOException:执行事务失败(尝试3次) 完成

0 个答案:

没有答案