Call CAPL function from Python

13,365

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)
Share:
13,365
Admin
Author by

Admin

Updated on June 07, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm working on CANalyzer and I can't find how to call a CAPL function which 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
    
  • PySerial Killer
    PySerial Killer over 6 years
    I feel curiosity. What is the purpose of using python scripts to execute CANoe CAPL's?
  • schaazzz
    schaazzz over 6 years
    Depends on the use case. I use a set of Python APIs for automating different parts of a full system test including a CAN simulation running in CANoe, a debugger connected to the target, a programmable powersupply and some other custom hardware. For some scenarios, using Windows COM to control every aspect of CANoe can become tedious as well as slow, so creating CAPL "APIs" which can be called externally allows for a lot of flexibility.
  • schaazzz
    schaazzz over 6 years
    Additionally, if you already have existing CAPL scripts that implement certain functionality for your simulation, you can also invoke the respective functions using externally if you have a corresponding use case (e.g. a system level test as mentioned in my previous comment). By the way, it doesn't have to be Python, it can be be any language, the idea is to be able to control an entire CANoe simulation externally (using Vector's COM API).
  • Sivaram Boina
    Sivaram Boina about 5 years
    @schaazzz did you achieved it finally? I am running a python script to get some results. My question, is it possible to send the results to CAN and display in CanOe Panel in python script?
  • schaazzz
    schaazzz about 5 years
    @sivaram If I understood your question correctly, I assume you want to send some value over CAN and update some value in a CANoe panel over COM (using Python). Panel values can be either Environment/System Variables or Signals, you can set them directly using the correct COM objects for setting variables or signals. If you need a specific examples, please create a new question with details.
  • Sivaram Boina
    Sivaram Boina almost 5 years
    @schaazzz Thanks for your response. I have achieved it finally. If needed any further help/suggestion, I will post here.