CAPL - Converting 4 raw bytes into floating point

心不动则不痛 提交于 2020-06-29 04:16:19

问题


CAPL - Vector.

I receive message ID 0x110 which holds current information:

0x3E6978D5 -> 0.228

Currently I can read the data and save into Enviroment Variable to show in Panel using:

putValue(slow_current, this.long(4));

But I don't know how to convert the HEX 4 bytes into float variable, since I cannot use address or casting (float* x = (float *)&vBuffer;)

How to make this conversion in CAPL script? Thanks.


回答1:


Typically your dbc-file shall contain conversion info from raw value (in your case 4B long) to physical value in form of factor and offset definition:

So your physical value of current shall be calculated as follows: phys_val = (raw_value * factor) + offset

Note: if you define negative offset then you actually subtracting it in equation above.

But it seems you don't have dbc-file so you need to figure out factor and offset by yourself (if you have 2 example raw values and know their physical equivalent then it shall be as easy as finding linear equation parameters -> y = ax + b).

CAPL shall look like this:

variables

{
    float current_phys;
    /* adjust below values to your needs */
    float factor = 0.001 
    dword offset = -1000
}
on message 0x110
{
    current_phys = (this.long(4) * factor) + offset;
    write(current_phys);
}



回答2:


Alternate solution if you don't want to force transform the value:

  1. You define a sysvar type float(double) and use that sysvar in the panel (link to it), instead of the envVar
  2. or you change the type of envVar to float(double).

The translation into float will be done automatically

.

Caveat: usually this trick requires that the input number is also 8 bytes as the defined CAPL float range 8 bytes. But you have this by message payload length constraint= 8bytes.




回答3:


Does not look good, but works:

received msg: 0x3E6978D5

putValue(float4byte,interpretAdFloat(this.long(4)));

float4byte = 0.23



来源:https://stackoverflow.com/questions/52590435/capl-converting-4-raw-bytes-into-floating-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!