# Modbus TCP Server on port 502

**URL:** <https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760>\
**Category:** General\
**Created:** [10 November 2018 21:47 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760 "2018-11-10T21:47:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![victor](https://avatars.discourse-cdn.com/v4/letter/v/3e96dc/32.png) [@victor](https://discourse.nodered.org/u/victor)\
**Post date:** [10 November 2018 21:47 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/1 "2018-11-10T21:47:05Z")

</div>

Hi .  
I am trying to create a TCP modbus server on standard port 502, but when using this port , I do not know why but the system crash.  
Does anyone have experience in this matter?

Thanks

---

<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:** [10 November 2018 22:06 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/2 "2018-11-10T22:06:09Z")

</div>

How are you trying to create the modbus TCP server? If you are using a node-red node then tell us which one (node-red-contrib-something possibly).  
Assuming the question does involve node red then seeing the node-red log might be very helpful. How to get that depends on your operating system. If the mechanism that you use for running node-red shows you message starting with Welcome to Node Red then that is what we want. If you are not seeing that then possibly running, in a terminal window the commands

```bash
node-red-stop
node-red-start

```

will show it. If not tell us the OS.  
If you can see the log then first look to see if there are any messages that tell you what the problem is, otherwise copy/paste the log here down to the point that it crashes. If the log does not show any evidence of the crash explain exactly what you mean by crashing.

---

<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:** [19 January 2019 05:22 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/3 "2019-01-19T05:22:31Z")

</div>

Not to bump an old thread, but for anyone who comes searching, the most likely culprit is needing root access to listen on a port less than 1024, assuming you're running Node-RED on a Linux device.

---

<div class="post-metadata">

**Author:** ![jimmy232](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jimmy232/32/26286_2.png) [@jimmy232](https://discourse.nodered.org/u/jimmy232)\
**Post date:** [16 March 2021 09:09 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/4 "2021-03-16T09:09:12Z")

</div>

I am using the node-red-contrib-modbus and every time I had a Modbus Server node and give it an IP it crashes. I have tried this on several PI 3B+ 32 systems.  
Each time I have to delete the flow and reimport it to get the flow to work again.  
It has nothing to do with ports less than 1024 either as I have used the default 10502 with it still crashing Node Red.

It sounds like a very useful node if it would work.

---

<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:** [16 March 2021 09:37 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/5 "2021-03-16T09:37:05Z")

</div>

Since you say this is not related to the port then it is not related to this problem. Please start a new thread.  
When you get a crash like this you can start node red using  
`node-red --safe`  
which will start it without running the flows. When you deploy, it will start the flows and you may well get further information on the problem in the console.  
But as I said, please start a new thread after you have done this, if you still need help.

I notice that on both your previous posts you asked a question but then never replied to the attempts to help you. That does not make it more likely that others will try to help you here.

---

<div class="post-metadata">

**Author:** ![jimmy232](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jimmy232/32/26286_2.png) [@jimmy232](https://discourse.nodered.org/u/jimmy232)\
**Post date:** [16 March 2021 10:01 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/6 "2021-03-16T10:01:47Z")

</div>

Thanks Colin,

I am fairly new to the whole forum thing and I consider your response to be most admirable.

Thanks again, and I will try to follow protocol a bit better in future 😊

 ![1DBDCCB5B6C74A51A4340270580B4443.png](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/7159a975d714a6caee787759490140daf4d4fda6.png)

---

<div class="post-metadata">

**Author:** ![jimmy232](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jimmy232/32/26286_2.png) [@jimmy232](https://discourse.nodered.org/u/jimmy232)\
**Post date:** [16 March 2021 11:18 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/7 "2021-03-16T11:18:23Z")

</div>

Hi again,

Turns out the problem it is a on port 502 and could potentially be resolved with @thatcadguy suggestion.  
I don't really know how to give root access to individual nodes within Node Red?  
This issue only conflicts with the Modbus server node, as I am able to use the read and write nodes on port 502

---

<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:** [16 March 2021 11:42 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/8 "2021-03-16T11:42:35Z")

</div>

Why do you need to use that port?

---

<div class="post-metadata">

**Author:** ![jimmy232](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jimmy232/32/26286_2.png) [@jimmy232](https://discourse.nodered.org/u/jimmy232)\
**Post date:** [16 March 2021 12:06 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/9 "2021-03-16T12:06:52Z")

</div>

Existing PLCs on the network are preconfigured to write to port 502. The code is all locked out with target IP address being the only editable text field in the HMI.

Nearly all Modbus clients are programmed to use port 502 for Modbus.

---

<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:** [16 March 2021 12:31 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/10 "2021-03-16T12:31:31Z")

</div>

You need to permit non root user to bind to port \< 1024. Search this forum and google. Been covered a few times.

something like `sudo setcap 'cap_net_bind_service=+ep' `which node` ` or `sudo setcap 'cap_net_bind_service=+ep' $(eval readlink -f `which node`)` should do the trick

Read [this thread](https://discourse.nodered.org/t/use-port-502-with-modbus-tcp/39615/13)

and [this](https://gist.github.com/firstdoit/6389682)

---

<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:** [16 March 2021 12:35 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/11 "2021-03-16T12:35:43Z")

</div>

Or configure your firewall to redirect that port to a higher one.

---

<div class="post-metadata">

**Author:** ![jimmy232](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jimmy232/32/26286_2.png) [@jimmy232](https://discourse.nodered.org/u/jimmy232)\
**Post date:** [17 March 2021 11:29 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/12 "2021-03-17T11:29:43Z")

</div>

Thanks @StevecI,

But I was still unable to stop the crash from happening.  
I tried to following suggested commands and still no luck.

\<  
pi@SESS:~ sudo setcap 'cap\_net\_bind\_service=+ep' /usr/bin/node pi@SESS:~ sudo setcap -v 'cap\_net\_bind\_service=+ep' /usr/local/bin/node  
/usr/local/bin/node differs in [pe]  
pi@SESS:~ sudo setcap 'cap\_net\_bind\_service=+ep' (eval readlink -f `/usr/bin/node`)  
^C  
pi@SESS:~ $

/\>

---

<div class="post-metadata">

**Author:** ![jimmy232](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jimmy232/32/26286_2.png) [@jimmy232](https://discourse.nodered.org/u/jimmy232)\
**Post date:** [17 March 2021 12:09 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/13 "2021-03-17T12:09:15Z")

</div>

Ok cool!  
It appears everything is now functioning properly and I am able to maintain a stable Modbus server.

Thanks for your assistance 🙂

---

<div class="post-metadata">

**Author:** ![OscarVasta](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oscarvasta/32/62116_2.png) [@OscarVasta](https://discourse.nodered.org/u/OscarVasta)\
**Post date:** [24 July 2022 12:31 UTC](https://discourse.nodered.org/t/modbus-tcp-server-on-port-502/4760/14 "2022-07-24T12:31:26Z")

</div>

Tengo un problema similar de comunicion con modbus y no logro que el puerto 502 aparesca abierto a pesar que el firewall lo ha abierto.  
Lei como lo soluciono Ud. pero no puedo entender nadas de lo que hizo.  
Seria tan amable de explicarl mas ampliamente lo que hizo para lograr que funcionara ?
