使用jamod库

时间:2016-10-04 07:41:34

标签: java exception

我使用jamod库实现了TCP Modbus Master 代码请求从Modbus保存寄存器并读取响应并将其写入文件。它还计算交易时间 这是我的代码:

public class TCPModbus_Task {
    private static ModbusTCPMaster tcp_Modbusmaster;

    public static void main(String[] args) {
        int NoOfRequests = 0;// loop variable
        int port = Modbus.DEFAULT_PORT;
        int ref = 0; //the reference; offset where to start reading from i.e. the start address.
        int count = 0; //the number of DI's to read
        int repeat = 1; //a loop for repeating the transaction
        String addr = null;
        long Avg_diff = 0;
        int reg_val = 0;
        Register[] registers = new Register[200];
        BufferedWriter output = null;
        long Avg_Time = 0;
        try {
            //Setup Parameters
            if(args.length < 3)
                System.exit(1);
            else {
                try {
                    String astr = args[0];
                    int idx = astr.indexOf(':');
                    if(idx != -1) {
                        port = Integer.parseInt(astr.substring(idx+1));
                        addr = astr.substring(0, idx);
                    }
                    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);
                }
            }

            //ModbusTCPMaster instance ( constructor called )
            tcp_Modbusmaster = new ModbusTCPMaster(addr, port);

            //Connect to slave
            tcp_Modbusmaster.connect();

            //Create file write modbus data
            File file = new File("D:/MODbusData.txt");
            if (!file.exists()) {
                     file.createNewFile();
            }


            do {
                    Thread.sleep(1000);

                    //TimeStamp before request
                    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+5.30"));
                    long time1 = cal.getTimeInMillis();
                    String Time1 = String.valueOf(time1);
                    System.out.println("Time1 = " + time1);         


                    //Read Modbus register (function code = 3)
                    registers = tcp_Modbusmaster.readMultipleRegisters(ref, count, 255);

                    System.out.println("Number of Modbus registers : " + registers.length);
                    System.out.println("\nModbus register values : \n");
                    for (int i=0; i<registers.length; i++)
                    {
                        System.out.println(registers[i].getValue());
                    }

                    //TimeStamp After response
                    Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("GMT+5.30"));
                    long time2 = cal1.getTimeInMillis();
                    String Time2 = String.valueOf(time2);

                    System.out.println("Time2 = " + time2);
                    int diff = (int)(time2-time1);
                    String Transac_time = String.valueOf(diff);
                    System.out.println("Transacion(time2 - time1) time in ms=" + diff);
                    NoOfRequests++;
                    Avg_diff = Avg_diff + (long)diff;

                    //Writing Modbus data and transaction timing to file
                    output = new BufferedWriter(new FileWriter(file,true));

                    output.write("Time1 in ms:  " + Time1 + "\n\r");
                    output.write("Modbus data: \n");
                    for(int i=0;i<registers.length;i++)
                    {
                        reg_val = registers[i].getValue();
                        String Register_value = String.valueOf(reg_val);                                                                        
                        output.write(Register_value + " ");
                    }
                    output.write("\n" + "Time2 in ms:  " + Time2 + "\n\r");
                    output.write("Transaction time in ms:  " + Transac_time + "\n");

                    //Calculating the Average Transaction Time
                    if(NoOfRequests>=repeat)
                    {
                        Avg_Time = Avg_diff/NoOfRequests;
                        String AVG_Time = String.valueOf(Avg_Time);
                        output.write("Avg Transaction time in ms:  " + AVG_Time + "\n\r");                          
                    }

                    output.close();

            }while(NoOfRequests < repeat);

            tcp_Modbusmaster.disconnect();



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

但是如果我将计数值(即从从站读取的字(寄存器)的数量)大于123,我想从Modbus从站读取200个保持寄存器(具有Modbus映射)。我可以毫无例外地在一个请求中读取count = 123寄存器。

它给出以下例外:

java.lang.IndexOutOfBoundsException
    at java.io.BufferedInputStream.read(Unknown Source)
    at java.io.DataInputStream.read(Unknown Source)
    at net.aeplmm.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:195)
    at net.aeplmm.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:192)
    at net.aeplmm.modbus.facade.ModbusTCPMaster.readMultipleRegisters(ModbusTCPMaster.java:282)
    at com.axonet.modbus.TCPModbus_Task.main(TCPModbus_Task.java:79)
java.io.EOFException
    at java.io.DataInputStream.readUnsignedByte(Unknown Source)
    at net.aeplmm.modbus.io.BytesInputStream.readUnsignedByte(BytesInputStream.java:169)
    at net.aeplmm.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:200)
    at net.aeplmm.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:192)
    at net.aeplmm.modbus.facade.ModbusTCPMaster.readMultipleRegisters(ModbusTCPMaster.java:282)
    at com.axonet.modbus.TCPModbus_Task.main(TCPModbus_Task.java:79)
java.io.EOFException
    at java.io.DataInputStream.readUnsignedByte(Unknown Source)
    at net.aeplmm.modbus.io.BytesInputStream.readUnsignedByte(BytesInputStream.java:169)
    at net.aeplmm.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:200)
    at net.aeplmm.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:192)
    at net.aeplmm.modbus.facade.ModbusTCPMaster.readMultipleRegisters(ModbusTCPMaster.java:282)
    at com.axonet.modbus.TCPModbus_Task.main(TCPModbus_Task.java:79)
java.io.EOFException
    at java.io.DataInputStream.readUnsignedByte(Unknown Source)
    at net.aeplmm.modbus.io.BytesInputStream.readUnsignedByte(BytesInputStream.java:169)
    at net.aeplmm.modbus.io.ModbusTCPTransport.readResponse(ModbusTCPTransport.java:200)
    at net.aeplmm.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:192)
    at net.aeplmm.modbus.facade.ModbusTCPMaster.readMultipleRegisters(ModbusTCPMaster.java:282)
    at com.axonet.modbus.TCPModbus_Task.main(TCPModbus_Task.java:79)
net.aeplmm.modbus.ModbusIOException: Executing transaction failed (tried 3 times)
    at net.aeplmm.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:197)
    at net.aeplmm.modbus.facade.ModbusTCPMaster.readMultipleRegisters(ModbusTCPMaster.java:282)
    at com.axonet.modbus.TCPModbus_Task.main(TCPModbus_Task.java:79)

如果有人有任何建议会有所帮助。

0 个答案:

没有答案