Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help needed ECONNREFUSED #518

Open
maximaexchen opened this issue Oct 16, 2023 · 2 comments
Open

Help needed ECONNREFUSED #518

maximaexchen opened this issue Oct 16, 2023 · 2 comments

Comments

@maximaexchen
Copy link

maximaexchen commented Oct 16, 2023

Hi everybody,
so I am new to this topic and got stocked.
I have the case that I have to control 8 devices with 8 different ip addresses.

So if I try to connect the 8 devices, I get the error e.g. if one is not reachable.


Error: connect ECONNREFUSED 192.168.0.9:502
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '192.168.0.9',
  port: 502
}


I trigger the script from a ionic app via api calls .
If the device on 192.168.0.9 is not available all other devices stop readHoldingRegisters.
If I only trigger the one on ip 192.168.0.10, which is available, I get the data from readHoldingRegisters

on node server:
app.post('/api/sps/connect', function (req, res) {
        let data = req.body.data[0];
        let ipAddressSPS = data.ipAddressSPS;
        let ovenPosition = data.ovenPosition;

        client.connectTCP(ipAddressSPS, { port: 502 })
        .then(function() {
            client.setID(ovenPosition);
            //client.setTimeout(1000);
        })
        .then(function() {
            console.log("Connected");
            console.log('ClientID: ' + client.getID());
            readRegisters();
        })
        .catch(function(e) {
            if(e.errno) {
                if(networkErrors.includes(e.errno)) {
                    console.log("we have to reconnect");
                }
            }
            console.log('Error');
            console.log('ClientID: ' + client.getID());
            console.log(e.message); 
            //client.close();
        });       
    });

    function readRegisters() {
        setInterval(function () {
            client.readHoldingRegisters(0, 24)
            .then(function(d) {
                console.log('-------------------------------');
                console.log("Receive:", d.data); 
                console.log('ClientID: ' + client.getID());
                console.log('-------------------------------');
            })
            .catch(function(e) {
                console.log(e.message); 
            });
        }, 1000);
    }

How could I catch the error and only start the client if it is available?

Here seems to be a similar problem, but I did't understand the suggested implementation.
#469

Any other suggestions?
Any help appreciated!
Thanks in advance!

@yaacov
Copy link
Owner

yaacov commented Oct 16, 2023

did you try to create a new client for each new IP you get ?
e.g. keep a dictionary of clients, ip as key, and client as value

the example you refer too, is handling connection errors, so if you get an error, it will close the connection and re-open it, this may also help

@maximaexchen
Copy link
Author

Hey @yaacov
Thank you very much for the very quick reply!
This saved my day. I was thinking too difficult.
I did it like you suggested with a dictionary of clients.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants