通过在javascript中将2位值转换为从modbus TCP检索的1位值来获得结果 [英] Get result by converting 2-bit value to 1-bit value retrieve from modbus TCP in javascript

查看:85
本文介绍了通过在javascript中将2位值转换为从modbus TCP检索的1位值来获得结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在modbus上从函数04获得转换/计算值的结果?



我收到了这2个值:[f020,46be ]



如何将此值转换为浮点数?该值的结果是24.25加减。这是设备传感器的电压值。



我尝试过:



How to get the result on converting/calculate value receive from Function 04 on modbus?

I have receive this 2 value: [ "f020", "46be" ]

How to translate this value to be float number? The result of this value is 24.25 plus minus. This is the value from device sensor of voltage.

What I have tried:

function translatethis()
{
    var result=msg.payload.result;
    var forreturn = "";
    for (var i = 0; i<=(result.length)-1; i++)
    {
        var extract = result[i][2] + result[i][3] + result[i][0] + result[i][1];
        forreturn += extract;
    }
    return forreturn[i];
}
var result=msg.payload.result;
var v1 = result[0][2] + result[0][3] + result[0][0] + result[0][1];
var v2 = result[1][2] + result[1][3] + result[1][0] + result[1][1];
var val = [v1, v2];



好​​的。我试着去功能,但失败了。相反,我先直接阅读硬编码。值为V1:20f0,V2:be46



V1和V2都应该在另一个计算或方法中产生24.25的值


Ok. I have try to go to function but it failed. Instead, I'm directly read hardcode first. The value will be V1: 20f0, V2: be46

Both V1 and V2 should be in another calculation or method to produce the value of 24.25

推荐答案

你的MODBUS实现'预缩放'的值是1000吗? (我知道有些人,我最近和他们一起工作过,这是有原因的,但是如果你问我它就是PITA)



我用c ++做了这个快速检查: -



Is your MODBUS implementation 'pre-scaling' the value by 1000 ? (I know some do, one Ive worked with recently did, there's a reason for it, but its a PITA if you ask me)

I did this in c++ as a quick check :-

union {
  float fRet;
  __int16 w[2];
} fw;

fw.x[0] = 0xf020;
fw.x[1] = 0x46be;

float f0 = fw.fRet;





并且收到了值24440.063 - 我也在'Endian-ness'上做了一个假设而不知道你的MODBUS实现是什么......





我认为这是其中一个Arduino库使用的方法

[/ edit]



and received the value 24440.063 - I've also made an assumption on 'Endian-ness' without knowing what your MODBUS implementation actually is ....

[edit]
I think this is the method used by one of the Arduino libraries
[/edit]


这里有一些阅读对于如何解释浮点数的MODBUS变种。

似乎没有一个明确的标准。



Modbus RTU按摩| Chipkin自动化系统 [ ^ ]



Modbus浮点 - Digi开发人员 [ ^ ]
Here is some reading for MODBUS variants of how to interpret floating points.
Seems like there is not one clear standard for it.

Modbus RTU Massage | Chipkin Automation Systems[^]

Modbus Floating Points - Digi Developer[^]


这篇关于通过在javascript中将2位值转换为从modbus TCP检索的1位值来获得结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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