# Convert modbus float \[array\] register to value

**URL:** <https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303>\
**Category:** General\
**Created:** [8 May 2020 11:07 UTC](https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303 "2020-05-08T11:07:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![luishurtado87](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/luishurtado87/32/21479_2.png) [@luishurtado87](https://discourse.nodered.org/u/luishurtado87)\
**Post date:** [8 May 2020 11:07 UTC](https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303/1 "2020-05-08T11:07:06Z")

</div>

Hello,

I am very new at Node-red but I am trying to create a dashboard for some of the modbus data of a syste. However, I am having problems reading 32-bit floating point values (16-bit integers are ok). And I was hoping someone could help me with this.

The information I want to read is a the voltage of the system, and it is contained by two registers (209/210). I am using a very simple stetup to read the registers, convert them into a foat buffer and finally to a decimal value.

For getting the float buffer I am ussing the following code

```auto
var payload=msg.payload;
var buf = Buffer.alloc(4);
buf.writeFloatBE(payload);
msg.payload=buf;
return msg;

```

For getting the decimal valules (found in one of the forum topics):

```auto
let pay = msg.payload;
const buf = Buffer.allocUnsafe(4); // (4) is ok
buf.writeUInt16BE(pay[0]); // high byte
msg.payload = buf.readFloatBE(0);
// 2 numbers after comma (string)
msg.payload = buf.readFloatBE(0).toFixed(2);
//
return msg;

```

But the values I get are not ok. Could anyone help?

 ![NodeRed](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/dbbf23783da26323d167e88e1950658437d32c83.jpeg)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 May 2020 11:28 UTC](https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303/2 "2020-05-08T11:28:50Z")

</div>

If that payload is an array you cant just write it as a float to the buffer.

you could use [this node](https://flows.nodered.org/node/node-red-contrib-buffer-parser)

modbus data --\> buffer-parser --\> any value you want

---

<div class="post-metadata">

**Author:** ![luishurtado87](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/luishurtado87/32/21479_2.png) [@luishurtado87](https://discourse.nodered.org/u/luishurtado87)\
**Post date:** [15 May 2020 14:02 UTC](https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303/3 "2020-05-15T14:02:12Z")

</div>

Thanks but i am afraid I still dont know how to use that node. I tried using it but I get either a syntax error "Unexpected token p in JSON at position 0" or a type error "Cannot read property 'resultType' of undefined". Do you have some examples how I should use it?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 May 2020 14:10 UTC](https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303/4 "2020-05-15T14:10:57Z")

</div>

There are lots of examples in the built in sample...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/66cddd64f1d839f859d69f63bb9174f2a52177cd.png)

CTRL+I \> import \> examples.

If you give me a copy of your data (the buffer data you are feeding in) and what you need out 9the formatted data out of buffer-parser) I'll knock up a simple example.

Share your flow (select the relevant nodes in your flow then press CTRL+E)

`````  
`paste your flow between 3 backticks - like this`  
`````

EDIT... these are the examples included...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0d5558c6d8dbf218ab171e55acf8e4a2e3cc0d58.png)

---

<div class="post-metadata">

**Author:** ![jrahumadag](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@jrahumadag](https://discourse.nodered.org/u/jrahumadag)\
**Post date:** [29 June 2020 00:27 UTC](https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303/5 "2020-06-29T00:27:11Z")

</div>

Hi Steve,

i was reading this topic because i'm looking for a hint to resolve a similar issue.

I'm using a Moxa ioLogik (1242) to read some AI (4-20mA). Moisture and Temperature. I can read scaling values and i received the following array [32870,16838,10199,16856], those values are 24,8 ppm and 27 °C aprox.

I 'don't understand how to use the buffer parser to convert [32870,16838] in 24,8 ppm and [10199,168566] in to 27 °C. I try with some examples that you refer, but i can't make it.

Thanks.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 June 2020 07:48 UTC](https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303/6 "2020-06-29T07:48:02Z")

</div>

All the info is in the built in help...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04d0043bbc6d94c5ee0b3f6bb80830fa7cc91739.png)

and the built in examples

> [@jrahumadag](#):
>
> I 'don't understand how to use the buffer parser to convert [32870,16838] in 24,8 ppm and [10199,168566] in to 27 °C

I did a demo with your data. The 1st buffer parser is set to return 1 object with your values in, the 2nd one is set to return individual results with the topic set (perfect for sending straight to MQTT)...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dc8505fbf13a80408aacec6175fd82e5227f925b.png)

1st buffer parser specification...

```auto
{
    "options": {
        "byteSwap": [
            "swap16"
        ],
        "singleResult": true,
        "resultType": "object"
    },
    "items": [
        {
            "name": "ppm",
            "type": "floatle",
            "offset": 0
        },
        {
            "name": "temp",
            "type": "floatle",
            "offset": 4
        }
    ]
}

```

