读取 OPC 值 Utgard

Read OPC Values Utgard

我有这段代码可以从 kepware OPC 服务器读取值

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 org.openscada.opc.lib.da.Item@11d7dda : Value: [[org.jinterop.dcom.core.JIUnsignedInteger@11d4b2e]], Timestamp: Sat Sep 12 21:10:57 IST 2015, Quality: 192, ErrorCode: 00000000

代替 org.jinterop.dcom.core.JIUnsignedInteger@11d4b2e,它应该打印值,但收到此消息。我在这里做错了什么?

您需要明确地从变量中获取值,如下所示:

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