# Modbus/Arrays extract divide and give in stream

**URL:** <https://discourse.nodered.org/t/modbus-arrays-extract-divide-and-give-in-stream/8772>\
**Category:** General\
**Created:** [9 March 2019 11:27 UTC](https://discourse.nodered.org/t/modbus-arrays-extract-divide-and-give-in-stream/8772 "2019-03-09T11:27:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krokodandi](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@krokodandi](https://discourse.nodered.org/u/krokodandi)\
**Post date:** [9 March 2019 11:27 UTC](https://discourse.nodered.org/t/modbus-arrays-extract-divide-and-give-in-stream/8772/1 "2019-03-09T11:27:43Z")

</div>

I don't understand how to grab and  
divide array  
He comes in the form

```auto
msg.payload : Object
object
data: array[2]
0: 317
1: 205
buffer: buffer[4]raw
0: 0x1
1: 0x3d
2: 0x0
3: 0xcd

```

I need to consistently pull out the data temperature and humidity.  
How do I write a function?  
What I wrote does not work...  
FunctionBlock  
{  
...  
var TH = [];  
for (var i=0;i\<10;i++)  
{  
TH[i] = { payload: msg.payload[i] };  
}  
return TH;  
...  
}

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [9 March 2019 11:32 UTC](https://discourse.nodered.org/t/modbus-arrays-extract-divide-and-give-in-stream/8772/2 "2019-03-09T11:32:15Z")

</div>

I would use the helper functions provided by the NodeJS buffer in `msg.payload.buffer`.

As far as I remember Modbus uses Big Endian, so `readInt16BE()` might be used in this case.

Have a look at [https://nodejs.org/docs/latest-v8.x/api/buffer.html](https://nodejs.org/docs/latest-v8.x/api/buffer.html).

EDIT: Have a look at the array `msg.payload.data`. These seem to already contain the decoded values of your Modbus registers, so you can use them directly.

Something like:

```auto
let temp = msg.payload.data[0];
let humidity = msg.payload.data[1];

```

The rest depends on what you want to do with these values.

---

<div class="post-metadata">

**Author:** ![krokodandi](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@krokodandi](https://discourse.nodered.org/u/krokodandi)\
**Post date:** [9 March 2019 13:34 UTC](https://discourse.nodered.org/t/modbus-arrays-extract-divide-and-give-in-stream/8772/3 "2019-03-09T13:34:25Z")

</div>

```auto
[{"id":"48d06761.fd16d8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"4fe752d5.9b481c","type":"debug","z":"48d06761.fd16d8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":680,"y":360,"wires":[]},{"id":"5a3bdd91.26c744","type":"modbus-read","z":"48d06761.fd16d8","name":"TH","topic":"","showStatusActivities":true,"logIOActivities":false,"showErrors":true,"unitid":"50","dataType":"InputRegister","adr":"1","quantity":"2","rate":"1","rateUnit":"m","delayOnStart":false,"startDelayTime":"","server":"3f5eb7a5.24f1d8","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":170,"y":220,"wires":[["d7328020.9b962"],[]]},{"id":"d9acf9dc.c75418","type":"function","z":"48d06761.fd16d8","name":"ConvertData","func":"let base = new ArrayBuffer(4); //4 bytes\nlet int16_view = new Int16Array(base);\nlet float32_view = new Float32Array(base);\n\nfloat32_view[0] = msg.payload;\n\nlet rValue = [];\nrValue.push(int16_view[0]);\nrValue.push(int16_view[1]);\n\nmsg.payload = rValue;","outputs":1,"noerr":0,"x":530,"y":180,"wires":[[]]},{"id":"d7328020.9b962","type":"change","z":"48d06761.fd16d8","name":"","rules":[{"t":"set","p":"payload.th","pt":"msg","to":" (payload.TH)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":240,"wires":[["4fe752d5.9b481c"]]},{"id":"3f5eb7a5.24f1d8","type":"modbus-client","z":"","name":"2ndser","clienttype":"simpleser","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"COM8","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"}]

```

"TypeError: Cannot read property '0' of undefined"

I can't open an array yet.  
But if remove the function, the array comes entire [ccc , hhh]  
...  
I don’t know how to pull these numbers out of the array. =(

---

<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:** [9 March 2019 14:03 UTC](https://discourse.nodered.org/t/modbus-arrays-extract-divide-and-give-in-stream/8772/4 "2019-03-09T14:03:43Z")

</div>

Did you realise you have posted a flow containing just a debug node and nothing else?  
See here for how to post a flow in this forum.

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

**Author:** ![krokodandi](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@krokodandi](https://discourse.nodered.org/u/krokodandi)\
**Post date:** [9 March 2019 15:08 UTC](https://discourse.nodered.org/t/modbus-arrays-extract-divide-and-give-in-stream/8772/5 "2019-03-09T15:08:07Z")

</div>

Ok! Everything worked out! Address for my device = 50 (d)

```auto
[{"id":"48d06761.fd16d8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"4fe752d5.9b481c","type":"debug","z":"48d06761.fd16d8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":580,"y":240,"wires":[]},{"id":"5a3bdd91.26c744","type":"modbus-read","z":"48d06761.fd16d8","name":"TH","topic":"","showStatusActivities":true,"logIOActivities":false,"showErrors":true,"unitid":"50","dataType":"InputRegister","adr":"1","quantity":"2","rate":"1","rateUnit":"m","delayOnStart":false,"startDelayTime":"","server":"3f5eb7a5.24f1d8","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":110,"y":100,"wires":[["d7328020.9b962"],[]]},{"id":"d9acf9dc.c75418","type":"function","z":"48d06761.fd16d8","name":"ConvertData","func":"var arr = msg.payload;\nvar temp = arr[0];\nvar hum = arr[1];\nmsg.payload =hum;\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":120,"wires":[["4fe752d5.9b481c"]]},{"id":"d7328020.9b962","type":"change","z":"48d06761.fd16d8","name":"","rules":[{"t":"set","p":"payload.th","pt":"msg","to":" (payload.TH)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":120,"wires":[["d9acf9dc.c75418","53d501e8.fdb63"]]},{"id":"53d501e8.fdb63","type":"function","z":"48d06761.fd16d8","name":"ConvertData","func":"var arr = msg.payload;\nvar temp = arr[0];\nvar hum = arr[1];\nmsg.payload =temp;\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":80,"wires":[["4fe752d5.9b481c"]]},{"id":"3f5eb7a5.24f1d8","type":"modbus-client","z":"","name":"2ndser","clienttype":"simpleser","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"COM8","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"}]

```
