求助各位: 请问我要用C#实现一个功能,就是 opcua 服务器端一个节点里面内容有变化,立刻得到这个节点的数据。我网上查了,需要用数据订阅功能。下面是我代码,但是运行到 m_OpcUaClient.AddSubscription("A", "ns=2;s=Machines/Machine B/TestValueFloat", SubCallback);这段后,跳转不到SubCallback()这个方法。报如下错误System.NullReferenceException:“未将对象引用设置到对象的实例。”
代码如下:private OpcUaClient m_OpcUaClient; private void button4_Click(object sender, EventArgs e)
{
// sub
m_OpcUaClient.AddSubscription("A", "ns=2;s=Machines/Machine B/TestValueFloat", SubCallback); }
private void SubCallback(string key, MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs args)
{
if (InvokeRequired)
{
Invoke(new Action<string, MonitoredItem, MonitoredItemNotificationEventArgs>(SubCallback), key, monitoredItem, args);
return;
} if (key == "A")
{
// 如果有多个的订阅值都关联了当前的方法,可以通过key和monitoredItem来区分
MonitoredItemNotification notification = args.NotificationValue as MonitoredItemNotification;
if (notification != null)
{
textBox1.Text = notification.Value.WrappedValue.Value.ToString();
}
}
}
代码如下:private OpcUaClient m_OpcUaClient; private void button4_Click(object sender, EventArgs e)
{
// sub
m_OpcUaClient.AddSubscription("A", "ns=2;s=Machines/Machine B/TestValueFloat", SubCallback); }
private void SubCallback(string key, MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs args)
{
if (InvokeRequired)
{
Invoke(new Action<string, MonitoredItem, MonitoredItemNotificationEventArgs>(SubCallback), key, monitoredItem, args);
return;
} if (key == "A")
{
// 如果有多个的订阅值都关联了当前的方法,可以通过key和monitoredItem来区分
MonitoredItemNotification notification = args.NotificationValue as MonitoredItemNotification;
if (notification != null)
{
textBox1.Text = notification.Value.WrappedValue.Value.ToString();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货