博客
关于我
Java实现OPCUA数据监听与写入
阅读量:636 次
发布时间:2019-03-14

本文共 5040 字,大约阅读时间需要 16 分钟。

优化后的文章

OP C服务监听与数据处理

在Java项目中,依托Spring框架搭建OPC客户端,实现对OPC服务端的监听与数据处理。通过Spring注入简化配置管理,确保系统稳定运行。

配置管理

OPC服务配置信息存储于application.yml,配置内容包括:

opc:  host: 192.168.1.205  domain: user: OPCUser  password: 123456  progId: Kepware.KEPServerEX.V6  itemIdList:    - Memorybase.Device01.TAG01    - Memorybase.Device01.TAG02    - Memorybase.Device01.TAG03

Spring容器注入

利用SpringUtil类实现Bean注入,支持通过类名或名称获取Spring Bean,保障模块间高效通信。其具体实现如下:

package com.util;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;@Componentpublic class SpringUtil implements ApplicationContextAware {    private static ApplicationContext applicationContext;    @Override    public void setApplicationContext(ApplicationContext applicationContext) {        if (SpringUtil.applicationContext == null) {            SpringUtil.applicationContext = applicationContext;        }    }    public static ApplicationContext getApplicationContext() {        return applicationContext;    }    public static Object getBean(String name) {        return getApplicationContext().getBean(name);    }    public static 
T getBean(Class
clazz) { return getApplicationContext().getBean(clazz); } public static
T getBean(String name, Class
clazz) { return getApplicationContext().getBean(name, clazz); }}

OPC数据存储

采用OpcMap类存储OPC数据,提供便捷的读写接口。其核心代码如下:

package com.opc;import java.util.HashMap;import java.util.Map;public class OpcMap {    private static Map
opcMap = new HashMap<>(); public static void putMap(String itemId, Object value) { opcMap.put(itemId, value); } public static Object getMap(String itemId) { return opcMap.get(itemId); }}

OPC客户端开发

基于OpcClient类实现OPC客户端功能,包括连接、数据读写和状态监听。其关键代码如下:

import com.result.Result;import org.jinterop.dcom.common.JIException;import org.openscada.opc.dcom.list.ClassDetails;import org.openscada.opc.lib.common.ConnectionInformation;import org.openscada.opc.lib.da.*;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;public class OpcClient extends Observable {    private Server mServer = null;    private static final Logger LOGGER = LoggerFactory.getLogger(OpcClient.class);    public boolean connectionServer(String host, String progId, String user, String password, String domain) throws UnknownHostException {        // 获取OPC服务器列表并建立连接        ServerList serverList = new ServerList(host, user, password, domain);        ConnectionInformation connectionInfo = new ConnectionInformation();        connectionInfo.setHost(host);        connectionInfo.setClsid(serverList.getClsIdFromProgId(progId));        // 连接服务器并注册状态监听器        mServer = new Server(connectionInfo, Executors.newSingleThreadScheduledExecutor());        mServer.connect();        mServer.addStateListener(new ServerConnectionStateListener() {            @Override            public void connectionStateChanged(boolean connected) {                LOGGER.info("Connection state: {}", connected);            }        });        return true;    }    // ...其他方法如断开连接、显示OPC服务器列表等}

Ugard框架应用

借助UtgardTutorial类集成Utgard框架,实现对OPC数据的读写与监听。其使用示例如下:

import com.opc.OpcClient;import com.opc.OpcMap;import com.opc.OpcProperties;import com.result.Result;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;@Configurationpublic class UtgardTutorial {    private static final Logger LOGGER = LoggerFactory.getLogger(UtgardTutorial.class);    @Autowired    private OpcProperties opc;    public void start() {        OpcClient opcClient = new OpcClient();        // 显示OPC服务器列表        opcClient.showAllOPCServer(opc.getHost(), opc.getUser(), opc.getPassword(), opc.getDomain());        // 连接OPC服务器        try {            boolean ret = opcClient.connectionServer(opc.getHost(), opc.getProgId(), opc.getUser(), opc.getPassword(), opc.getDomain());            if (!ret) {                LOGGER.info("无法连接OPC服务器");                return;            }            // 验证监测点列表            if (!opcClient.checkItemList(opc.getItemIdList())) {                LOGGER.info("缺少监测点");                return;            }            // 注册数据监听            opcClient.subscribe(new Observer() {                @Override                public void update(Observable o, Object arg) {                    Result result = (Result) arg;                    OpcMap.putMap(result.getItemId(), result.getValue());                    LOGGER.info("接收到数据变化: item ID={}, 值={}", result.getItemId(), result.getValue());                    // 进行OPC数据写入操作                    opcClient.writeOPC(result.getValue());                }            });            // 开始异步数据读取            opcClient.readObjectList(opc.getItemIdList(), 500);        } catch (UnknownHostException e) {            LOGGER.error("连接OPC服务器时发生错误", e);        }    }}

以上是对OPC数据监听与处理系统的完整实现,涵盖配置管理、Spring注入、数据存储以及客户端开发等关键环节。

转载地址:http://eemoz.baihongyu.com/

你可能感兴趣的文章
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>
Mongodb学习总结(1)——常用NoSql数据库比较
查看>>
MongoDB学习笔记(8)--索引及优化索引
查看>>
mongodb定时备份数据库
查看>>
mppt算法详解-ChatGPT4o作答
查看>>
mpvue的使用(一)必要的开发环境
查看>>
MQ 重复消费如何解决?
查看>>
mqtt broker服务端
查看>>
MQTT 保留消息
查看>>
MQTT 持久会话与 Clean Session 详解
查看>>
MQTT工作笔记0007---剩余长度
查看>>
MQTT工作笔记0009---订阅主题和订阅确认
查看>>