问题
I'm working on CANalyzer and I can't find how to call a CAPL function wich contains a parameter. If I put num
in functions_call.Call(num)
it doesn't work.
def call(num):
print 'calling from CAN'
x=int(num)
functions_call.Call()
return 1
回答1:
I ran into a similar problem a while back and some Googling led me to the following application note by Vector:
http://vector.com/portal/medien/cmc/application_notes/AN-AND-1-117_CANoe_CANalyzer_as_a_COM_Server.pdf
...checkout section "2.7 Calling CAPL Functions".
To sum it up, make sure to declare your CAPL function's parameters as "long", .e.g: the following seemed to work for me:
void function1(long l)
{
write("function1() called with %d!", l);
}
For the sake of completion, this is how my python code (for the example above) looks like:
from win32com import client
import pythoncom
import time
function1 = None
canoe_app = None
is_running = False
class EventHandler:
def OnInit(self):
global canoe_app
global function1
function1 = canoe_app.CAPL.GetFunction('function1')
def OnStart(self):
global is_running
is_running = True
canoe_app = client.Dispatch('CANoe.Application')
measurement = canoe_app.Measurement
measurement_events = client.WithEvents(measurement, EventHandler)
measurement.Start()
# The following loop takes care of any pending events and, once, the Measurement
# starts, it will call the CAPL function "function1" 10 times and then exit!
count = 0
while count < 10:
if (is_running):
function1.Call(count)
count += 1
pythoncom.PumpWaitingMessages()
time.sleep(1)
回答2:
If I pass char as argument in function1.Call(some char variable) in python, it throws error as
File "C:\Python27\lib\site-packages\win32com\gen_py\4CB02FC0-4F33-11D3-854D-00105A3E017Bx0x1x31.py", line 1668, in Call , p7, p8, p9, p10) File "C:\Python27\lib\site-packages\win32com\client__init__.py", line 467, in ApplyTypes self.oleobj.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args), pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352571), 10)
Python:
var = 'abc'
count = 0
while count < 10:
if (is_running):
function1.Call(var)
count += 1
CAPL:
void function1(char var1[])
{
//Code
}
来源:https://stackoverflow.com/questions/36867122/call-capl-function-from-python