# Modbus flex-server ECONNREFUSE

**URL:** https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721
**Category:** General
**Tags:** modbus
**Created:** [23 April 2025 12:58 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721 "2025-04-23T12:58:33Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)
#### Post date: [23 April 2025 12:58 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/1 "2025-04-23T12:58:33Z")

</div>

I have a server with a modbus-contrib-flex-getter that reads from another server Modbus-contrib-flex-server.

Regularly (once or twice a day, cant see a pattern to it) I get an error "ECONNREFUSED" from the server that hosts the getter.

Sometimes it is enough to redeploy the flex-server node-red, but sometimes I have to disable the flex-server node and enable it again.

The inject for inserting buffer into flex-server is set 100ms apart.I don't know if that matters anything though.

Node-red V4.0.2 (flex-server host)  
Flex-server V1.0.1

 ![flex-server](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7ab14afd64ee7e9d7fbc884870af274ffb247b61.png)  
 ![flex-getter](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5add1603f11d10569755b534de99fbc9b7603936.png)

---

<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: [23 April 2025 13:11 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/2 "2025-04-23T13:11:30Z")

</div>

> [@stgj](#):
>
> ![flex-getter](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5add1603f11d10569755b534de99fbc9b7603936.png)
> 
> flex-getter1007×560 132 KB

This looks awfully complex - and possibly not necessary. The loops are certainly **not** recommended. What is this doing? Perhaps you could share this part of the flow?

---

<div class="post-metadata">

### Author: ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)
#### Post date: [23 April 2025 13:52 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/3 "2025-04-23T13:52:20Z")

</div>

I forgot to include the inject node that starts the flow. It is set to every 10sec.

Function 300:  
First the flow sets up a request object and prepares some influx data.

```auto
const fields = context.get("Input")

msg.payload = {
    'fc': 4,
    'unitid': 1,
    'address': 0,
    'quantity': fields.length
}

msg.influxObj = {
    Temperature: {},
    SetValues: {},
    Status: {},
    Alarms: {},
    PointAlarms: {}
}

msg.fields = fields

msg.trip = 1

return msg;

```

The flow then goes to the getter.  
The switch node receives the payload from the getter and sends the flow on where msg.trip is equal to 1.  
The next function node then process the data and makes another request object.

```auto
const obj = {}
const fields = msg.fields

msg.payload.data.forEach((data, index) => {
    if(fields[index].field == "PID effect") {
        obj[fields[index].field] = data
        return
    }
    obj[fields[index].field] = Number(calculateFromRawValueMA(data).toFixed(1))
})

msg.influxObj.Temperature = obj

msg.fields = context.get("Holding")

msg.payload = {
    'fc': 3,
    'unitid': 1,
    'address': 0,
    'quantity': msg.fields.length
}

msg.trip = 2

return msg;

function calculateFromRawValueMA(mA) {
    // 4-20mA signal
    // -50 to 150 range
    const PvHigh = 150 // Process value high
    const PvLow = -50 // Process value low
    const Ihigh = 20000 // mA high
    const Ilow = 4000 // mA low
    const I = parseInt(mA) // Amp/Process value

    const Pv = (PvHigh - PvLow)/(Ihigh - Ilow)*(I-Ilow)+PvLow // Process value
    return Pv
}

```

Then the flow goes to a 100ms delay to the getter.  
This repeats itself until the end, where all the data is formatted pr influxdb standard and sent to influx.

The reason for the number of loops is requesting between different modbus functions and some of them because of several thousands difference in modbus addresses in the same modbus function...  
if that makes any sense.

Best regards  
Steffen

---

<div class="post-metadata">

### Author: ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)
#### Post date: [6 May 2025 07:16 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/4 "2025-05-06T07:16:42Z")

</div>

Hi Steve.

How would you divide the differences in modbus function(coil, register etc) if not like this?

Best regards  
Steffen

---

<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: [6 May 2025 07:34 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/5 "2025-05-06T07:34:25Z")

</div>

Before I can suggest any architectural change, I need to ask, are you using modbus as a means of sharing data between different node-red instances?

Based on your opening statement:

> I have a server with a modbus-contrib-flex-getter that reads from another server Modbus-contrib-flex-server

It looks like that is the case. And if this is purely for node-red to node-red communication, there are far better ways to achieve this. Please appreciate that I don't have all of your details or end goal so it might be totally necessary. Please clarify.

---

<div class="post-metadata">

### Author: ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)
#### Post date: [6 May 2025 08:02 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/6 "2025-05-06T08:02:30Z")

</div>

I am using node-red as a modbus client in this example. But that's for testing purposes only.

My device uses node-red, flex-server.  
As a communication protocol the requirement is modbus TCP/IP.  
In a production environment some form of modbus client is used. Not Node-red.

Best regards  
Steffen

---

<div class="post-metadata">

### Author: ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)
#### Post date: [12 May 2025 12:31 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/7 "2025-05-12T12:31:01Z")

</div>

As far as I know, it is not normal (or possible) to make requests across different function codes in one request.

I could make multiple requests at the same time. That is, not waiting for one request to finish before I make another one. In that way I would not need a loop like I'm using now.  
I thought that a loop was the way to go. Would you rather make request at the same time?  
Like requesting a data block in the holding register from address 1000 - 1080.  
And at the same time request some coils from 500 - 550.  
And some Input register from 0 - 60.

The modbus contrib flex getter takes care of this for me?

Best regards  
Steffen

---

<div class="post-metadata">

### Author: ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)
#### Post date: [15 May 2025 06:46 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/8 "2025-05-15T06:46:57Z")

</div>

I had my Node-red editor open when the error occurred today.  
The error is:  
"Error: read ECONNRESET". This is outputed by the flex-server node.  
I have an error Catch node attached to the flex-server, but the error was not catched by the Catch node.

For the client node-red to be able to get responses from modbus requests I had to redeploy the node-red where the flex-server resides.

Everything else works. Just the flex-server that doesn't respond.

Best regards  
Steffen

---

<div class="post-metadata">

### Author: ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)
#### Post date: [18 June 2025 05:28 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/9 "2025-06-18T05:28:50Z")

</div>

Did som more research today.  
It looks like the node-red/flex-server/linux-server responds with an RST, ACK.

When I do:

> netstat -an | grep :11502

There is nothing.  
11502 is the port that the flex-server is bound to.

Then, this time, I redeploy node-red and everything works.

This time: netstat -an | grep :11502

> tcp 0 0 0.0.0.0:11502 0.0.0.0:\* LISTEN  
> tcp 0 0 192.168.1.3:11502 10XX.XX.XX:39674 ESTABLISHED

Why would the flex-server/node-red suddenly stop responding?  
Any thoughts?

Best regards  
Steffen

---

<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: [18 June 2025 07:37 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/10 "2025-06-18T07:37:47Z")

</div>

> [@stgj](#):
>
> I could make multiple requests at the same time. That is, not waiting for one request to finish before I make another one. In that way I would not need a loop like I'm using now.

> [@stgj](#):
>
> at the same time request some coils from 500 - 550.  
> And some Input register from 0 - 60.
> 
> The modbus contrib flex getter takes care of this for me?

yes the Modbus Flex Getter node should be able to queue the reads with its internal queue as far as you have the option enabled in Modbus Config to do so

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

> [@stgj](#):
>
> "Error: read ECONNRESET". This is outputed by the flex-server node.

Have you checked your network connection .. are you sure its not a network problem ?

---

<div class="post-metadata">

### Author: ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)
#### Post date: [18 June 2025 10:16 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/11 "2025-06-18T10:16:38Z")

</div>

It's not a network problem. I reach the linux machine fine. As I said, when I redeploy node-red the modbus flex-server start responding. If there was a network problem I could not have reached the node-red editor.

Best regards  
Steffen

---

<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: [18 June 2025 11:38 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/12 "2025-06-18T11:38:23Z")

</div>

Maybe this github issue is related to what you see with ECONNRESET

> <https://github.com/BiancoRoyal/node-red-contrib-modbus/issues/326>
>
> \### Which node-red-contrib-modbus version are you using?
> 
> 5.23.1
> 
> \### What happe…ned?
> 
> After the ethernet link fails there is a random chance that the Modbus Flex Server stops listening to the designated port without error or any kind of warning. This was tested on a raspberry pi by directly disconnecting the ethernet cable and approximately half of the time this would happen.
> 
> \### Server
> 
> None/This is related to a node that doesn't connect to a server
> 
> \### How can this be reproduced?
> 
> Set up a flex server node on the machine IP static address and any port (let us say 11502). It can be seen the server is listening on the port by running on a linux platform
> netstat -tnlp | grep :11502
> After disconnecting for ~4 seconds the ethernet cable and reconnecting it there is ~50% chance that the node will stop listening, by shown by the above command, while it still shows up as active on NodeRED.
> 
> I am not able to test it on any other machine at the moment, I would appreciate feedback on reproducibility.
> 
> \### What did you expect to happen?
> 
> The node should issue the underlying net.Server to listen again on the port (maybe in response to a 'close' event?)
> 
> \### Other Information
> 
> Platform: raspbian on raspberry pi 4B
> Node-RED version: 3.0.2
> NodeJS version: 16.17.0

but as usual the issue was autoclosed with no apparent fix ..

There is also a separate server node .. [Modbus Flex Server](https://flows.nodered.org/node/node-red-contrib-modbus-flex-server) by the same developer. This node also seems to suffer by the same issue not recovering after a socket error. There is a suggested fix though if you want to fork the repo and try it yourself.

> <https://github.com/BiancoRoyal/node-red-contrib-modbus-flex-server/issues/16>
>
> \### Which node-red-contrib-modbus version are you using?
> 
> 1.01 Modbus Flex
> 
> \### …What happened?
> 
> We noticed that a PLC was abruptly interrupting the connection upon loading a new PLC program. This caused a socketError on the node's side. The flex tries to restart the server but the port 502 never re-opens. The startServer is indeed called after the some cleanup by modbus-serial and net but I dont believe it currently does anything other than say "listening on port 502". I nulled the modbusServer object before the startServer call so that it passes the check on line 83 and a establishes a new connection.  
> 
> 
> \`\`\`
> node.modbusServer.on('socketError', function (err) {
> internalDebugLog(err.message)
> if (node.showErrors) {
> node.warn(err)
> }
> mbBasics.setNodeStatusTo('error', node)
> 
> node.modbusServer.close(function () {
> node.modbusServer = null /////////////my "fix"
> node.startServer()
> })
> })
> 
> \`\`\`
> 
> 
> The comment below on modbus-serial might be worth looking at. Is it guaranteed that the sockets are destroyed prior to the callback(startServer on flex side) is started? Does it matter? 
> 
> 
> \`\`\`
> close(callback) {
> const modbus = this;
> 
> // close the net port if exist
> if (modbus.\_server) {
> modbus.\_server.removeAllListeners("data");
> modbus.\_server.close(callback);///this is async and may be executing the callback before sockets are destroyed??..
> 
> modbus.socks.forEach(function(e, sock) {
> sock.destroy();
> });
> 
> modbusSerialDebug({ action: "close server" });
> } else {
> modbusSerialDebug({ action: "close server", warning: "server already closed" });
> }
> }
> \`\`\`
> 
> 
> Please let me know your thoughts and thank you!
> 
> 
> 
> \### Server
> 
> Modbus-Flex-Server Node (Please attach Script to the next section)
> 
> \### How can this be reproduced?
> 
> Try to trigger a socketError. I use a wago cc-100 with a couple of modbus writing channels connected the flex server. I would reflash the plc while in run mode while ignoring warning by codesys.
> 
> \### What did you expect to happen?
> 
> \_No response\_
> 
> \### Other Information
> 
> \_No response\_

---

<div class="post-metadata">

### Author: ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)
#### Post date: [18 June 2025 17:28 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/13 "2025-06-18T17:28:55Z")

</div>

Thanks a lot, UnborN! Great find.

Looks like I’m not alone after all.  
I’ve seen this type of error mentioned in older posts where the creator provided a fix.  
I thought this bug had already been resolved by now.

I will see if I can use the temporary fix.

Best regards.  
Steffen

---

<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: [18 June 2025 18:25 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/14 "2025-06-18T18:25:43Z")

</div>

I remember also them fixing something related but i think it was for the Read nodes not reconnecting ?

I wish someone with the experience in programming could contribute to these nodes, since Modbus is still widely used in industrial applications .. hint hint 😉

---

<div class="post-metadata">

### Author: ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)
#### Post date: [19 June 2025 04:26 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/15 "2025-06-19T04:26:47Z")

</div>

I think I have been able to edit the file in question with the fix.  
Now I have to wait for a week to se.

Best regards  
Steffen

---

<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: [8 July 2025 16:01 UTC](https://discourse.nodered.org/t/modbus-flex-server-econnrefuse/96721/16 "2025-07-08T16:01:56Z")

</div>

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