问题
我试图调用CAPL通用函数(在我的例子中是timeNowNS),但我不知道这是否可能。

我在用什么?
我使用Python3.7和Vector11.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模拟,启动了测量,我可以访问信号、env变量和sys变量。
然后,我创建了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块后,我添加了度量设置的照片

发布于 2019-08-20 09:00:40
经过漫长的尝试和错误以及@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章中所述的这里,只可以在测量对象.的OnInit事件处理程序中将CAPL函数分配给变量。
我在研究文档时增加了这一行:
self.mCANoeMeasurement.OnInit += CANoe._IMeasurementEvents_OnInitEventHandler(self.OnInit)在添加它之后,在init上执行OnInit函数,并将CAPL函数分配给一个变量,然后我可以使用该变量调用该函数。
再次感谢你@m-spiller!
发布于 2019-08-19 07:16:48
您必须编写一个CAPL函数,在其中调用timeNowNS。然后,可以按照您实现的方式从Python调用此CAPL函数。
GetFunction只适用于(用户编写的) CAPL函数。您不能直接调用CAPL内在函数(即内置CAPL函数)。
将其放入CAPL文件:
int MyFunc()
{
return timeNowNS();
}然后像这样从Python调用:
def getFunction(self):
function1 = self.mCANoeCAPL.GetFunction('MyFunc')
result = function1.Call()https://stackoverflow.com/questions/57524982
复制相似问题