NodeJS Modbus Master 从多个 IP 地址读取
NodeJS Modbus Master Read From multiple IP Adresses
我正在一个使用 jsmodbus (https://github.com/Cloud-Automation/node-modbus) 模块的 modbus 项目中工作,这样我就可以使用 readHoldingRegister 函数:
var modbus = require('jsmodbus');
function read(station, callback){
var client = modbus.client.tcp.complete({
'host' : station.ipAdress,
'port' : station.port, //502
'autoReconnect' : true,
'reconnectTimeout' : 1000,
'timeout' : 5000,
'unitId' : 0
});
client.connect();
client.on('connect', function () {
client.readHoldingRegisters(startReg, endReg).then(function (resp) {
return callback(resp);
}).fail(console.log);
});
client.on('error', function (err) {
retrun callback(null);
});
}
我需要为我拥有的 IP 数量执行此操作,并将每个 "resp" 结果推送到一个数组 "allResults" 中,然后是 return,但我没有得到结果当我将上面的代码放在 for 循环中时 async.each.
readAll(ip_adresses, (allResults) => {
doSomethingWith(allResults);
});
这样做的正确方法是什么?
像下面这样尝试
var async = require('async');
...
function read(station, callback) {
...
}
async.map(ips, read, function(err, results) {
if (err)
return console.log(err);
...
// Process results
})
在 promise
中包装 read
function read(station) {
return new Promise((resolve, reject) => {
...
client.readHoldingRegisters(startReg, endReg).then(function (resp) {
// here is the point
// in every promise, after resolve, the result will transform to `then` function
// and you will get the result via `read(station).then(result)`
return resolve(resp);
}).fail(console.log)
});
}
function readAll(stations) {
// the stations contain multiple ip, such as [ip1, ip2, ip3...]
// the map function transform every ip to a promise
// use Promise.all to run every promise
// and then, get the results
var readTasks = stations.map(read);
Promise.all(readTask)
.then(allResults => {
doSomethingWith(allResults);
});
}
我正在一个使用 jsmodbus (https://github.com/Cloud-Automation/node-modbus) 模块的 modbus 项目中工作,这样我就可以使用 readHoldingRegister 函数:
var modbus = require('jsmodbus');
function read(station, callback){
var client = modbus.client.tcp.complete({
'host' : station.ipAdress,
'port' : station.port, //502
'autoReconnect' : true,
'reconnectTimeout' : 1000,
'timeout' : 5000,
'unitId' : 0
});
client.connect();
client.on('connect', function () {
client.readHoldingRegisters(startReg, endReg).then(function (resp) {
return callback(resp);
}).fail(console.log);
});
client.on('error', function (err) {
retrun callback(null);
});
}
我需要为我拥有的 IP 数量执行此操作,并将每个 "resp" 结果推送到一个数组 "allResults" 中,然后是 return,但我没有得到结果当我将上面的代码放在 for 循环中时 async.each.
readAll(ip_adresses, (allResults) => {
doSomethingWith(allResults);
});
这样做的正确方法是什么?
像下面这样尝试
var async = require('async');
...
function read(station, callback) {
...
}
async.map(ips, read, function(err, results) {
if (err)
return console.log(err);
...
// Process results
})
在 promise
中包装 readfunction read(station) {
return new Promise((resolve, reject) => {
...
client.readHoldingRegisters(startReg, endReg).then(function (resp) {
// here is the point
// in every promise, after resolve, the result will transform to `then` function
// and you will get the result via `read(station).then(result)`
return resolve(resp);
}).fail(console.log)
});
}
function readAll(stations) {
// the stations contain multiple ip, such as [ip1, ip2, ip3...]
// the map function transform every ip to a promise
// use Promise.all to run every promise
// and then, get the results
var readTasks = stations.map(read);
Promise.all(readTask)
.then(allResults => {
doSomethingWith(allResults);
});
}