简体   繁体   English

在 CAPL 中使用之前将消息和信号声明为变量

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

I created a CAPL program to calculate the consumption each time I receive a specific frame.我创建了一个 CAPL 程序来计算每次收到特定帧时的消耗。 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.在下面的示例中,帧称为 TOTAL_DISTANCE_VhSpeed_565,其信号为 ST_CONS_EV_565,但这些可能会根据日志而改变。

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.预先感谢您的反馈。

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.或者,如果您有 dbc,您可以直接从 PDU.signal 中提取数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM