# Modbus TCP array to readable value

**URL:** <https://discourse.nodered.org/t/modbus-tcp-array-to-readable-value/29851>\
**Category:** General\
**Created:** [10 July 2020 13:34 UTC](https://discourse.nodered.org/t/modbus-tcp-array-to-readable-value/29851 "2020-07-10T13:34:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Barry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/john_barry/32/28987_2.png) [@John\_Barry](https://discourse.nodered.org/u/John_Barry)\
**Post date:** [10 July 2020 13:34 UTC](https://discourse.nodered.org/t/modbus-tcp-array-to-readable-value/29851/1 "2020-07-10T13:34:11Z")

</div>

Hi,  
I've been through several sites and forums but i'm still struggling to read a frequency value from a power meter to node red. The debug node shows an array of two values but i don't know how to manipulate them into the actual value of around 50Hz.  
From another app called modbus poll i can pull up the register and the convert it to float CD AB and it works.

```auto
msg.payload = [0, 16833];
let pay = msg.payload;

const buf = Buffer.allocUnsafe(4);
buf.writeInt16BE(pay[0],2);
buf.writeInt16BE(pay[1],0);

msg.payload = buf.readFloatBE(0);
return msg;

```

This code does the trick but i'm not sure how to inject the message from the modbus read node instead of using the constant "[0, 16833]"

---

<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:** [10 July 2020 15:39 UTC](https://discourse.nodered.org/t/modbus-tcp-array-to-readable-value/29851/2 "2020-07-10T15:39:23Z")

</div>

Attach a debug node to the output of the modbus node.

See how the values are returned in msg.payload and look similar?

So wire the modbus node to your function node and in your function node - instead of setting the msg.payload to an array, just don't. (e.g. delete the top line )

---

<div class="post-metadata">

**Author:** ![DrdP](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@DrdP](https://discourse.nodered.org/u/DrdP)\
**Post date:** [12 July 2020 23:20 UTC](https://discourse.nodered.org/t/modbus-tcp-array-to-readable-value/29851/3 "2020-07-12T23:20:31Z")

</div>

Good day

I managed to get some modbus communcations to work on a power electronics drive. what is the actual address that you want to access perhaps I can help.

---

<div class="post-metadata">

**Author:** ![John\_Barry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/john_barry/32/28987_2.png) [@John\_Barry](https://discourse.nodered.org/u/John_Barry)\
**Post date:** [13 July 2020 07:39 UTC](https://discourse.nodered.org/t/modbus-tcp-array-to-readable-value/29851/4 "2020-07-13T07:39:19Z")

</div>

Hi,

This is the device and register  
Phoneix Contact: EEM-MA770  
Address (dec): 32780   
Address (hex): 0x800C   
Number of registers: 2   
Name: Frequency   
Unit: Hz   
Factor: 1   
Read only

A debug node returns this out of the Modbus read node:  
array[2]  
0: 15991  
1: 16968

After removing the msg.payload it works but sometimes it returns an error:  
"RangeError [ERR\_OUT\_OF\_RANGE]: The value of "value" is out of range. It must be \>= -32768 and \<= 32767. Received 33554"

When the value is in range i also need to round it off to 2 decimals from 50.01499938964844

---

<div class="post-metadata">

**Author:** ![John\_Barry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/john_barry/32/28987_2.png) [@John\_Barry](https://discourse.nodered.org/u/John_Barry)\
**Post date:** [13 July 2020 07:43 UTC](https://discourse.nodered.org/t/modbus-tcp-array-to-readable-value/29851/5 "2020-07-13T07:43:13Z")

</div>

```auto
[{"id":"3a59fc59.ee0454","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"74dd717f.076f2","type":"modbus-client","z":"","name":"Power meter","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"192.168.0.98","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true},{"id":"f90ef26a.a529a","type":"modbus-client","z":"","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"},{"id":"29344771.8d9648","type":"ui_group","z":"","name":"GSM network","tab":"5abf1f53.dc4c7","order":1,"disp":true,"width":"6","collapse":false},{"id":"60fd2c2e.5d5ce4","type":"ui_group","z":"","name":"Router","tab":"5abf1f53.dc4c7","order":2,"disp":true,"width":"6","collapse":false},{"id":"48110a04.687a34","type":"ui_group","z":"","name":"Data usage","tab":"5abf1f53.dc4c7","order":3,"disp":true,"width":"6","collapse":false},{"id":"bb35e927.f880e8","type":"modbus-client","z":"","name":"Router","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"192.168.1.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"},{"id":"5abf1f53.dc4c7","type":"ui_tab","z":"","name":"Router","icon":"router","order":5,"disabled":false,"hidden":false},{"id":"22905fa6.bb9e8","type":"modbus-client","z":"","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectTimeout":2000},{"id":"7abaf24b.aedc1c","type":"modbus-read","z":"3a59fc59.ee0454","name":"Frequency","topic":"Frequency","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"255","dataType":"HoldingRegister","adr":"32780","quantity":"2","rate":"5","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"74dd717f.076f2","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":500,"y":220,"wires":[["99cc33f3.dd3c9","68c1bd80.92d7f4"],["44ce6e95.72803"]]},{"id":"587f0034.ec73b","type":"debug","z":"3a59fc59.ee0454","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":300,"wires":[]},{"id":"44ce6e95.72803","type":"function","z":"3a59fc59.ee0454","name":"","func":"let pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeInt16BE(pay[0],2);\nbuf.writeInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":300,"wires":[["587f0034.ec73b"]]},{"id":"99cc33f3.dd3c9","type":"debug","z":"3a59fc59.ee0454","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":220,"wires":[]},{"id":"63368447.81be0c","type":"debug","z":"3a59fc59.ee0454","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":260,"wires":[]},{"id":"68c1bd80.92d7f4","type":"function","z":"3a59fc59.ee0454","name":"","func":"let pay = msg.payload;\n\nconst buf = Buffer.allocUnsafe(4);\nbuf.writeInt16BE(pay[0],2);\nbuf.writeInt16BE(pay[1],0);\n\nmsg.payload = buf.readFloatBE(0);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":260,"wires":[["63368447.81be0c"]]}]

```

---

<div class="post-metadata">

**Author:** ![John\_Barry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/john_barry/32/28987_2.png) [@John\_Barry](https://discourse.nodered.org/u/John_Barry)\
**Post date:** [13 July 2020 14:39 UTC](https://discourse.nodered.org/t/modbus-tcp-array-to-readable-value/29851/6 "2020-07-13T14:39:06Z")

</div>

Hi,  
Simple solution was to just make it a Uint and it works

```auto
let pay = msg.payload;

const buf = Buffer.allocUnsafe(4);
buf.writeUInt16BE(pay[0],2);
buf.writeUInt16BE(pay[1],0);

msg.payload = buf.readFloatBE(0);
return msg;

```

---

<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:** [13 July 2020 16:02 UTC](https://discourse.nodered.org/t/modbus-tcp-array-to-readable-value/29851/7 "2020-07-13T16:02:00Z")

</div>

So you changed the top line as I suggested?

> [@Steve-Mcl](#):
>
> instead of setting the msg.payload to an array, just don't. (e.g. delete the top line )

---

<div class="post-metadata">

**Author:** ![John\_Barry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/john_barry/32/28987_2.png) [@John\_Barry](https://discourse.nodered.org/u/John_Barry)\
**Post date:** [14 July 2020 14:05 UTC](https://discourse.nodered.org/t/modbus-tcp-array-to-readable-value/29851/8 "2020-07-14T14:05:26Z")

</div>

yes thanks that got the data into the function and then the data type just needed to be a uint

---

<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 July 2020 14:05 UTC](https://discourse.nodered.org/t/modbus-tcp-array-to-readable-value/29851/9 "2020-07-28T14:05:29Z")

</div>

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