首先导包jeasyOPC.jar包 配置JCustomOpc.dll文件
测试代码如下:
//读取OPC数据
private void testOPC() throws Exception {
JOpc.coInitialize();//ICONICS.SimulatorOPCDA.2
JOpc jopc = new JOpc("localhost", "OMRON.OPC", "lxxxxxx");//第一个参数是本机地址,第一个参数是OPC服务器名,第三个参数是本机电脑用户名
OpcItem item1 = new OpcItem("Device.G.A", true, "");
// OpcItem item2 = new OpcItem("Random.Real8", true, "");
// OpcItem item3 = new OpcItem("Random.Real8", true, "");
OpcGroup group = new OpcGroup("Device", true, 10, 0.0f);//Address Space
//
group.addItem(item1);
// group.addItem(item2);
// group.addItem(item3);
jopc.addGroup(group);
jopc.connect();
System.out.println("JOPC client is connected...");
jopc.registerGroup(group);
jopc.updateGroups();//没有更新可能报错
jopc.registerItem(group, item1);
System.out.println("OPCGroup are registered...");
synchronized (JeasyopcTest06.class) {
JeasyopcTest06.class.wait(2000);
}
// Synchronous reading of group
int cycles = 10;
int acycle = 0;
/**
* 设置读取30次
*/
while (acycle++ < cycles) {
/**
* 设置每2秒钟读取一次
*/
synchronized (JeasyopcTest06.class) {
JeasyopcTest06.class.wait(2000);
}
OpcGroup responseGroup = jopc.synchReadGroup(group);
System.out.println(responseGroup);
}
JOpc.coUninitialize();
}