尝试使用Jamod从电表Modbus TCP读取数据

时间:2018-06-20 18:20:09

标签: java modbus-tcp

我正在尝试从功率计cck 7550s或cck4400读取数据,并且在尝试执行事务时遇到异常:

我的代码是:

 package teste;

    import java.net.InetAddress;
    import java.net.UnknownHostException;

    import net.wimpi.modbus.ModbusException;
    import net.wimpi.modbus.ModbusIOException;
    import net.wimpi.modbus.ModbusSlaveException;
    import net.wimpi.modbus.io.ModbusTCPTransaction;
    import net.wimpi.modbus.msg.ReadMultipleRegistersRequest;
    import net.wimpi.modbus.msg.ReadMultipleRegistersResponse;
    import net.wimpi.modbus.net.TCPMasterConnection;

    public class teste {
        public static void main(String[] args) throws Exception {

            String address = "ip"; // my device ip adress

            TCPMasterConnection conn = null;

            try {
                conn = new TCPMasterConnection(InetAddress.getByName(address));
                conn.connect();
            } catch (UnknownHostException e) {
                throw new Exception("IP is not reachabled.{" + address + "}", e);
            } catch (Exception e) {
                throw new Exception(e.getMessage(), e);
            }

            ModbusTCPTransaction transaction = new ModbusTCPTransaction(conn);
            transaction.setReconnecting(true);

            ReadMultipleRegistersResponse response = null;
            ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest();
            request.setTransactionID();
            request.setProtocolID();
            request.setUnitID(0);
            request.setReference(1048);
            request.setWordCount(2);



            try {
                transaction.setRequest(request);
                transaction.execute();  //error here, when trying to read the data
                response = (ReadMultipleRegistersResponse) transaction.getResponse();

                for (int n = 0; n < response.getWordCount(); n++) {
                    System.out.println("Word " + n + "=" + response.getRegisterValue(n));
                    System.out.println(response.getHexMessage());
                    System.out.println(response.getFunctionCode());
                }

            } catch (ModbusIOException e) {
                e.printStackTrace();

            } catch (ModbusSlaveException e) {
                e.printStackTrace();

            } catch (ModbusException e) {
                e.printStackTrace();
            }

        }
    }

错误:

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 teste.teste.main(teste.java:45)
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 teste.teste.main(teste.java:45)
java.net.SocketException: Connection reset
    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 teste.teste.main(teste.java:45)
net.wimpi.modbus.ModbusIOException: Executing transaction failed (tried 3 times)
    at net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:197)
    at teste.teste.main(teste.java:45)

我需要从电表读取所有数据,以便我可以对其进行管理,并感谢所有帮助。 我可以在没有电源的情况下读取电表,但是现在我正在实际测量的电表上进行测试,但出现此异常。

0 个答案:

没有答案