原函数是opcUaClient.MonitorValue("ns=4;s=MAIN.d", new Action<double, Action>(MonitorTestValueFloat));

由于订阅函的的回调函的类型是

public void MonitorValue<T>(string tag, Action<T, Action> callback);
所以返回的函数参数中只有数值,所以在实际应用中是不够的,另外多个订阅就要加多个回调函数处理。为此需要更改为
 private void MonitorTestValueFloat(object clientHandle, DataValue value)
操作步骤如下:
先继承OpcUaHelper.OpcUaClient 后添加订阅方法 具体代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Client;
namespace 组态
{
/// <summary>
/// 值变化委托函数
/// </summary>
/// <param name="clientHandle">客户处理事件</param>
/// <param name="value">变化的值</param>
public delegate void valueChanged(object clientHandle, DataValue value);
class MyOpcUa:OpcUaHelper.OpcUaClient
{
/// <summary>
/// OPCUA服务器订阅
/// </summary>
private Subscription m_Subscription;

/// <summary>
/// 添加处理函数
/// </summary>
public void addNotificationHandle(int publishingInterval)
{
try
{
Opc.Ua.Client.Subscription innerSubscription = new Opc.Ua.Client.Subscription(this.Session.DefaultSubscription);

innerSubscription.DisplayName = "My Subscription Name";
innerSubscription.PublishingEnabled = true;
innerSubscription.PublishingInterval = publishingInterval; // in milliseconds.
innerSubscription.KeepAliveCount = 10; // 10*UaRefreshRate = 5s if UaRefreshRate = 500
innerSubscription.LifetimeCount = 100; // UaRefreshRate*100 = 50s if UaRefreshRate = 500;
innerSubscription.MaxNotificationsPerPublish = 100;

//将订阅与会话关联起来
this.Session.AddSubscription(innerSubscription);

// Call the server and create the subscription.
//调用服务器并创建订阅。
innerSubscription.Create();

// At this point the subscription is sending publish requests at the keep alive rate.
// Use the Notification event the session to receive updates when a publish completes.
//此时,订阅将以保持活跃的速率发送发布请求。
//使用通知事件会话在发布完成时接收更新。
this.Session.Notification += new NotificationEventHandler(Session_Notification);

m_Subscription = innerSubscription;
//m_Subscription.Session = m_Session;
//newSubscription.innerSubscription = innerSubscription;
}
catch (Exception e)
{
throw e;
}
this.Session.Notification += Session_Notification;
}
/// <summary>
/// 添加订阅函数
/// </summary>
/// <param name="variableNodeId">节点ID</param>
/// <param name="clientHandle">客户端对象</param>
/// <param name="callback">回调函数</param>
/// <param name="samplingRate">采样时间.</param>
/// <param name="serverHandle">服务处理</param>
public void AddDataMonitoredItem(NodeId variableNodeId, object clientHandle, valueChanged callback, uint samplingRate, out object serverHandle)
{
serverHandle = null;

try
{
if (m_Subscription==null)
{
m_Subscription = new Subscription();
//m_Subscription.Session = m_Session;
}
MonitoredItem monitoredItem = (m_Subscription.DefaultItem);
ClientMonitoredItemData clientData = new ClientMonitoredItemData();
clientData.callback = callback;
clientData.clientHandle = clientHandle;

// Monitored item settings:
monitoredItem.StartNodeId = variableNodeId;
monitoredItem.AttributeId = Attributes.Value;
monitoredItem.MonitoringMode = MonitoringMode.Reporting;
monitoredItem.SamplingInterval = (int)samplingRate; // Affects the read cycle between UA Server and data source
monitoredItem.QueueSize = 1;
monitoredItem.DiscardOldest = false;
monitoredItem.Handle = clientData;

// Add item to subscription.
m_Subscription.AddItem(monitoredItem);

// Call the server and apply any changes to the state of the subscription or monitored items.
m_Subscription.ApplyChanges();

// Check result of add.
if (monitoredItem.Status.Error != null && StatusCode.IsBad(monitoredItem.Status.Error.StatusCode))
{
throw ServiceResultException.Create(
monitoredItem.Status.Error.StatusCode.Code,
"Creation of data monitored item failed");
}

serverHandle = monitoredItem;
}
catch (Exception e)
{
throw e;
}
}

/// <summary>
/// 当服务器发布数据时,响应从到达时,就会执行函数。
/// </summary>
/// <param name="session">The target of the event.</param>
/// <param name="e">The <see cref="Opc.Ua.Client.NotifacationEventArgs"/>Instance containing the event data.</param>
private void Session_Notification(Session session, NotificationEventArgs e)
{
NotificationMessage message = e.NotificationMessage;

// Check for keep alive.
if (message.NotificationData.Count == 0)
{
return;
}

// Get the data changes (oldest to newest).
foreach (MonitoredItemNotification datachange in message.GetDataChanges(false))
{
// Lookup the monitored item.
MonitoredItem monitoredItem = e.Subscription.FindItemByClientHandle(datachange.ClientHandle);

if (monitoredItem == null)
{
continue;
}

ClientMonitoredItemData clientData = monitoredItem.Handle as ClientMonitoredItemData;

clientData.callback(clientData.clientHandle, datachange.Value);
}
}
}
/// <summary>
/// 客户端监控项数据
/// </summary>
public class ClientMonitoredItemData
{
/// <summary>
/// 客户端处理对象
/// </summary>
public object clientHandle = null;
/// <summary>
/// 变化值
/// </summary>
public valueChanged callback = null;
}
}

调用方法

