# Forward Port to Modbus TCP

**URL:** <https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976>\
**Category:** General\
**Created:** [14 March 2019 12:56 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976 "2019-03-14T12:56:27Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![workano](https://avatars.discourse-cdn.com/v4/letter/w/74df32/32.png) [@workano](https://discourse.nodered.org/u/workano)\
**Post date:** [14 March 2019 12:56 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/1 "2019-03-14T12:56:28Z")

</div>

Hello all, I have a Modbus device capable to connect to a remote IP/Port. I would like to to connect it to Node-Red and bind this port to Modbus TCP.

I did a connection to Node-Red TCP listener, but I do not know how to read it from Modbus.

Any idea is welcome.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [14 March 2019 13:09 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/2 "2019-03-14T13:09:48Z")

</div>

Hello @workano, welcome to Node-RED forum.

You can do that very easily by installing and using the `node-red-contrib-mobus`.

> **[node-red-contrib-modbus](https://flows.nodered.org/node/node-red-contrib-modbus)**
>
> The all in one Modbus TCP and Serial contribution package for Node-RED.

---

<div class="post-metadata">

**Author:** ![workano](https://avatars.discourse-cdn.com/v4/letter/w/74df32/32.png) [@workano](https://discourse.nodered.org/u/workano)\
**Post date:** [14 March 2019 13:19 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/3 "2019-03-14T13:19:47Z")

</div>

Hi Andrei, thank you for your quick answer. I am using node-red-contrib-mobus, I can read and write to the device from Node-Red but could not "listen" for a connection from device.

I have connected the device via TCP Listen but did not realize how to bind this connection to Modbus.

Is it possible for you to send a flow or tell me how should I procede?  
Thank you so much for your help.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [14 March 2019 13:49 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/4 "2019-03-14T13:49:23Z")

</div>

Please share more information on how you configured the TCP node and what happened. Knowing what failed may help us to find a solution. The two most common issues when trying to connect a device with TCP is permission issues when using ports below 1024 and missing to add line feed when sending commands to the device. What is the model of the device you are trying to connect to?

---

<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:** [14 March 2019 14:57 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/6 "2019-03-14T14:57:07Z")

</div>

Do you mean you want the device running node-red to act as a modbus server rather than client?

---

<div class="post-metadata">

**Author:** ![workano](https://avatars.discourse-cdn.com/v4/letter/w/74df32/32.png) [@workano](https://discourse.nodered.org/u/workano)\
**Post date:** [14 March 2019 15:01 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/7 "2019-03-14T15:01:05Z")

</div>

No, I just want to bind two TCP ports.  
Eg. My device will conect to the port 8502 and Node-Red will connect to the port 8505.  
I want to bind these two ports so Node-Red could pool the device.

---

<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:** [14 March 2019 15:09 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/8 "2019-03-14T15:09:48Z")

</div>

> [@workano](#):
>
> Eg. My device will conect to the port 8502 and Node-Red will connect to the port 8505.  
> I want to bind these two ports so Node-Red could pool the device.

OK, so you want the node red device to listen on port 8502 using TCP? You can use the TCP In node for that. That is one of the built-in nodes.

---

<div class="post-metadata">

**Author:** ![workano](https://avatars.discourse-cdn.com/v4/letter/w/74df32/32.png) [@workano](https://discourse.nodered.org/u/workano)\
**Post date:** [14 March 2019 15:37 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/9 "2019-03-14T15:37:54Z")

</div>

Yes, I did that. I do not know how to bind the two ports.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [14 March 2019 15:41 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/10 "2019-03-14T15:41:44Z")

</div>

> [@workano](#):
>
> I did a connection to Node-Red TCP listener, but I do not know how to read it from Modbus.

Can you share a picture from your flow ? I wanted to understand what you mean by "Node-RED TCP listener". I dont understand if you are trying to use a TCP node or using only Modbus contrib node. It would be easy to help you if we have a clear picture of what you already did instead of trying to guess.

Anyway...a new guess on my side. As you are configuring you modbus gateway as a TCP client then you may want to use the Modbus server for testing the connection with your gateway device. Have you already tried this node ?

![r-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/550a2fd21d0d363c709f898efcea374fddadd5e0.png)

---

<div class="post-metadata">

**Author:** ![workano](https://avatars.discourse-cdn.com/v4/letter/w/74df32/32.png) [@workano](https://discourse.nodered.org/u/workano)\
**Post date:** [14 March 2019 15:55 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/12 "2019-03-14T15:55:36Z")

</div>

I already use this function on ScadaBR. It has Modbus Listener option.  
It just waits for a connection and then start the modbus pooling.

---

<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:** [14 March 2019 16:09 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/14 "2019-03-14T16:09:56Z")

</div>

Are all the node-red elements in that diagram in the same computer?  
Also where is the modbus server in this?

---

<div class="post-metadata">

**Author:** ![workano](https://avatars.discourse-cdn.com/v4/letter/w/74df32/32.png) [@workano](https://discourse.nodered.org/u/workano)\
**Post date:** [14 March 2019 16:11 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/15 "2019-03-14T16:11:36Z")

</div>

No, PLC is a phisical device, it is the server.  
Node-red elements are the TCP listeners and the Modbus-read.

---

<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:** [14 March 2019 16:14 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/16 "2019-03-14T16:14:02Z")

</div>

> [@workano](#):
>
> No, PLC is a phisical device, it is the server.

But you have the plc labelled as a modbus client, not a modbus server. Redraw it with extra lines round the physical devices and draw the arrows client \> server.

---

<div class="post-metadata">

**Author:** ![workano](https://avatars.discourse-cdn.com/v4/letter/w/74df32/32.png) [@workano](https://discourse.nodered.org/u/workano)\
**Post date:** [14 March 2019 16:24 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/18 "2019-03-14T16:24:30Z")

</div>

Yes, I believe so, thank you so much for your help.

---

<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:** [14 March 2019 16:27 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/19 "2019-03-14T16:27:51Z")

</div>

So is that three separate devices running node-red?  
The modbus client is the one that issues, for example, coil read requests. The Server is the one that provides the coil data back to the client. So who in the diagram is asking for the data and who is providing the data?

---

<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:** [14 March 2019 16:49 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/20 "2019-03-14T16:49:24Z")

</div>

You still haven't explained fully which is the modbus server/master (the one providing the data) and what is asking for the data.  
You can carry on editing the previous post.  
If the PLC/Gateway is actually the master then which port is it listening on?

---

<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:** [14 March 2019 16:56 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/21 "2019-03-14T16:56:57Z")

</div>

I have just looked back at your earlier posts. You said that if you configure the gateway as a server then you can read and write to it from node-red. In this situation presumably you are reading data that is in the PLC (which is the actual server, the gateway is just a gateway to that server).  
Why do you want to change the gateway to be a client? Surely that is only valid if the PLC is operating as a client.

**[Edit]** I have been blocked too now, for posting too many without a reply.

> [@workano](#):
>
> I just need to know how to listen on 2 TCP ports and bind then. Something like SOCAT does, but using node-red.

You mean you want the node-red device to operate as a router? Passing any data it receives on one port to the other?

---

<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:** [14 March 2019 17:30 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/23 "2019-03-14T17:30:49Z")

</div>

> [@workano](#):
>
> I tried many ways. Could you please provide a flow for me?

Sorry, I don't know how to make a router with node red.  
So I think what you are actually trying to do is connect a modbus read node to an incoming connection, but I don't know any other way of doing that either.  
Perhaps someone with more knowledge of networking can help.

---

<div class="post-metadata">

**Author:** ![thatcadguy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thatcadguy/32/5273_2.png) [@thatcadguy](https://discourse.nodered.org/u/thatcadguy)\
**Post date:** [16 March 2019 03:35 UTC](https://discourse.nodered.org/t/forward-port-to-modbus-tcp/8976/24 "2019-03-16T03:35:08Z")

</div>

I'm not sure I fully understand what is desired here, but as far as forwarding TCP requests, I was able to do this using:

TCP In (stream of buffers) -\> TCP request to Modbus server (return after timeout) -\> TCP out (Reply to TCP)

In my setup, I had Node-RED listen (the TCP In) on port 7000, and the TCP request went to a local Modbus server running on port 7001. You'd then have to decode the responses from the TCP request node.

You could also decompose the incoming request, compose a new request with a Modbus node, and then compose the response needed for the TCP out. Doesn't really look like there's an elegant solution here without getting dirty.
