-
-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from yaacov/add-async-server-example
add a server example
- Loading branch information
Showing
4 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* eslint-disable no-console, no-unused-vars, spaced-comment */ | ||
|
||
// create an empty modbus client | ||
//var ModbusRTU = require("modbus-serial"); | ||
var ModbusRTU = require("../index"); | ||
var vector = { | ||
getInputRegister: function(addr, unitID) { | ||
// Synchronous handling | ||
return addr; | ||
}, | ||
getHoldingRegister: function(addr, unitID, callback) { | ||
// Asynchronous handling (with callback) | ||
setTimeout(function() { | ||
// callback = function(err, value) | ||
callback(null, addr + 8000); | ||
}, 10); | ||
}, | ||
getCoil: function(addr, unitID) { | ||
// Asynchronous handling (with Promises, async/await supported) | ||
return new Promise(function(resolve) { | ||
setTimeout(function() { | ||
resolve(addr % 2 === 0); | ||
}, 10); | ||
}); | ||
}, | ||
setRegister: function(addr, value, unitID) { | ||
// Asynchronous handling supported also here | ||
console.log("set register", addr, value, unitID); | ||
return; | ||
}, | ||
setCoil: function(addr, value, unitID) { | ||
// Asynchronous handling supported also here | ||
console.log("set coil", addr, value, unitID); | ||
return; | ||
} | ||
}; | ||
|
||
// set the server to answer for modbus requests | ||
console.log("ModbusTCP listening on modbus://0.0.0.0:8502"); | ||
var serverTCP = new ModbusRTU.ServerTCP(vector, { | ||
host: "0.0.0.0", | ||
port: 8502, | ||
debug: true, | ||
unitID: 1 | ||
}); | ||
|
||
serverTCP.on("socketError", function(err) { | ||
// Handle socket error if needed, can be ignored | ||
console.error(err); | ||
}); |
Empty file.
Empty file.