首页 > 解决方案 > 在 CANoe 中使用 comtypes 运行 CAPL 函数会出错

问题描述

现在,我正在尝试使用 python comtypes 包使用 CANoe COM API 在 CANoe 中调用 CAPL 函数。

为此,我创建了以下小型简短 python 程序:

from comtypes.client import CreateObject
c=CreateObject("CANoe.Application")
squareFunction=c.CAPL.GetFunction("square")
res=squareFunction.Call(5)
print(res==25)

这应该调用我的简短 CAPL 函数:

int square(int x) {
   return x*x;
}

不幸的是,c.CAPL.GetFunction("square")如果仿真在 CANoe 中运行,程序会产生异常。

COMError: (-2147418113, 'Critical Error', (None, None, None, 0, None)) 

如果 CANoe 中的模拟停止,则没有错误,但函数调用会产生None.

有谁知道,这里发生了什么?

标签: pythoncomcaplcanoecomtypes

解决方案


首先,确保您的函数是在测量设置中的 CAPL 块中定义的,而不是在模拟设置中。

应用笔记CANalyzer/CANoe as a COM Server by Vector链接在第 15 页上指出

将 CAPL 函数分配给变量只能在 Measurement 对象的 OnInit 事件处理程序中完成。

即您的squareFunction变量必须在 OnInit 事件期间进行初始化。与此类似:

def OnInit():
  self.squareFunction = c.CAPL.GetFunction("square")

c.Measurement.OnInit += CANoe._IMeasurementEvents_OnInitEventHandler(self.OnInit)

OnInit将在测量初始化期间执行,您可以稍后执行self.squareFunction.Call(5)


推荐阅读