求助各位:
請問我要用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();
}
}
}
uj5u.com熱心網友回復:
System.NullReferenceException:“未將物件參考設定到物件的實體。”這個錯誤在哪行拋出的?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/132252.html
標籤:C#
