Declare a message and signal as a variable before use it in CAPL

892 Views Asked by At

I created a CAPL program to calculate the consumption each time I receive a specific frame. The problem is that if the frame is different, the name of the frame and its signal must be changed throughout the code.

Is it possible to declare a message and a signal as a variable for use throughout the code?

I would like to declare the message and its signal at the beginning of the program, which would allow to change only this one and not the whole code.

In the example below, the frame is called TOTAL_DISTANCE_VhSpeed_565 and its signal is ST_CONS_EV_565 but these may change depending on the log.

on message TOTAL_DISTANCE_VhSpeed_565
{
 
  // First loop for init
  if (firstloop == 0) firstvalue = this.ST_CONS_EV_565.phys;
  if (firstloop == 0) currentvaluehexlast = this.ST_CONS_EV_565;
  if (firstloop == 0) currentvaluelast = this.ST_CONS_EV_565.phys;
  if (firstloop == 0) firstloop = 1;
  
  
  // Get the hex and phys value from consumption signal
  currentvaluehex = this.ST_CONS_EV_565;
  currentvalue = this.ST_CONS_EV_565.phys;
  
  // If the current value is lower than the last one, that mean we do a full step
  // Then, we take the last value from the maximum step and add it to the consumption calculation
  if ((firststep == 0) & currentvaluehex < currentvaluehexlast) canaddition = canaddition + (currentvaluelast - firstvalue);
  firststep = 1;
  if ((firststep == 1) & currentvaluehex < currentvaluehexlast) canaddition = canaddition + currentvaluelast;
  
  // the current value become the last one for the next loop
  currentvaluehexlast = currentvaluehex;
  currentvaluelast = currentvalue;
  
  
  output(this);
}

Thank you in advance for your feedback.

1

There are 1 best solutions below

0
On

yes, use syntax message and then you can set any variable from this message payload. ex:

variable
{
message PDU_Name1 msg_A:
message PDU_Name2 msg_B:
int currentvaluelast;
}
on message PDU_Name1
{
currentvaluelast = msg_A.Byte(0);
}
on message PDU_Name2
{
currentvaluelast = msg_B.Byte(0);
// when different frame layout: currentvaluelast = msg_B.Byte(1); 
}

alternatively, you can extract data directly from PDU.signal if you have dbc.

currentvaluelast = PDU_Name1.signal3;
currentvaluelast = PDU_Name2.signal1;