python - 在 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
.
有谁知道,这里发生了什么?
解决方案
首先,确保您的函数是在测量设置中的 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)
推荐阅读
- c++ - 为什么 freopen() 不能在 Microsoft Visual Studio 上工作,而是在 CodeBlocks 上工作?
- typescript - 我可以创建两个从数字或字符串派生的相等类型,然后求和或连接这些类型吗?
- android - 向通知中心添加信息
- html - 如何使背景适合整个 iPhoneX 屏幕,包括刘海
- javascript - `Gatsby Build` 错误地输出混洗数组对象
- sql - 使用 PostgreSQL 在 Django 中寻找读写锁,例如,SELECT FOR SHARE
- github-pages - Github-pages/kramdown 不会正确呈现我的混合 markdown/html
- css - 在 Outlook 2010 的电子邮件中选择表的第一个子项
- sql - SQL查找所有注册学生数量超过该课程允许的enroll_limit的课程
- java - 使用 StringBuilder 进行字符串连接