Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Quick question about CAPL (passing a message as an argument to a function)

Status
Not open for further replies.

pk_volt

Member level 2
Joined
Oct 20, 2010
Messages
45
Helped
12
Reputation
24
Reaction score
12
Trophy points
1,288
Activity points
1,528
I found a pretty helpful CAPL programming tutorial pdf on here, which has helped me get started with CAPL very quickly.

However, I'm trying to pass a message to another function as an argument, and I can't seem to get this right. I'm basically just trying to do a simple loopback from CAN1 to CAN2. Note: the commented out code in "on message CAN1.vtmCmd1" loopback code works, but I would preferably like to use functions to organize my test cases.


Here is my code.

Code:
on message CAN1.vtmCmd1
{
 
    //Loopback for 1 second, fault for 600ms.
    invalid_test_0x232(this);
  /*  message *gatewayMsg;
    gatewayMsg = this;
    gatewayMsg.CAN = 2;
    output(gatewayMsg);
    */
}

Code:
void invalid_test_0x232(message vtmCmd1 tmCmd1){
    int tm;
    char test_state = 0;
    char alive_counter;

    //configure pkt to loop out to CAN2
    tmCmd1.CAN = 2;
   
    output(tmCmd1);


---------- Post added at 20:00 ---------- Previous post was at 19:56 ----------

ah, I just figured it out. Turns out you have to create a "pointer" or another instance for the gateway message inside my function: For future references, here is the code that i got it to work:

Code:
void invalid_test_0x232(message vtmCmd1 tmCmd1){
    int tm;
    char test_state = 0;
    char alive_counter;

    //configure pkt to loop out to CAN2
    //tmCmd1.CAN = 2;
    //output(tmCmd1); 
 
     message *gMsg;
     gMsg = tmCmd1;
     gMsg.CAN = 2;
     output(gMsg);
 /*
}
 

On a similar note, how can I return a message from a function? I have tried to declare my function as:

Code:
message MyFunction(message * msg)
{
   return msg;
}

but this gives a parse error on the return type (i.e. message).
 

On a similar note, how can I return a message from a function? I have tried to declare my function as:

Code:
message MyFunction(message * msg)
{
   return msg;
}

but this gives a parse error on the return type (i.e. message).

Hi there, I, too was unable to find out a way on how to return a message, but I did find a discovery that might help solve your problem.

I noticed that if you pass a specific message to a function, it seems to retrieve it as a pointer, allowing you to modify its contents without having to return it.

For example, this is a simple test I did. note that I am sending a constant value of 1 for my gear_cmd from my CAN device connected to my Cancase logger

Code:
on message CAN1.vtmCmd1
{
    write("Gear Command before: %d", this.gear_cmd);

    testfunc(this);
    write("Gear Command now: %d", this.gear_cmd);
}

Code:
void testfunc (message vtmCmd1 msg)
{

msg.gear_cmd = 3;

}

and the output:
CAPL Gear Command now: 3
CAPL Gear Command before: 1
CAPL Gear Command now: 3
CAPL Gear Command before: 1
CAPL Gear Command now: 3
CAPL Gear Command before: 1
CAPL Gear Command now: 3
....



Let me know if this helps.
 
  • Like
Reactions: EmilN

    EmilN

    Points: 2
    Helpful Answer Positive Rating
Thank you very much! I had a though that this might be the case, but I have not yet had the possibility to try it out, but I will try to do that now. Thanks for your help!
 

another quick question.

How do you use "Value Table" parameters in CAPL scripts. For example, if I have a message signal pwr_state associated with a value table with values {ON, OFF, ERROR}

how do I do something like: message.pwr_state = ON;

or something like that?
 

Can you use enum?

Code:
    enum State {isOn, isOff, isError};
    enum State state;
    state = (enum State) isOn;

    switch (state) {
     case isOn:
        write("It is on.");
        break;
     case isOff:
        write("It is off.");
        break;
     case isError:
        write("There is an error.");
        break;
    }

    if (state == isOn)
        write("OK");
 

Can you use enum?

Code:
    enum State {isOn, isOff, isError};
    enum State state;
    state = (enum State) isOn;

    switch (state) {
     case isOn:
        write("It is on.");
        break;
     case isOff:
        write("It is off.");
        break;
     case isError:
        write("There is an error.");
        break;
    }

    if (state == isOn)
        write("OK");

Did that code actually work? I'll have to give it a try tomorrow. Last time I looked at the programming reference, CAPL doesn't support enums. Nevertheless, I think it is still more convenient to grab values strait from the value table into CAPL scripts :(
 

I have to say that I never tried it, but it does compile and enums are supported in CAPL according to the help file. (I'm using CANalyzer 7.2 if that matters.)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top