2014-12-04 59 views
0

我在java中使用libnodave庫libnodave-java.0.1每500ms(間隔)讀取一個字節的java程序(使用NetBean)。閱讀操作持續時間令我感到失望,即大約250ms(dt)。libnodave java - 「讀取」操作的持續時間

Graphic Interface

這是我的主類。正如你所看到的連接請求在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類,包含讀取功能。正如你所看到的只是一個「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

+1

SourceForge網站上的其他用戶發表評論抱怨緩慢。可能是庫效率不高,或者設備通信本身就很慢。 – 2014-12-04 16:12:22

回答

0

我已經嘗試過與VB庫,也有同樣的結果。我已經嘗試了PLCSIM和IM151-8。結果是一樣的。我也試過模擬CPU315和CPU317沒有成功。在聯繫西門子支持後,它似乎是由PLC循環操作(可能用於保護西門子產品)引入的PLC響應延遲。這個答案並不令我滿意,所以我將嘗試不同的方式來快速響應PLC。