2nd buffer parser specification...

```auto
{
    "options": {
        "byteSwap": [
            "swap16"
        ],
        "singleResult": false,
        "resultType": "value",
        "setTopic": true
    },
    "items": [
        {
            "name": "ppm",
            "type": "floatle",
            "offset": 0
        },
        {
            "name": "temp",
            "type": "floatle",
            "offset": 4
        }
    ]
}

```

The output...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db05b3bfe9f76ad0fc105cbe1927810c4f89ccff.png)

Here is the flow...

```auto
[{"id":"5326f890.9a4668","type":"inject","z":"3391e156.d9d19e","name":"fake data","topic":"","payload":" [32870,16838,10199,16856]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":300,"y":860,"wires":[["c425a4f4.8a8e48","528d2375.d3654c"]]},{"id":"af8f4394.bc2ca","type":"debug","z":"3391e156.d9d19e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":630,"y":860,"wires":[]},{"id":"e346f503.875208","type":"debug","z":"3391e156.d9d19e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":650,"y":920,"wires":[]},{"id":"c425a4f4.8a8e48","type":"buffer-parser","z":"3391e156.d9d19e","name":"","data":"payload","dataType":"msg","specification":"{\"options\":{\"byteSwap\":[\"swap16\"],\"singleResult\":true,\"resultType\":\"object\"},\"items\":[{\"name\":\"ppm\",\"type\":\"floatle\",\"offset\":0},{\"name\":\"temp\",\"type\":\"floatle\",\"offset\":4}]}","specificationType":"json","x":470,"y":860,"wires":[["af8f4394.bc2ca"]]},{"id":"528d2375.d3654c","type":"buffer-parser","z":"3391e156.d9d19e","name":"","data":"payload","dataType":"msg","specification":"{\"options\":{\"byteSwap\":[\"swap16\"],\"singleResult\":false,\"resultType\":\"value\",\"setTopic\":true},\"items\":[{\"name\":\"ppm\",\"type\":\"floatle\",\"offset\":0},{\"name\":\"temp\",\"type\":\"floatle\",\"offset\":4}]}","specificationType":"json","x":470,"y":920,"wires":[["e346f503.875208"]]}]

```

---

<div class="post-metadata">

**Author:** ![jrahumadag](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@jrahumadag](https://discourse.nodered.org/u/jrahumadag)\
**Post date:** [29 June 2020 19:49 UTC](https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303/7 "2020-06-29T19:49:02Z")

</div>

Thanks Steve.

I deployed both buffer parser and are working good. In my aplicattion, the first parser work better than second (i'm connecting the node to Ubidots\_out node).

An other thing i'm learned, it's about some concepts that i don't handle. For example, in the variable "ppm" the offset is 0, but in the variable "temp" is 4. Why?  
So, the last favor i ask you if you know some repository where i can learn this concepts. Maybe are too simple, but i'm new in this world.

Thanks again.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 June 2020 20:24 UTC](https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303/8 "2020-06-29T20:24:08Z")

</div>

> [@jrahumadag](#):
>
> For example, in the variable "ppm" the offset is 0, but in the variable "temp" is 4. Why?

As your data is 4 x 16bit values, that equates to 8 bytes. Bytes 0~3 contains the value of **ppm**. Bytes 4~7 contain the value of **temp**.

That buffer parser node is designed to ease a common task (like this task you're facing). Behind the scenes, it interprets your specification & parses the data by a sprinkling of maths & the [nodejs Buffer](https://nodejs.org/docs/latest-v12.x/api/buffer.html)

> [@jrahumadag](#):
>
> So, the last favor i ask you if you know some repository where i can learn this concepts. Maybe are too simple, but i'm new in this world

There is no specific tutorials for this kind of thing. You kinda pick it up working with PLCs and various programming languages over the course of many years. For this particular solution, there are a few different technologies / disciplines converging here...  
JavaScript / nodejs / nodejs buffers, Json, Computer science bits n bytes, Node-red.

You are probably best off starting here: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). It is by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in less than an hour.

Get to grips with node-red and pick up the coding when you need to get more adventurous.

Ps, read the built in help of a node in the sidebar in node-red. Normally all the info you need is in there.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![jrahumadag](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@jrahumadag](https://discourse.nodered.org/u/jrahumadag)\
**Post date:** [29 June 2020 20:52 UTC](https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303/9 "2020-06-29T20:52:28Z")

</div>

Thanks for the info.

I will review the videos of Node-Red Essentials

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 August 2020 20:52 UTC](https://discourse.nodered.org/t/convert-modbus-float-array-register-to-value/26303/10 "2020-08-28T20:52:29Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
