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)