package org.example.opc;
|
|
import org.jinterop.dcom.common.JIException;
|
import org.openscada.opc.lib.common.ConnectionInformation;
|
import org.openscada.opc.lib.da.*;
|
|
|
import java.util.concurrent.Executors;
|
|
|
import static org.example.opc.conf.OPCConfiguration.getCLSIDConnectionInfomation;
|
|
/**
|
* 功能:OPC通信线程 描述:通过循环读取心跳和状态控制字,按照商议好的交互流程读写变量
|
*/
|
public class OPCComm {
|
|
private Server server;
|
|
|
private Item ZYYSTART;//开始
|
private Item ZYYEND;//结束
|
private Item P1;//心跳
|
|
|
/**
|
* 单例模式
|
*/
|
private static class SingletonHolder {
|
static final OPCComm doOPCComm = new OPCComm();
|
}
|
public static OPCComm getInstance() {
|
return SingletonHolder.doOPCComm;
|
}
|
/**
|
* 启动server 创建一个监控线程 创建一个写入线程
|
*/
|
public void init() throws Exception {
|
|
|
// 加载配置文件
|
final ConnectionInformation ci = getCLSIDConnectionInfomation();
|
// 创建server
|
final Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
|
|
try {
|
// 启动server
|
server.connect();
|
GlobalVariable.textArea.append("OPC连接成功………………………………………………"+"\r\n");
|
this.server = server;
|
// 同步读取,500ms一次
|
final AccessBase access = new SyncAccess(server, 500);
|
|
//OP010组
|
final Group OP010_GROUP = server.addGroup("OP140_ZYY");
|
ZYYSTART = OP010_GROUP.addItem((OPCElement.ZYYSTART));//心跳
|
ZYYEND = OP010_GROUP.addItem((OPCElement.ZYYEND));//心跳
|
P1 = OP010_GROUP.addItem((OPCElement.P1));//心跳
|
|
// start reading
|
access.bind();
|
} catch (final JIException e) {
|
GlobalVariable.textArea.append("OPC连接失败………………………………………………"+"\r\n");
|
GlobalVariable.textArea.append(e.getMessage()+"\r\n");
|
Thread.sleep(1000*10);
|
//init();
|
System.out.println(String.format("%08X: %s", e.getErrorCode(), server.getErrorMessage(e.getErrorCode())));
|
}
|
}
|
|
public Server getServer() {
|
return server;
|
}
|
|
public Item getZYYSTART() {
|
return ZYYSTART;
|
}
|
|
public Item getZYYEND() {
|
return ZYYEND;
|
}
|
|
public Item getP1() {
|
return P1;
|
}
|
}
|