MyOpcUa opcUaClient = new MyOpcUa();
opcUaClient.ConnectServer("opc.tcp://localhost:4840");
opcUaClient.addNotificationHandle(1000);
object sobj = null;
//opcUaClient.MonitorValue("ns=4;s=MAIN.d", new Action<double, Action>(MonitorTestValueFloat));
opcUaClient.AddDataMonitoredItem("ns=4;s=MAIN.d", null, MonitorTestValueFloat, 1000, out sobj);

//回调处理函数

//clientHandle:用户对象,

//value:数据值

private void MonitorTestValueFloat(object clientHandle, DataValue value)
{
object objv = value.Value;
}

关于OPC UA Helper 命名空间中的OpcUaClient 类中的订阅函数的更改的更多相关文章

  1. Oracle数据库中调用Java类开发存储过程、函数的方法

    Oracle数据库中调用Java类开发存储过程.函数的方法 时间:2014年12月24日  浏览:5538次 oracle数据库的开发非常灵活,不仅支持最基本的SQL,而且还提供了独有的PL/SQL, ...

  2. 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法

    实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法 #import <Found ...

  3. 尚硅谷面试第一季-11MyBatis中当实体类中的属性名和表中的字段名不一样怎么办

    问题: MyBatis中当实体类中的属性名和表中的字段名不一样 ,怎么办 ? 解决方案: 1.写sql语句时起别名 <!-- id属性:必须是接口中方法的方法名 resultType属性:必须是 ...

  4. 转载:C++中两个类中互相包含对方对象的指针问题

    原文链接:http://www.cnblogs.com/hanxi/archive/2012/07/25/2608068.html 前几天很不爽,因为C++中两个类中互相包含对方对象的指针编译时提示某 ...

  5. 【记录】mybatis中获取常量类中数据

    部分转载,已注明来源: 1.mybatis中获取常量类中数据 <update id="refuseDebt"> UPDATE dt_debt a SET         ...

  6. cc31a_demo--CppPrimer_静态成员与继承-在派生类中访问基类中的static成员的方法

    //*基类中的static成员,在整个继承层次中只有一个实例 //*在派生类中访问基类中的static成员的方法 //1.基类名::成员名 //2.子类名::成员名 //3.对象.成员名 //4.指针 ...

  7. c++中可以对类中私有成员中的静态变量初始化吗?

    转载http://www.cnblogs.com/carbs/archive/2012/04/04/2431992.html 问题:我看的书上写的对私有部分的访问可以是公共部分的成员函数,也可以是友员 ...

  8. java面向对象中的String类中12种常用的方法

    1.字符串与字符数组的转换 字符串可以使用toCharArray()方法变成一个字符数组,也可以使用String类的构造方法把一个字符数组变成一个字符串. public class StringAPI ...

  9. Python中从B类中调用A类的方法。

    好久没上了,Python还在学--最近进度有点慢... 下面代码记录了一个不太好理解的点,自己写了个小例子,总算是理顺了. B类想要调用A类,自己在网上看了一下其他人的回复:创建A类的实例,直接调用这 ...

随机推荐

  1. ubuntu重启、关机命令

    重启命令 :     1.reboot     2.shutdown -r now 立刻重启    3.shutdown -r 10 过10分钟自动重启    4.shutdown -r 20:35 ...

  2. 求指定区间内与n互素的数的个数 容斥原理

    题意:给定整数n和r,求区间[1, r]中与n互素的数的个数. 详细见容斥定理 详细代码如下 int solve(int r, int n) { vector<int>p; p.clear ...

  3. AGC010 - A: Addition

    原题链接 题意简述 给出一个个数的数列,每次选出两个奇偶性相同的数合成一个数,问最终能否只剩下一个数. 分析 非常简单的一道题. 两个偶数可以合成一个偶数,两个奇数也能合成一个偶数.所以合并偶数时偶数 ...

  4. SSRF漏洞总结

    SSRF漏洞:(服务端请求伪造)是一种由攻击者构造形成由服务端发起请求的一个安全漏洞.一般情况下,SSRF攻击的目标是从外网无法访问的内部系统.(正是因为它是由服务端发起的,所以它能够请求到与它相连而 ...

  5. linux zabbix监控服务器搭建

    搭建Zabbix监控服务器 准备运行环境(lamp) [root@zhuji1 ~]# yum -y install httpd [root@zhuji1 ~]# yum -y install php ...

  6. Python-ORM之sqlalchemy的简单使用

    ORM之sqlalchemy 基础章节 使用SQLAlchemy链接数据库 from sqlalchemy import create_engine from sqlalchemy.ext.decla ...

  7. android技术晋升之道

    写一篇文章记录一下我看到的几个特别常见的误区,希望对团队晋升的同学能有帮助. 误区1:把特质当成案例 工作非常努力,连续一个月加班到12点,解决了问题 喜欢学习新技术和分享,团队同学都很喜欢 善于钻研 ...

  8. R语言︱文本挖掘——jiabaR包与分词向量化的simhash算法(与word2vec简单比较)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- <数据挖掘之道>摘录话语:虽然我比 ...

  9. AM335x(TQ335x)学习笔记——LCD驱动移植

    TI的LCD控制器驱动是非常完善的,共通的地方已经由驱动封装好了,与按键一样,我们可以通过DTS配置完成LCD的显示.下面,我们来讨论下使用DTS方式配置内核完成LCD驱动的思路. (1)初步分析 由 ...

  10. Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons

    提供两种思路 一种线段树区间更新 另一种用map维护连续的区间,也是题解的思路 第二种很难写(我太渣,看了别人的代码,发现自己写的太烦了) #include<iostream> #incl ...