有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在使用一个控制器进行一个项目,我想通过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)

净。威皮。modbus。ModbusIOException:执行事务失败(尝试了3次) 完成


共 (0) 个答案