Menu

Update Data very slow

Giuseppe
2017-06-12
2017-06-13
  • Giuseppe

    Giuseppe - 2017-06-12
        Thread threadInit = new Thread(new Runnable() {
    
            public void run() {
                Message msg = null;
                enExecution = true;
                Integer i = 0;
                while (enExecution) {
                    try {
                        i++;
                        readDataMb();
                        Thread.sleep(1000); //mmsec Refresh data
                    } catch (InterruptedException e) {
                        Log.d("[Android]Main", e.getMessage());
                    }
                }
            }
        });
        threadInit.start();
    

        private void readDataMb() {
    
        String astr = "192.168.3.88"; //Modbus Device
        InetAddress addr = null;
        try {
            addr = InetAddress.getByName(astr);
        } catch (UnknownHostException e) {
            e.printStackTrace();
            System.exit(1);
        }
        int port = 502;
    
        TCPMasterConnection con = new TCPMasterConnection(addr);
    
        con.setPort(port);
        try {
            con.connect();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }
    
        try {
            ModbusTCPTransaction trans = null; // the transaction
    
            String refe = "4000";// HEX Address
            int ref = Integer.parseInt(refe, 16);// Hex to int
            int count = 2; // the number Address to read
    
            ReadMultipleRegistersRequest Req = new ReadMultipleRegistersRequest(
                    ref, count);
            ReadMultipleRegistersResponse Res = new ReadMultipleRegistersResponse();
    
            // 3. Start Transaction
            trans = new ModbusTCPTransaction(con);
            trans.setRetries(5);
            trans.setReconnecting(true);
            trans.setRequest(Req);
    
            int k = 0;
            do {
                trans.execute();
                Res = (ReadMultipleRegistersResponse) trans.getResponse();
                Temp_Room = (Res.getRegisterValue(0));
                k++;
            } while (k < count);
        } catch (ModbusIOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ModbusSlaveException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ModbusException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

    Theoretically my thread should be updated every second but in reality it is not, it is very slow but I don't no which is the problem.
    Someone can help me?

     
  • John D Charlton

    John D Charlton - 2017-06-12

    You can try to reduce your sleep time so that the read operation is able to work more frequently. As long as you have some sleep time in the loop it will prevent it from spinning and using up all of the processor time. Also you probably want to open the TCP socket in a separate method and leave it open for the duration rather than reconnecting every second. That is probably where all the extra time is going.

    John

     
  • Giuseppe

    Giuseppe - 2017-06-13

    Hi John, do you have an exampe with refresh update of 1 second?

     
  • Giuseppe

    Giuseppe - 2017-06-13

    I solved, the problem was that the modbus data simulator (http://www.plcsimulator.org/) was set with number of server connection 10, changing this parameter with 1000 the app works fine.

     

Log in to post a comment.