Right now I'm trying to call a CAPL function in CANoe with the CANoe COM API using the python comtypes package.
For this I created the following small short python program:
from comtypes.client import CreateObject
c=CreateObject("CANoe.Application")
squareFunction=c.CAPL.GetFunction("square")
res=squareFunction.Call(5)
print(res==25)
This should call my short CAPL function:
int square(int x) {
return x*x;
}
Unfortunately, the program yields in c.CAPL.GetFunction("square")
an exception, in case the simulation is running in CANoe.
COMError: (-2147418113, 'Critical Error', (None, None, None, 0, None))
If the simulation in CANoe is stopped, there is no error, but the call of the function yields None
.
Does anyone knows, what is going on here?
First, make sure your function is defined in a CAPL block in the measurement-setup, not in the simulation-setup.
The application note CANalyzer/CANoe as a COM Server by Vector link states on page 15 that
The assignment of a CAPL function to a variable can only be done in the OnInit event handler of the Measurement object.
I.e. your squareFunction
variable has to be initialized during the OnInit event. Similar to this:
def OnInit():
self.squareFunction = c.CAPL.GetFunction("square")
c.Measurement.OnInit += CANoe._IMeasurementEvents_OnInitEventHandler(self.OnInit)
By this OnInit
will be executed during measurement init and you can later execute self.squareFunction.Call(5)