如何从 Python 3.x 调用 CAPL 通用函数?
How to call CAPL general functions from Python 3.x?
问题
我正在尝试调用 CAPL 通用函数(在我的例子中是 timeNowNS),但我不知道这是否可行。
我在用什么?
我正在使用 Python 3.7 和 Vector CANoe 11.0。
连接是使用 .NET CANoe API 完成的。
这就是我访问 DLL 的方式。
import clr
sys.path.append("C:\Program Files\Vector CANoe 11.0\Exec64") # path to CANoe DLL Files
clr.AddReference('Vector.CANoe.Interop') # add reference to .NET DLL file
import CANoe # import namespace from DLL file
我尝试了什么?
我成功打开了 CANoe 仿真,开始了测量,我可以访问信号、环境变量和系统变量。
然后我创建了 CAPL 对象并尝试使用 GetFunction 方法来获取 CAPLFunction 对象以便我可以调用它。
def begin_can(self, sCfgFile, fPrjInitFunc = None):
self.open_can()
self.load_can_configuration(sCfgFile)
self.start_can_measurement(fPrjInitFunc)
def open_can(self):
self.mCANoeApp = CANoe.Application()
self.mCANoeMeasurement = CANoe.Measurement(self.mCANoeApp.Measurement)
self.mCANoeEnv = CANoe.Environment(self.mCANoeApp.Environment)
self.mCANoeBus = CANoe.Bus(self.mCANoeApp.get_Bus("CAN"))
self.mCANoeSys = CANoe.System(self.mCANoeApp.System)
self.mCANoeNamespaces = CANoe.Namespaces(self.mCANoeSys.Namespaces)
self.mCANoeCAPL = CANoe.CAPL(self.mCANoeApp.CAPL)
self.mCANoeCAPL.Compile()
def getFunction(self):
function1 = self.mCANoeCAPL.GetFunction('timeNowNS')
# here I tried also CANoe.CAPLFunction(self.mCANoeCAPL.GetFunction('timeNowNS'))
# but i got attribute error: doesn't exist or something like that
result = function1.Call()
预期结果
我应该使用这个函数得到当前的模拟时间。
实际结果
使用上面的代码我得到:
**COMException**: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
at CANoe.ICAPL5.GetFunction(String Name)
我尝试了不同的代码变体,但没有成功。
有没有可能是硬件问题?
我应该在 CANoe 模拟中做一些设置吗?
如果您需要更多信息,请问我!提前致谢
更新:我在添加 CAPL 块后添加了一张测量设置的照片
您必须编写一个调用 timeNowNS
的 CAPL 函数。然后可以按照您实现的方式从 Python 调用此 CAPL 函数。
GetFunction
仅适用于(用户编写的)CAPL 函数。您不能直接调用 CAPL 内在函数(即内置 CAPL 函数)。
将其放入 CAPL 文件中:
int MyFunc()
{
return timeNowNS();
}
然后从 Python 这样调用:
def getFunction(self):
function1 = self.mCANoeCAPL.GetFunction('MyFunc')
result = function1.Call()
经过长时间的尝试和错误以及@m-spiller 的帮助,我找到了解决方案。
function2 = None
def open_can(self):
self.mCANoeApp = CANoe.Application()
self.mCANoeMeasurement = self.mCANoeApp.Measurement # change here: no cast necessary
self.mCANoeEnv = CANoe.Environment(self.mCANoeApp.Environment)
self.mCANoeBus = CANoe.Bus(self.mCANoeApp.get_Bus("CAN"))
self.mCANoeSys = CANoe.System(self.mCANoeApp.System)
self.mCANoeNamespaces = CANoe.Namespaces(self.mCANoeSys.Namespaces)
self.mCANoeCAPL = CANoe.CAPL(self.mCANoeApp.CAPL)
self.mCANoeMeasurement.OnInit += CANoe._IMeasurementEvents_OnInitEventHandler(self.OnInit)
# change here also: explained below
def OnInit(self):
global function2
function2 = CANoe.CAPLFunction(mCANoeCAPL.GetFunction('MyTime')) # cast here is necessary
def callFunction(self):
result = function2.Call()
初始代码有什么问题?
问题是我试图在测量开始后将函数分配给变量。
如第 2.7 章所述here,只能在 Measurement 对象的 OnInit 事件处理程序中将 CAPL 函数分配给变量。
我添加了这一行,研究了文档:
self.mCANoeMeasurement.OnInit += CANoe._IMeasurementEvents_OnInitEventHandler(self.OnInit)
添加后,在初始化时执行 OnInit 函数并将 CAPL 函数分配给一个变量,然后我可以使用该变量调用该函数。
再次感谢@m-spiller!
问题
我正在尝试调用 CAPL 通用函数(在我的例子中是 timeNowNS),但我不知道这是否可行。
我在用什么?
我正在使用 Python 3.7 和 Vector CANoe 11.0。
连接是使用 .NET CANoe API 完成的。 这就是我访问 DLL 的方式。
import clr
sys.path.append("C:\Program Files\Vector CANoe 11.0\Exec64") # path to CANoe DLL Files
clr.AddReference('Vector.CANoe.Interop') # add reference to .NET DLL file
import CANoe # import namespace from DLL file
我尝试了什么?
我成功打开了 CANoe 仿真,开始了测量,我可以访问信号、环境变量和系统变量。
然后我创建了 CAPL 对象并尝试使用 GetFunction 方法来获取 CAPLFunction 对象以便我可以调用它。
def begin_can(self, sCfgFile, fPrjInitFunc = None):
self.open_can()
self.load_can_configuration(sCfgFile)
self.start_can_measurement(fPrjInitFunc)
def open_can(self):
self.mCANoeApp = CANoe.Application()
self.mCANoeMeasurement = CANoe.Measurement(self.mCANoeApp.Measurement)
self.mCANoeEnv = CANoe.Environment(self.mCANoeApp.Environment)
self.mCANoeBus = CANoe.Bus(self.mCANoeApp.get_Bus("CAN"))
self.mCANoeSys = CANoe.System(self.mCANoeApp.System)
self.mCANoeNamespaces = CANoe.Namespaces(self.mCANoeSys.Namespaces)
self.mCANoeCAPL = CANoe.CAPL(self.mCANoeApp.CAPL)
self.mCANoeCAPL.Compile()
def getFunction(self):
function1 = self.mCANoeCAPL.GetFunction('timeNowNS')
# here I tried also CANoe.CAPLFunction(self.mCANoeCAPL.GetFunction('timeNowNS'))
# but i got attribute error: doesn't exist or something like that
result = function1.Call()
预期结果
我应该使用这个函数得到当前的模拟时间。
实际结果
使用上面的代码我得到:
**COMException**: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
at CANoe.ICAPL5.GetFunction(String Name)
我尝试了不同的代码变体,但没有成功。
有没有可能是硬件问题? 我应该在 CANoe 模拟中做一些设置吗?
如果您需要更多信息,请问我!提前致谢
更新:我在添加 CAPL 块后添加了一张测量设置的照片
您必须编写一个调用 timeNowNS
的 CAPL 函数。然后可以按照您实现的方式从 Python 调用此 CAPL 函数。
GetFunction
仅适用于(用户编写的)CAPL 函数。您不能直接调用 CAPL 内在函数(即内置 CAPL 函数)。
将其放入 CAPL 文件中:
int MyFunc()
{
return timeNowNS();
}
然后从 Python 这样调用:
def getFunction(self):
function1 = self.mCANoeCAPL.GetFunction('MyFunc')
result = function1.Call()
经过长时间的尝试和错误以及@m-spiller 的帮助,我找到了解决方案。
function2 = None
def open_can(self):
self.mCANoeApp = CANoe.Application()
self.mCANoeMeasurement = self.mCANoeApp.Measurement # change here: no cast necessary
self.mCANoeEnv = CANoe.Environment(self.mCANoeApp.Environment)
self.mCANoeBus = CANoe.Bus(self.mCANoeApp.get_Bus("CAN"))
self.mCANoeSys = CANoe.System(self.mCANoeApp.System)
self.mCANoeNamespaces = CANoe.Namespaces(self.mCANoeSys.Namespaces)
self.mCANoeCAPL = CANoe.CAPL(self.mCANoeApp.CAPL)
self.mCANoeMeasurement.OnInit += CANoe._IMeasurementEvents_OnInitEventHandler(self.OnInit)
# change here also: explained below
def OnInit(self):
global function2
function2 = CANoe.CAPLFunction(mCANoeCAPL.GetFunction('MyTime')) # cast here is necessary
def callFunction(self):
result = function2.Call()
初始代码有什么问题?
问题是我试图在测量开始后将函数分配给变量。
如第 2.7 章所述here,只能在 Measurement 对象的 OnInit 事件处理程序中将 CAPL 函数分配给变量。
我添加了这一行,研究了文档:
self.mCANoeMeasurement.OnInit += CANoe._IMeasurementEvents_OnInitEventHandler(self.OnInit)
添加后,在初始化时执行 OnInit 函数并将 CAPL 函数分配给一个变量,然后我可以使用该变量调用该函数。
再次感谢@m-spiller!