문제

I was in need of a Modbus module to use in Node and I found a somewhat light-weight solution that works perfectly. Its Modbus-Stack for Node by TooTallNate. (https://github.com/TooTallNate/node-modbus-stack)

I implemented it and it seems to work. Using my browser I make a call to my Node server then using Wireshark I see the Modbus packet go from my Node server (modbus master) to another device (modbus slave) then I see another Modbus packet go from the device to the Node server.

But the server would never be able to return the data to my browser. Turns out its the Node server was listening and prepared to handle 'response' event but was getting 'error' event. When I handled the 'error' event I saw the following (stringified):

{"code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect"}

Node code:

    var http = require("http");
    var querystring = require("querystring");
    var url = require("url");
    var fs = require("fs");
    var RIR = require("modbus-stack").FUNCTION_CODES.READ_HOLDING_REGISTERS;

    http.createServer(onRequest).listen(8080);
    function onRequest(request, response) {

        var query = url.parse(request.url).query;
        var path = url.parse(request.url).pathname;
        var wants;

        if(path == '/mbust'){     
            mbReturn(function(data){
            response.writeHead(200,{"content-Type":"text"});
            response.end(data);
        });
        }
        else{
            wants = 'unknown request'+" "+path+" "+query;
            response.end(wants);
        }

    }

    function mbReturn(pasd){
        var client = require("client").createClient(502,'192.168.1.199');
        var req = client.request(RIR,2000,1);
        req.on('response',function(registers){
            pasd(registers);
            client.end();
        });
        req.on('error',function(e){
            pasd(JSON.stringify(e));
            client.end();
        });
    }

And that's where I am stuck. Any ideas/tips on how to debug this would really help. This is kind of unfamiliar territory for me so really anything would help.

TIA!

Niko

update: I called e.stack to see the lineage of the error calls and this is what I got:

    Error: connect ECONNREFUSED
        at errnoException (net.js:776:11)
        at Object.afterConnect [as oncomplete] (net.js:767:19)
도움이 되었습니까?

해결책

Turns out that I just needed to make sure that there were no other devices trying to be the modbus device's master; the modbus slave can only have one master.

Also I had to move the create client code out of the function that was called whenever an HTTP call was made to the server so that Node would create one client to be the modbus slave and rely on that for all following modbus transactions.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top