libnodave java“读取”操作的持续时间

libnodave java“读取”操作的持续时间,java,plc,libnodave,Java,Plc,Libnodave,我用java和NetBean编写了一个小程序,它使用libnodave库每隔500毫秒读取一个字节。我对读取操作的持续时间感到失望,即大约250ms dt 这是我的主课。如您所见,连接请求仅在第9行执行一次: private int area; private long secondo=0; public MachCtrl() { initComponents(); Mradio.setSelected(true);

我用java和NetBean编写了一个小程序,它使用libnodave库每隔500毫秒读取一个字节。我对读取操作的持续时间感到失望,即大约250ms dt

这是我的主课。如您所见,连接请求仅在第9行执行一次:

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();
}
这是DataIsoTCP类,它包含read函数。正如您所看到的,这只是一个readBytes请求,没有可能延迟代码执行的繁重操作

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;
        }
    }    
}
我需要至少每250ms读取一次数据,然后读取操作的持续时间应该减半。有人设法加速了这一行动? 谢谢你的帮助!
Stefano

我也尝试过使用VB库,得到了相同的结果。我试过PLCSIM和IM151-8。结果是一样的。我也尝试过模拟CPU315和CPU317,但没有成功。联系西门子支持部门后,似乎是PLC响应延迟,由PLC循环操作引入,可能是为了保护西门子产品。这个答案让我不满意,所以我将尝试不同的方法让PLC快速响应。

SourceForge网站上有其他用户抱怨速度慢。可能是库效率低下,或者设备通信本身就很慢。