3

我正在尝试使用 Modbus/TCP 从设备的保持寄存器(由 Weidmüller 制造的 PowerMonitor)中读取值。我使用 java 和一个名为“jamod”的 modbus 库。我的代码:

public class Master1 {

    public static void main(String[] args) {

        try {
            TCPMasterConnection con = null; // the connection
            ModbusTCPTransaction trans = null; // the transaction
            ReadMultipleRegistersRequest req = null; // the request
            ReadMultipleRegistersResponse res = null; // the response
            byte[] ip = { (byte) 127, (byte) 0, (byte) 0, (byte) 1 }; // the
                                                                        // powermonitor
                                                                        // actually
                                                                        // has a
                                                                        // different
                                                                        // IP

            InetAddress addr = InetAddress.getByAddress(ip); // the slave's
                                                                // adress

            int port = 502; // Modbus.DEFAULT_PORT;

            int ref = 4; // the reference; offset where to start reading from,
                            // =n-1

            int count = 4; // the number of Reg's to read
            int repeat = 100; // a loop for repeating the transaction

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

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

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

            // 5. Execute the transaction repeat times
            int k = 0;
            do {
                trans.execute();
                res = (ReadMultipleRegistersResponse) trans.getResponse();
                for (int i = 0; i < count; i++) {
                    System.out.println("Status register " + (ref + i + 1)
                            + ": " + res.getRegisterValue(i));
                }
                k++;
            } while (k < repeat);

            // 6. Close the connection
            con.close();

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

    }
}

在eclipse中运行此代码(使用实际IP)时,我在控制台上得到了这个:

java.net.SocketTimeoutException: Read timed out  
    at java.net.SocketInputStream.socketRead0(Native Method)  
    at java.net.SocketInputStream.read(Unknown Source)  
    at java.net.SocketInputStream.read(Unknown Source)  
    at java.io.BufferedInputStream.fill(Unknown Source)  
    at java.io.BufferedInputStream.read1(Unknown Source)  
    at java.io.BufferedInputStream.read(Unknown Source)  
    at java.io.DataInputStream.read(Unknown Source)  
    at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:171)  
    at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193)  
    at Master1.main(Master1.java:44)  
java.net.SocketTimeoutException: Read timed out  
    at java.net.SocketInputStream.socketRead0(Native Method)  
    at java.net.SocketInputStream.read(Unknown Source)  
    at java.net.SocketInputStream.read(Unknown Source)  
    at java.io.BufferedInputStream.fill(Unknown Source)  
    at java.io.BufferedInputStream.read1(Unknown Source)  
    at java.io.BufferedInputStream.read(Unknown Source)  
    at java.io.DataInputStream.read(Unknown Source)  
    at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:171)  
    at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193)  
    at Master1.main(Master1.java:44)  
java.net.SocketTimeoutException: Read timed out  
    at java.net.SocketInputStream.socketRead0(Native Method)  
    at java.net.SocketInputStream.read(Unknown Source)  
    at java.net.SocketInputStream.read(Unknown Source)  
    at java.io.BufferedInputStream.fill(Unknown Source)  
    at java.io.BufferedInputStream.read1(Unknown Source)  
    at java.io.BufferedInputStream.read(Unknown Source)  
    at java.io.DataInputStream.read(Unknown Source)  
    at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:171)  
    at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193)  
    at Master1.main(Master1.java:44)  
java.net.SocketTimeoutException: Read timed out  
    at java.net.SocketInputStream.socketRead0(Native Method)  
    at java.net.SocketInputStream.read(Unknown Source)  
    at java.net.SocketInputStream.read(Unknown Source)  
    at java.io.BufferedInputStream.fill(Unknown Source)  
    at java.io.BufferedInputStream.read1(Unknown Source)  
    at java.io.BufferedInputStream.read(Unknown Source)  
    at java.io.DataInputStream.read(Unknown Source)    
    at net.wimpi.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:171)  
    at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:193)  
    at Master1.main(Master1.java:44)  
net.wimpi.modbus.ModbusIOException: Executing transaction failed (tried 3 times)  
    at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:197)  
    at Master1.main(Master1.java:44)  

当我在本地主机上使用 EasyModbusTCP 服务器模拟器运行这个程序时(如上),没有问题,我得到了存储在保持寄存器中的值。那我做错了什么?我真的不知道该怎么办。我希望我问的方式没问题(我在这里很新)。非常感谢!

4

0 回答 0