高级HMI订阅不起作用

时间:2014-08-05 07:47:10

标签: c# wpf

我在尝试订阅时遇到错误。 m_Plc.Read函数运行正常,但对我的应用程序来说太慢了。

这是我的代码:

private AdvancedHMIDrivers.EthernetIPforCLXCom m_Plc;
public MainPageViewModel()
{
   m_Plc = new EthernetIPforCLXCom();
   m_Plc.IPAddress = m_IPAddress;
   m_Plc.ProcessorSlot = Convert.ToInt32(m_ProcessorSlot);
   m_Plc.ComError += DoOnCommError;
   m_Plc.SubscriptionError += DoOnCommError;
   string testString = m_Plc.Read("MyTag");  
   int index = m_Plc.Subscribe("MyTag", 1, 2000, testPlcReaded);
}
private void testPlcReaded(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
{
    try
    {
        Console.WriteLine(e.Values[0]);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

这是我得到的错误:

m_SynchronizingObject.BeginInvoke(SubscriptionList(i).dlgCallBack, z)

位于以下文件夹中:EthernetIPforCLXCom.vb。

有一个指向m_SynchronizingObject的空指针。

错误详情:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=AdvancedHMIDrivers
  StackTrace:
       at AdvancedHMIDrivers.EthernetIPforCLXCom.DataLinkLayer_DataReceived(Object sender, PlcComEventArgs e) in C:\prj\TLLAB\TLLAB\AdvancedHMIBeta363 (1)\AdvancedHMIDrivers\EthernetIPforCLXCom.vb:line 1269
       at MfgControl.AdvancedHMI.Drivers.CIP.OnDataReceived(PlcComEventArgs e)
       at MfgControl.AdvancedHMI.Drivers.CIP.DataReceivedEIP(Object sender, PlcComEventArgs e)

2 个答案:

答案 0 :(得分:1)

在当前版本

中看起来像是一个错误

代码检查SynchronizingObject是否为null或者在调用它之前是否创建了它的句柄,所以如果它为null,它就会崩溃

If m_SynchronizingObject Is Nothing OrElse DirectCast(m_SynchronizingObject, Windows.Forms.Control).IsHandleCreated Then
    '...<some code>...

    m_SynchronizingObject.BeginInvoke(SubscriptionList(i).dlgCallBack, z)
End If

你应该提交一个bug,因为它可能应该是这样的

If m_SynchronizingObject IsNot Nothing AndAlso DirectCast(m_SynchronizingObject, Windows.Forms.Control).IsHandleCreated Then
    '...<some code>...

    m_SynchronizingObject.BeginInvoke(SubscriptionList(i).dlgCallBack, z)
Else
    '...<some other code>...
End If

编辑:我将此贴到了the project site on Sourceforge,Archie已经解决了下一个版本的问题

答案 1 :(得分:0)

您没有实例化您的对象。当您尝试使用它时它为null。 这应该有所帮助:

private EthernetIPforCLXCom m_Plc = new EthernetIPforCLXCom
 {
   IPAddress = m_IPAddress,
   ProcessorSlot = Convert.ToInt32(m_ProcessorSlot),
   ComError += DoOnCommError,
   SubscriptionError += DoOnCommError
};