# Write to Modbus TCP holding registers

**URL:** <https://discourse.nodered.org/t/write-to-modbus-tcp-holding-registers/56707>\
**Category:** Industrial\
**Created:** [15 January 2022 19:30 UTC](https://discourse.nodered.org/t/write-to-modbus-tcp-holding-registers/56707 "2022-01-15T19:30:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hjojohan1](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@hjojohan1](https://discourse.nodered.org/u/hjojohan1)\
**Post date:** [15 January 2022 19:30 UTC](https://discourse.nodered.org/t/write-to-modbus-tcp-holding-registers/56707/1 "2022-01-15T19:30:58Z")

</div>

Hi  
I want to create a ModbusTCP slave(server) using NodeRed where I can write values from my Lorawan gateway so that a PLC can connect to the modbus slave and retrieve the decoded data.

I am trying to write value 1601.5 to modbus holding register 400001 and 40002 what am i a doing wrong ? I have a PC program that connects to the modbus server and reads 40.001 to 40.010 but no value appears.

**Functioncode:**

var fc=16;  
var sa=40001;  
var addresses=2;  
var value=1601.5;  
buf=Buffer.alloc(4);  
buf.writeFloatBE(value);  
[//buf.writeFloatBE](https://buf.writeFloatBE)(16001.5);  
var values=[(buf[0]\*256+buf[1]),(buf[2]\*256)+buf[3]]  
msg.slave\_ip="192.168.1.31";  
msg.payload={"value":values , 'fc': fc, 'unitid': 1, 'address': sa , 'quantity': addresses };  
return msg;

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [15 January 2022 20:09 UTC](https://discourse.nodered.org/t/write-to-modbus-tcp-holding-registers/56707/2 "2022-01-15T20:09:44Z")

</div>

I dont have much experience with buffer methods .. does this work ?

```auto
var fc = 16;
var sa = 1;
var addresses = 2;

var value = 1601.5;
var buf = Buffer.alloc(4);
buf.writeFloatBE(value);
node.warn({ "buf": buf });

node.warn(buf.readInt16BE(2));
node.warn(buf.readInt16BE(0));

var values = [buf.readInt16BE(0), buf.readInt16BE(2)]
//msg.slave_ip = "192.168.1.31";
msg.payload = { "value": values, 'fc': fc, 'unitid': 1, 'address': sa, 'quantity': addresses };

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:** [15 January 2022 22:11 UTC](https://discourse.nodered.org/t/write-to-modbus-tcp-holding-registers/56707/3 "2022-01-15T22:11:02Z")

</div>

The node-red-contrib-buffer-parser package has a buffer maker node that simplifies all this data conversion. You could generate a buffer from your values then send that to a buffer parser node that converts the buffer to int16 values suitable for modbus.

---

<div class="post-metadata">

**Author:** ![hjojohan1](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@hjojohan1](https://discourse.nodered.org/u/hjojohan1)\
**Post date:** [15 January 2022 22:37 UTC](https://discourse.nodered.org/t/write-to-modbus-tcp-holding-registers/56707/4 "2022-01-15T22:37:50Z")

</div>

No I get no result on your code either

[[Album] imgur.com ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/20638d00ac54b6b6866379f84a8f7eed5fc2d72c.jpeg "imgur.com") ](https://imgur.com/a/PqwCvzB)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [15 January 2022 22:53 UTC](https://discourse.nodered.org/t/write-to-modbus-tcp-holding-registers/56707/5 "2022-01-15T22:53:24Z")

</div>

Thats strange because with my modbus simulator it was ok ..  
(by the way .. you can upload images directly on the forum .. no need for an external site)

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

Are you sure you have the IP of the device correctly configured in the modbus node and that you are on the same network ?  
i ask because i noticed you had in your code

> [@hjojohan1](#):
>
> msg.slave\_ip="192.168.1.31";

and in you last post

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

@Steve-Mcl i was going to recommend your node's but since Johan was already in a Function node i thought of having a go with JS ... Buffer-parser is a must when working with Modbus.

---

<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:** [16 January 2022 22:24 UTC](https://discourse.nodered.org/t/write-to-modbus-tcp-holding-registers/56707/6 "2022-01-16T22:24:13Z")

</div>

Hi I have had success by setting the MODBUS Read node like this. I am reading registers from a VSD. MODBUS can be tricky as some devices do not require the first digit like I am monitoring register 42006 but due to me selecting FC3 it nows that it starts with 40000 so I only needed to put in 2006. When I entered 42006 it was looking at 442006. Sometimes the registers need to be read 1 less than actual so the manual indicated that I need to read 42007 but to read it you need to enter 1 less.

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/0/5076cee70080230de0bc52293309e4cb8ac7d146.jpeg)

---

<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:** [17 March 2022 22:25 UTC](https://discourse.nodered.org/t/write-to-modbus-tcp-holding-registers/56707/7 "2022-03-17T22:25:05Z")

</div>

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