2

I am trying to write a function in CAPL that takes a signal and calculates the physical value with the signal value, the signal factor and the signal offset.

This is how a simple gateway normally works:

message CAN1.myMessage1 myMessage1 = {DIR = RX};//message from the database
message CAN2.myMessage2 myMessage2 = {DIR = TX};//another message from the database

on message CAN1.*
{
   if(this.id == myMessage1.id)
   {
      myMessage1 = this;
      myMessage2.mySignalB = myMessage1.mySignalA * myMessage1.mySignalA.factor + myMessage1.mySignalA.offset;
   }
}

And this is what I am trying to do:

...
on message CAN1.*
{
   if(this.id ==myMessage1.id)
   {
      myMessage1 = this;
      myMessage2.mySignalB = PhysicalValue(myMessage1.mySignalA);
   }
}

double PhysicalValue(signal * s)
{
  return s*s.factor+s.offset;
}

There are two problems with this code:
Firstly when I pass the signal as the parameter the compiler says that the types don't match. The second problem is that inside the function the attributes (factor and offset) are no longer recognized. These problems might have something to do with the weird object-oriented-but-not-really nature of CAPL. The value of the signals can be accessed directly but it also has attributes?

int rawValue = myMessage1.mySignalA;

If you are familiar with C you might say that the problem is that I am specifying a pointer in the function but that I am not passing a pointer into it. But in CAPL there are no pointers and the * simply means anything. Without the * I would have needed to use a specific signal which would have defeated the purpose of the function.

EDIT:
I have found the attribute .phys by now which does exactly what my demo function would have done.

double physValue = myMessage1.mySignalA.phys;

This has already made my code much shorter but there are other operations that I need to perform for multiple signals so being able to use signals as a function parameter would still be useful.

4

2 回答 2

0

请注意,CANoe 已经有一个功能可以完全按照您的要求进行(乘以因子并添加偏移量)。它被称为getSignal

on message CAN1.*
{
   if(this.id == myMessage1.id)
   {
      myMessage2.mySignalB = getSignal(myMessage1::mySignalA);
   }
}

偏移量和因子在例如 DBC 文件中定义。

于 2019-09-05T14:04:00.480 回答
0

你可以做的是:

double PhysicalValue(signal * s)
{
  // access signal by prepending a $
  return $s.phys;
}

像这样打电话

on message CAN1.*
{
   if(this.id ==myMessage1.id)
   {
      myMessage1 = this;
      myMessage2.mySignalB = PhysicalValue(CAN1::myMessage1::mySignalA);
   }
}

即,当您调用函数时,您必须提供信号的限定名称(使用冒号而不是点)。据我所知,无法使用myMessage1.mySignalA,因为信号本身不是 CAPL 数据类型。

除此之外,您可能会重新考虑是否真的应该使用on message,而是切换到on signal. CANoe 的信号服务器处理信号值,无论它们是通过什么消息发送的。

于 2019-07-10T10:10:59.797 回答