C# 将事件处理程序附加到Com事件:InvalidOperationException(S7-PLCSIM)

C# 将事件处理程序附加到Com事件:InvalidOperationException(S7-PLCSIM),c#,events,com,plc,C#,Events,Com,Plc,我正在尝试将事件处理程序附加到的事件。但我有个例外 using S7PROSIMLib; private S7ProSim ps = new S7ProSim(); ps.Connect(); ps.SetScanMode(ScanModeConstants.SingleScan); ps.BeginScanNotify(); try { ps.ScanFinished += Ps_ScanFinished; //IS7ProSimEvents_ScanFinishedEv

我正在尝试将事件处理程序附加到的事件。但我有个例外

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.
我也尝试过使用注释掉的代码,但得到了相同的异常

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");
} 
附加这样的事件应该可以工作,这是一个使用相同COM接口的流行工具


我的代码出了什么问题?

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

该问题是由prosim引用中的
嵌入互操作类型
设置引起的

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


因此,在projectreferences部分,检查prosim引用的属性。确保未设置
嵌入互操作类型。

您是否正在尝试将RaspberryPi连接到300/400模拟器?是的,您怎么知道?:D:)听起来很不错。你是想把Pi用作远程IO还是别的什么?你有兴趣帮助这个项目吗?是的,我计划用它把模拟扩展到现实世界。如果能帮上点忙,我怎么联系你?