2015-09-12 30 views
0

我有這樣的代碼是從凱譜華OPC服務器讀OPC值Utgard

package opcread; 
import java.util.concurrent.Executors; 
import org.jinterop.dcom.common.JIException; 
import org.openscada.opc.lib.common.ConnectionInformation; 
import org.openscada.opc.lib.da.AccessBase; 
import org.openscada.opc.lib.da.DataCallback; 
import org.openscada.opc.lib.da.Item; 
import org.openscada.opc.lib.da.ItemState; 
import org.openscada.opc.lib.da.Server; 
import org.openscada.opc.lib.da.SyncAccess; 

public class OPCRead { 


    public static void main(String[] args) throws Exception { 
     // TODO code application logic here 
     final ConnectionInformation ci = new ConnectionInformation(); 
     ci.setHost("localhost"); 
     ci.setDomain("MYDOMAIN"); 
     ci.setUser("MY_COMPUTER_USERNAME"); 
     ci.setPassword("MY_COMPUTER_PASSWORD"); 
     ci.setProgId("Kepware.KEPServerEX.V5\\MP.ANC1._System._Mode"); 
     ci.setClsid("B3AF0BF6-4C0C-4804-A122-6F3B160F4397"); 
     final String itemId = "_System._Time_Second"; 

     final Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor()); 

     try 
     { 
      server.connect(); 

      final AccessBase access = new SyncAccess(server, 500); 
      access.addItem(itemId, new DataCallback(){ 
       @Override 
       public void changed(Item item, ItemState state){ 
        System.out.println("Data change " + item + " : " + state); 
       } 

      }); 

      access.bind(); 

      Thread.sleep(10*1000); 

      access.unbind(); 
     } 
     catch(final JIException e) 
     { 
      System.out.println("Errorrrrrrrr : " + String.format("%08X: %s", e.getErrorCode(),server.getErrorMessage(e.getErrorCode()))); 
     } 
     catch(Exception ex) 
     { 
      System.out.println("Errorrrrrrrr : " + ex.getMessage()); 
     } 
    } 

} 

我想在_Mode標籤讀取當前值讀取值。我還給出了Kepware.KEPServerEX.V5\\MP.ANC1._System._Mode以上的完整路徑。但結果是,它不顯示值出現在標籤即userRate,而這是給下面的消息

Sep 12, 2015 9:10:57 PM rpc.DefaultConnection processOutgoing 
INFO: 
Sending REQUEST 
Sep 12, 2015 9:10:57 PM rpc.DefaultConnection processIncoming 
INFO: 
Recieved RESPONSE 
Data change [email protected] : Value: [[[email protected]]], Timestamp: Sat Sep 12 21:10:57 IST 2015, Quality: 192, ErrorCode: 00000000 

在地方[email protected],它應打印的價值,但得到此消息。我在這裏做什麼錯?

回答

1

你需要從變量值明確,這樣的:

access.addItem(itemId, new DataCallback() { 
    @Override 
    public void changed(Item item, ItemState state) { 
     System.out.println("Data change " + state.getObjectAsUnsigned().getValue() + " : " + state); 
    } 
}); 
+0

我使用'state.getObjectAsUnsigned()時收到錯誤的getValue()'但使用'state.getValue(當沒有得到錯誤).getObjectAsUnsigned()'。現在也可以通過'data change [email protected]獲取響應:Value:org.jinterop.dcom.core.JIUnsignedInteger @ 11d4b2e' –

+0

更改爲'state.getValue()。getObjectAsUnsigned() .getValue()。toString()'我不是getiing'org.jinterop.dcom.core.JIUnsignedInteger @ 11d4b2e',而是我得到的數字像23,24,25。但'Kepware.KEPServerEX.V5 \\ MP.ANC1._System._Mode'包含字符串值。我期待着那樣。爲什麼我獲得數字? –

+0

因爲您使用帶項目ID「_System._Time_Second」的'addItem',您可以獲取數字。所以你得到秒。 –