1

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?

Aleph0
  • 5,816
  • 4
  • 29
  • 80

1 Answers1

2

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)

MSpiller
  • 3,500
  • 2
  • 12
  • 24
  • Many thanks for the fast reply. Unfortunately, I'm not able to run your posted piece of code. _IMeasurementEvent_OnInitEventHandler seems to be unknown. Can you please post a minimal reproducible example, that complety shows what do? – Aleph0 Feb 28 '20 at 14:29
  • You can add event handler in python as such: ```python class MeasurementInitHandler: def OnInit(self): pass win32com.client.WithEvents(canoe_measurement_instance, MeasurementInitHandler) ``` – Hatim Apr 07 '22 at 18:00