libnodave java - duration of "read" operation

1.8k Views Asked by At

I have made a little program in java (with NetBean) that uses libnodave libraries libnodave-java.0.1 for reading a byte every 500ms (Interval). I was disappointed by the reading operation duration, that is around 250ms (dt).

Graphic Interface

This is my main class. As you can see the connection request is performed only once at the 9th line:

private int area;
private long secondo=0;
public MachCtrl() {  
    initComponents(); 
    Mradio.setSelected(true);                                
    db.setText("0");
    db.setEnabled(false);
    area=Nodave.FLAGS;
    DataIsoTCP.Start("172.17.5.31");
    ActionListener listener = new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent evt) {
            long start, elapsedTime;
            if(type.getSelectedItem().toString()=="float"){                           
                start = System.nanoTime(); 
                float r=(float)DataIsoTCP.Read(area, type.getSelectedItem().toString(), Integer.parseInt(db.getText()), Integer.parseInt(address.getText()), Integer.parseInt(bit.getSelectedItem().toString()));
                elapsedTime = System.nanoTime() - start;
                value.setText(Float.toString(r)); 
                dt.setText(Long.toString(Math.round(elapsedTime/1000000)));
            }                                      
            if(type.getSelectedItem().toString()=="double"){
                start = System.nanoTime(); 
                long r=(long)DataIsoTCP.Read(area, type.getSelectedItem().toString(), Integer.parseInt(db.getText()), Integer.parseInt(address.getText()), Integer.parseInt(bit.getSelectedItem().toString()));
                elapsedTime = System.nanoTime() - start;
                value.setText(Long.toString(r)); 
                dt.setText(Long.toString(Math.round(elapsedTime/1000000)));
            }                                      
            if(type.getSelectedItem().toString()=="word"){
                start = System.nanoTime(); 
                long r=(long)DataIsoTCP.Read(area, type.getSelectedItem().toString(), Integer.parseInt(db.getText()), Integer.parseInt(address.getText()), Integer.parseInt(bit.getSelectedItem().toString()));
                elapsedTime = System.nanoTime() - start;
                value.setText(Long.toString(r)); 
                dt.setText(Long.toString(Math.round(elapsedTime/1000000)));
            }                                      
            if(type.getSelectedItem().toString()=="byte"){
                start = System.nanoTime(); 
                byte r=(byte)DataIsoTCP.Read(area, type.getSelectedItem().toString(), Integer.parseInt(db.getText()), Integer.parseInt(address.getText()), Integer.parseInt(bit.getSelectedItem().toString()));
                elapsedTime = System.nanoTime() - start;
                value.setText(Integer.toString(r)); 
                dt.setText(Long.toString(Math.round(elapsedTime/1000000)));
            }                                      
            if(type.getSelectedItem().toString()=="bit"){
                start = System.nanoTime(); 
                byte r=(byte)DataIsoTCP.Read(area, type.getSelectedItem().toString(), Integer.parseInt(db.getText()), Integer.parseInt(address.getText()), Integer.parseInt(bit.getSelectedItem().toString()));
                elapsedTime = System.nanoTime() - start;
                value.setText(Integer.toString(r)); 
                dt.setText(Long.toString(Math.round(elapsedTime/1000000)));
            }
            interval.setText(Long.toString((System.nanoTime() - secondo)/1000000));
            secondo = System.nanoTime();
        }
    };
    Timer timert = new Timer(500,listener);
    timert.start();
}

And this is the DataIsoTCP class, that contains the read function. As you can see is simply a "readBytes()" request, there are no heavy operations that could delay the code execution.

package examples;

import PLC.Nodave;
import PLC.PLCinterface;
import PLC.TCPConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class DataIsoTCP {

    public static boolean Connection = false;
    private static int i, j;
    public static byte b, b1, b2, b3;
    public static long a, c;
    //private static float d, e, f;
    private static char buf[];
    public static byte buf1[];
    public static PLCinterface di;
    public static TCPConnection dc;
    public static Socket sock;
    private static int slot;
    public static byte[] by;
    public static String IP;

    DataIsoTCP(String host) {
        IP = host;
        //Nodave.Debug=Nodave.DEBUG_ALL;
        buf = new char[Nodave.OrderCodeSize];
        buf1 = new byte[Nodave.PartnerListSize];
        try {
            sock = new Socket(host, 102);
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public static void Start(String adres) {
        Nodave.Debug=Nodave.DEBUG_ALL^(Nodave.DEBUG_IFACE|Nodave.DEBUG_SPECIALCHARS);

        DataIsoTCP tp = new DataIsoTCP(adres);
        DataIsoTCP.StartConnection();
    }

    public static void StartConnection() {
        Connection = false;
        OutputStream oStream = null;
        InputStream iStream = null;
        slot = 2;

        if (sock != null) {
            try {
                oStream = sock.getOutputStream();
            } catch (IOException e) {
                System.out.println(e);
            }
            try {
                iStream = sock.getInputStream();
            } catch (IOException e) {
                System.out.println(e);
            }
            di = new PLCinterface(
                    oStream,
                    iStream,
                    "IF1",
                    0,
                    Nodave.PROTOCOL_ISOTCP);

            dc = new TCPConnection(di, 0, slot);
            int res = dc.connectPLC();
            if (0 == res) {
                Connection = true;
                System.out.println("Connection OK ");
            } else {
                System.out.println("No connection");
            }
        }
    }

    public static void StopConnection() {
        if (Connection == true) {
            Connection = false;
            dc.disconnectPLC();
            di.disconnectAdapter();
        }
    }

    public static float Read(int area, String type, int db, int address, int bit) {
        int bytes;
        if("float".equals(type)){
            float r=0;
            bytes=4;
            int res = dc.readBytes(area, db, address, bytes, null);
            if(res==0){
                r=dc.getFloat();
            }
            return r;       
        }
        else if("double".equals(type)){
            float r=0;
            bytes=4;
            int res = dc.readBytes(area, db, address, bytes, null);
            if(res==0){
                r=dc.getU32();
            }
            return r;      
        }
        else if("word".equals(type)){
            int r=0;
            bytes=2;
            int res = dc.readBytes(area, db, address, bytes, null);
            if(res==0){
                r=dc.getWORD();
            }
            return r;      
        }
        else if("byte".equals(type)){
            int r=0;
            bytes=1;
            int res = dc.readBytes(area, db, address, bytes, null);
            if(res==0){
                r=dc.getBYTE();
            }
            return r;      
        }
        else if("bit".equals(type)){
            int r=0;
            bytes=1;
            int res = dc.readBytes(area, db, address, bytes, null);
            if(res==0){
                r=dc.getBYTE();
            }
            return (r >> bit)&1;    
        }else{
            return 0;
        }
    }    
}

I would need to read data at least every 250ms, then the duration of the read operation should be halved. Someone managed to accelerate this operation? Thankyou for your help! Stefano

1

There are 1 best solutions below

0
On BEST ANSWER

I've tried also with VB libraries, with the same result. I've tried with both PLCSIM and IM151-8. The result is the same. I've tried also simulating CPU315 and CPU317 without success. After contacting Siemens support, it seems to be a PLC response delay, introduced by PLC cyclic operation (maybe for safeguarding Siemens products). This answer didn't satisfied me, so I'm going to try different ways for having PLC response quickly.