2016-04-26 658 views
1

我正在使用CANalyzer,我無法找到如何調用包含參數的CAPL函數。如果我把num放在functions_call.Call(num)裏面不起作用。從Python中調用CAPL函數

def call(num): 
    print 'calling from CAN' 
    x=int(num) 
    functions_call.Call() 
    return 1 

回答

3

我遇到了類似的問題而回,有些谷歌搜索導致我的應用筆記由Vector:

http://vector.com/portal/medien/cmc/application_notes/AN-AND-1-117_CANoe_CANalyzer_as_a_COM_Server.pdf

...結帳「2.7調用CAPL功能」。

概括起來講,一定要聲明你的CAPL函數的參數爲​​「長」,.eg:以下似乎爲我工作:

void function1(long l) 
{ 
    write("function1() called with %d!", l); 
} 

完成的緣故,這是怎麼了我Python代碼(對於上面的例子中)看起來像:

from win32com import client 
import pythoncom 
import time 

function1 = None 
canoe_app = None 
is_running = False 

class EventHandler: 

    def OnInit(self): 
     global canoe_app 
     global function1 

     function1 = canoe_app.CAPL.GetFunction('function1') 

    def OnStart(self): 
     global is_running 
     is_running = True 

canoe_app = client.Dispatch('CANoe.Application') 
measurement = canoe_app.Measurement 
measurement_events = client.WithEvents(measurement, EventHandler) 
measurement.Start() 


# The following loop takes care of any pending events and, once, the Measurement 
# starts, it will call the CAPL function "function1" 10 times and then exit! 
count = 0 
while count < 10: 
    if (is_running): 
     function1.Call(count) 
     count += 1 

    pythoncom.PumpWaitingMessages() 
    time.sleep(1) 
+1

我覺得好奇。使用python腳本執行CANoe CAPL的目的是什麼? –

+1

取決於用例。我使用一組Python API來自動化完整系統測試的不同部分,包括在CANoe中運行的CAN仿真,連接到目標的調試器,可編程電源和其他一些定製硬件。對於某些場景,使用Windows COM來控制CANoe的每個方面可能變得繁瑣和緩慢,因此創建可在外部調用的CAPL「API」可以提供很大的靈活性。 – schaazzz

+1

此外,如果您已經有現有的CAPL腳本來爲您的仿真實現某些功能,那麼如果您有相應的用例(例如我之前的評論中提到的系統級別測試),則還可以使用外部調用各自的功能。 順便說一下,它並不是Python,它可以是_any_語言,其思想是能夠從外部控制整個CANoe模擬(使用Vector的COM API)。 – schaazzz

-1

如果我通過焦炭用作在function1.Call(一些char變量)在python說法,它會引發錯誤作爲

File "C:\Python27\lib\site-packages\win32com\gen_py\4CB02FC0-4F33-11D3-854D-00105A3E017Bx0x1x31.py", line 1668, in Call , p7, p8, p9, p10) File "C:\Python27\lib\site-packages\win32com\client__init__.py", line 467, in ApplyTypes self.oleobj.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args), pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352571), 10)

Python:

var = 'abc' 

count = 0 

while count < 10: 

    if (is_running): 

     function1.Call(var) 

     count += 1 

CAPL: 

void function1(char var1[]) 

{ 

    //Code 

} 
+1

這不提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/18231430) – larsr