Convert modbus float [array] register to value

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

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

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?

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

you could use this node

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

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?

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

image

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

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.

All the info is in the built in help...
image

and the built in examples

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

1st buffer parser specification...

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

2nd buffer parser specification...

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

The output...

Here is the flow...

[{"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"]]}]
2 Likes

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.

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

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. 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.

Thanks for the info.

I will review the videos of Node-Red Essentials