2016-01-22 128 views
2

我正在尝试将事件处理程序附加到COM Object的事件。但是我得到一个InvalidOperationException。将事件处理程序附加到Com事件:InvalidOperationException(S7-PLCSIM)

using S7PROSIMLib; 

private S7ProSim ps = new S7ProSim(); 

ps.Connect(); 
ps.SetScanMode(ScanModeConstants.SingleScan); 
ps.BeginScanNotify(); 

try { 
    ps.ScanFinished += Ps_ScanFinished; 
    //IS7ProSimEvents_ScanFinishedEventHandler scanFinishedDelegate = new IS7ProSimEvents_ScanFinishedEventHandler(Ps_ScanFinished); 
    //ps.ScanFinished += scanFinishedDelegate; 
} 
catch (InvalidOperationException ex) 
{ 
    Console.WriteLine(ex.Source); 
    Console.WriteLine(ex.InnerException); 
    Console.WriteLine(ex.Message); 
    Console.WriteLine(ex.StackTrace); 
} 

private void Ps_ScanFinished(object ScanInfo) 
{ 
    Console.WriteLine("Event fired"); 
} 

输出为:

event invocation for COM objects requires event to be attributed with DispIdAttribute 
    at System.Runtime.InteropServices.ComAwareEventInfo.GetDataForComInvocation(EventInfo eventInfo, Guid& sourceIid, Int32& dispid) 
    at System.Runtime.InteropServices.ComAwareEventInfo.AddEventHandler(Object target, Delegate handler) 
    at PiPLCSimBridge.Form1.Form1_Load(Object sender, EventArgs e) in C:\PiPLCSimBridge\Form1.cs:Line 72. 

我也曾尝试使用注释掉的代码,但得到了同样的异常。

附加这样的事件应该工作,使用相同的COM接口is doing it this way.

什么是错我的代码的流行工具?

+0

您是否试图将RaspberryPi连接到300/400模拟器? – dss539

+0

是的,你怎么能告诉?:D – c3ntry

+0

:)这听起来很整齐。你是否试图将Pi用作远程IO或其他东西?你对这个项目的帮助感兴趣吗? – dss539

回答

3

幸运的是,我最近遇到了与这个完全相同的COM库完全相同的错误。

该问题是由prosim参考中的Embed Interop Types设置引起的。

我通过讨论,发现了这个事实 here.

所以,在项目引用部分,请检查PROSIM参考属性。确保没有设置Embed Interop Types

相关问题