2014-12-04 2 views
0

libnodave 라이브러리를 사용하는 java (비트)에서 500ms (간격)마다 바이트를 읽는 작은 프로그램을 만들었습니다. 나는 약 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 사용자 사이트에있는 다른 사용자의 의견이 저속이라는 불평이 있습니다. 어쩌면 라이브러리가 비효율적이거나 장치 통신이 본질적으로 느릴 수 있습니다. –

답변

0

동일한 결과로 VB 라이브러리에서도 시도했습니다. 나는 PLCSIM과 IM151-8 모두 시도했다. 결과는 같습니다. 나는 성공없이 CPU315와 CPU317을 시뮬레이션하려고 시도했다. Siemens 지원팀에 문의 한 후 PLC주기 지연 (Siemens 제품 보호를위한 것일 수도 있음)에 의해 도입 된 PLC 응답 지연 인 것으로 보입니다. 이 대답은 나를 만족시키지 못했기 때문에 신속하게 PLC를 응답 할 수있는 다양한 방법을 시도 할 것입니다.