由于导师项目的需求,最近自己在负责OPC开发知识的研究。自己对Utgard的数据访问方式进行了总结,与大家分享,学习交流:
- 1.基于opc对象的特征及关系的Utgard的使用—-同步读取某个点位的值(Item项的read()方法)
Server server = new Server(BaseConfiguration.getCLSIDConnectionInfomation(), Executors.newSingleThreadScheduledExecutor());
server.connect();
Group group = server.addGroup();
Item item = group.addItem(“Random.Real5”);
System.out.println(“ItemName:[” + item.getId()+ “],value:” + item.read(false).getValue());
- 2.基于AccessBase的Utgard的使用
(1)使用SyncAccess类隔时间段地进行同步读取数据 ,它实现了Runnable接口,实际上通过另一个线程进行同步读,具体代码如下:
public static void syncRead(Server server) throws Exception {
final String itemId = “Random.Int2”;
// 每隔1秒同步读
AccessBase access = new SyncAccess(server, 1000);
access.addItem(itemId, new DataCallback() {
@Override
public void changed(Item item, ItemState itemState) {
System.out.println(itemState);
}
});
// start reading
access.bind();
// wait a little bit
Thread.sleep(5 * 1000);
// stop reading
access.unbind();
}
(2) 使用Async20Access类隔时间段地进行异步读取数据,它实现了IOPCDataCallback接口,基于事件回调的实现,具体代码如下:
public static void asyncRead(Server server) throws Exception {
final String itemId = “Random.Int2”;
// 第三个参数用于设置初始化时是否执行访问
AccessBase access = new Async20Access(server, 1000, false);
access.addItem(itemId, new DataCallback() {
@Override
public void changed(Item item, ItemState itemState) {
System.out.println(“>>> Asynchronized read: value=”
+ itemState.getValue());
}
});
access.bind();
Thread.sleep(5 * 1000);
access.unbind();
}
本文分享了两种Utgard OPC数据访问方法:一种是基于opc对象特征的同步读取,另一种是基于AccessBase的同步和异步读取。通过具体代码示例介绍了如何实现这些读取操作。
652

被折叠的 条评论
为什么被折叠?



