博客
关于我
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/

你可能感兴趣的文章
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>