CAPL-将4个原始字节转换为浮点数 [英] CAPL - Converting 4 raw bytes into floating point

查看:566
本文介绍了CAPL-将4个原始字节转换为浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CAPL-向量。

我收到消息ID 0x110,其中包含当前信息:

I receive message ID 0x110 which holds current information:

0x3E6978D5-> 0.228

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));

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

但是我不知道如何将4个十六进制字节转换为float变量,因为我不能使用地址或强制转换( float * x =(float *)&vBuffer;)

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;)

如何在CAPL脚本中进行此转换?

How to make this conversion in CAPL script? Thanks.

推荐答案

通常,您的dbc文件应包含从原始值(在您的情况下为4B长)到物理值的转换信息。因子和偏移量定义的形式:

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:

因此,您的电流物理值应按以下公式计算:
phys_val =(raw_value * factor)+偏移量

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.

但是您似乎没有dbc文件,因此您需要自己弄清楚系数和偏移量(如果您有2个示例原始值并知道它们的物理等效项)那么它就像查找线性方程参数一样简单-> y = ax + b)。

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应该看起来像这样:

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);
}

这篇关于CAPL-将4个原始字节转换为浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