# Help in understanding output from Modbus node

**URL:** <https://discourse.nodered.org/t/help-in-understanding-output-from-modbus-node/28194>\
**Category:** General\
**Created:** [11 June 2020 18:34 UTC](https://discourse.nodered.org/t/help-in-understanding-output-from-modbus-node/28194 "2020-06-11T18:34:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [11 June 2020 18:34 UTC](https://discourse.nodered.org/t/help-in-understanding-output-from-modbus-node/28194/1 "2020-06-11T18:34:43Z")

</div>

I have read and re-read all the posts here about Modbus nodes and their output, but I am truly stumped as to what I am seeing.

In CAS Modbus scanner, I get:

 ![CAS Modbus output for register 121](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/cab785f422a7e48b1a39affbba3a4ba3edad1073.png)

while in Node-RED (taken a few minutes later, and trying to read the same registers), I get:

 ![NR1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/e/2ede14b784a8fca93d6d72748f98609bc3796254.png)

Originally I thought Node-RED was outputting 16-bit integers or something, but I do not think so. For what it's worth, the values I see on the controller that I am trying to read are approx 1143 and approx 101, so CAS Modbus scanner is clearly seeing the correct values, but Node-RED is apparently not.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 June 2020 21:09 UTC](https://discourse.nodered.org/t/help-in-understanding-output-from-modbus-node/28194/2 "2020-06-11T21:09:24Z")

</div>

Node red _is_ outputing 16 bit registers, but the debug is showing each byte as decimal. So the first number has ms byte 4 and ls byte 119, which is 4\*256 + 119 = 1143 which is exactly what you expected. To generate the values in a function node you can do

```auto
let v1 = msg.payload[0] << 8 + msg.payload[1]
etc.

```

however I think there as some javascript functions that will do that for you more concisely and also at least one contrib nodes but I don't remember what they are.

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [11 June 2020 21:22 UTC](https://discourse.nodered.org/t/help-in-understanding-output-from-modbus-node/28194/3 "2020-06-11T21:22:00Z")

</div>

Thank you Colin! This is exactly the help I needed.

---

<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:** [11 June 2020 21:39 UTC](https://discourse.nodered.org/t/help-in-understanding-output-from-modbus-node/28194/4 "2020-06-11T21:39:35Z")

</div>

This return from the modbus node is a a nodejs buffer - this is quite typical of many PLC type nodes.

A buffer is ideal for data where the endianness of the data may or may not be the same on the receiver (node-red in this case)

additionally, nodejs buffer has many built in functions that permit easy conversion from byte to int16 / uint16 / float / int32 etc (in both big endian and little endian)

If you are not much of a coder I wrote a contrib node that does most of the heavy lifting.

> **[node-red-contrib-buffer-parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser)**
>
> A dynamic node to convert values in a buffer or integer array into the many different data type(s). Supports Big/Little Endian, BCD, byteswapping and much more

Example....

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a730c8ebac970f79783ee200e01ae5461a1fcfd4.png)

```auto
[{"id":"a1be6544.008218","type":"buffer-parser","z":"553814a2.1248ec","name":"","data":"payload","dataType":"msg","specification":"{\"options\":{\"resultType\":\"object\",\"singleResult\":true},\"items\":[{\"name\":\"MB30121\",\"type\":\"int16\",\"offset\":0,\"length\":1},{\"name\":\"MB30122\",\"type\":\"int16\",\"offset\":2,\"length\":1}]}","specificationType":"json","x":970,"y":240,"wires":[["3c8ca47e.9ffb7c"]]},{"id":"a59c2b8.5d451d8","type":"inject","z":"553814a2.1248ec","name":"Fake Modbus DATA [4,119,4,118]","topic":"","payload":"[4,119,4,118]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":910,"y":200,"wires":[["a1be6544.008218"]]},{"id":"3c8ca47e.9ffb7c","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","x":990,"y":280,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [11 June 2020 23:17 UTC](https://discourse.nodered.org/t/help-in-understanding-output-from-modbus-node/28194/5 "2020-06-11T23:17:20Z")

</div>

Thank you Steve. That node does the trick nicely. In fact, I am trying to read quite a few registers (not just registers 121 and 122). So I figured I could change the code in your Node to add register 123, for example:  
 ![Node JS integer conversion](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d7798d0e23a009502c415a6697ad97729662655.jpeg)  
but it does not work (gives me _SyntaxError: Unexpected token { in JSON at position 351_ and when I go back to the node editor, it lost the formatting, i.e. looks like this _{ "options": { "resultType": "object", "singleResult": true }, "items": [ {_

so I have obviously wandered above my knowledge grade. I can still make it work with the array[2] node you provided.

The real end game is to get a whole slew of values from the device (shown in bold below), which I believe will be possible with your node if I can get the syntax correct.  
**|122|Probe Sensor mV value**  
**|123|Process Variable**  
**|124|Dew point value**  
**|125|Probe Temperature**  
**|126|CO measured value**  
|127|Probe Resistance value  
|128|Probe Response Time  
|129|Probe Temperature at last Probe Test  
|130|Main Control Output Value  
|131|Secondary Control Output Value  
|132|PID Out Value - % Output|  
|133|Main Control Ouput Display value  
|134|Secondary Control Output Display Value  
|135|Device Status Mode  
|136|Type of Operative setpoint  
**|137|Operative Setpoint value**  
|138|Remote Setpoint value  
|139|Not Used  
|140|Not Used  
|141|Not Used  
**|142|CO Factor**  
**|143|H2 Factor**  
**|144|%O2 process variable**

---

<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:** [12 June 2020 06:26 UTC](https://discourse.nodered.org/t/help-in-understanding-output-from-modbus-node/28194/6 "2020-06-12T06:26:34Z")

</div>

The syntax error is you have a trailing comma after the new item making the json invalid.

The specification has many options - read the built in help on the info side bar & check out the built in examples (on the import examples dialog (CTRL+I) )

for example, if you wanted to send the data to mqtt, you could set the option `singleResult` to false & add a topic to each item & the node will generate separate messages that you can pipe directly into an MQTT out node.

---

<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:** [11 August 2020 06:26 UTC](https://discourse.nodered.org/t/help-in-understanding-output-from-modbus-node/28194/7 "2020-08-11T06:26:42Z")

</div>

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