[英]How to call CAPL general functions from Python 3.x?
I'm trying to call CAPL general functions (in my case timeNowNS ) but I don't know if it's possible.我正在尝试调用CAPL通用函数(在我的情况下是timeNowNS ),但我不知道是否可能。
I'm using Python 3.7 and Vector CANoe 11.0.我使用的是 Python 3.7 和 Vector CANoe 11.0。
The connection is done using the .NET CANoe API.连接是使用 .NET CANoe API 完成的。 This is how i've accesed the DLLs.
这就是我访问 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
I opened the CANoe simulation succesfully, started the measure and I'm having access to signals, env variables and sys variables.我成功地打开了 CANoe 模拟,开始了测量,我可以访问信号、环境变量和系统变量。
Then I've created the CAPL Object and tried using the GetFunction method to obtain the CAPLFunction object so i could call it.然后,我创建了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()
I should get the current simulation time using this function.我应该使用此功能获取当前的模拟时间。
Using the above code I get:使用上面的代码我得到:
**COMException**: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
at CANoe.ICAPL5.GetFunction(String Name)
I've tried different variations of the code but didn't get anywhere.我尝试了代码的不同变体,但没有任何结果。
Is it possible to be a hardware problem?有没有可能是硬件问题? Should I do some settings in the CANoe Simulation?
我应该在 CANoe Simulation 中做一些设置吗?
If you need more information, please ask me!如果您需要更多信息,请询问我! Thanks in advance
提前致谢
Update: I've added a photo of my measure setup after adding the CAPL block更新:我在添加 CAPL 块后添加了我的测量设置的照片
You have to write a CAPL function in which you call timeNowNS
.您必须编写一个 CAPL 函数,在其中调用
timeNowNS
。 This CAPL function can then be called from Python in the way you have implemented.然后可以按照您实现的方式从 Python 调用此 CAPL 函数。
GetFunction
only works with (user-written) CAPL functions. GetFunction
仅适用于(用户编写的)CAPL 函数。 You cannot call CAPL intrinsics (ie built-in CAPL functions) directly.您不能直接调用 CAPL 内在函数(即内置 CAPL 函数)。
Put this into a CAPL file:将其放入 CAPL 文件中:
int MyFunc()
{
return timeNowNS();
}
and call like this from Python:并从 Python 中这样调用:
def getFunction(self):
function1 = self.mCANoeCAPL.GetFunction('MyFunc')
result = function1.Call()
After a long session of try and error and the help of @m-spiller I found the solution.经过长时间的尝试和错误以及@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()
What was the problem with the initial code?最初的代码有什么问题?
The problem was that i tried to assign a function to a variable after the measure has started.问题是我试图在度量开始后将一个函数分配给一个变量。
As stated here in chapter 2.7, the assignment of a CAPL Function to a variable can only be done in the OnInit event handler of the Measurement object.如前所述这里的章节2.7,一个CAPL函数的一个变量赋值只能在测量对象的OnInit事件处理程序中完成。
I've added this line, studying the documentation:我已经添加了这一行,研究了文档:
self.mCANoeMeasurement.OnInit += CANoe._IMeasurementEvents_OnInitEventHandler(self.OnInit)
After adding it, on init the OnInit function was executed and the CAPL function was assigned to a variable and afterwards i could use that variable to call the function.添加后,在初始化时执行 OnInit 函数并将 CAPL 函数分配给一个变量,然后我可以使用该变量来调用该函数。
Thank you again, @m-spiller !再次感谢您,@m-spiller !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.