Permalink
Browse files

Added a new server example that shows the use of Creationix's "Stack"…

… library as the handler.
  • Loading branch information...
1 parent e5f4886 commit be2c6ea24090aef677a4cd571083023d2c480f2d @TooTallNate committed Jan 5, 2011
Showing with 37 additions and 0 deletions.
  1. +37 −0 examples/stackServer.js
View
@@ -0,0 +1,37 @@
+require('modbus-stack');
+require('modbus-stack/server').createServer(
+ require('stack')(
+ // Handle "Read Coils" (well, we just call `next()`...)
+ functionCode(1, function(req, res, next) {
+ console.log("Got request for '1', but passing it on...");
+ next();
+ }),
+ // Handle "Read Input Registers"
+ functionCode(4, function(req, res, next) {
+ var resp = new Array(req.quantity);
+ for (var i=0; i < req.quantity; i++) {
+ resp[i] = req.startAddress + i;
+ }
+ res.writeResponse(resp);
+ }),
+ // If all else fails, call our "Illegal Function" response.
+ illegalFunction
+ )
+).listen(502);
+
+
+function functionCode(fc, callback) {
+ return function(req, res, next) {
+ if (req.functionCode === fc) {
+ callback.apply(this, arguments);
+ } else {
+ next();
+ }
+ }
+}
+
+function illegalFunction(request, response, err) {
+ var errCode = err && err.errno ? err.errno : 1;
+ console.log("responding with exception: " + errCode);
+ response.writeException(errCode);
+}

0 comments on commit be2c6ea

Please sign in to comment.