2016-01-22 124 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

相關問題