diff --git a/client.js b/client.js index be63b56..0ee999a 100644 --- a/client.js +++ b/client.js @@ -50,7 +50,7 @@ Client.REQUESTS = { 3: putTwoWord16be, // READ_INPUT_REGISTERS 4: putTwoWord16be, - // WRITE_SINGLE_COIL + // WRITE_SINGLE_REGISTER: 5: function(address, value) { if (typeof value !== 'boolean') throw new Error('"Write Single Coil" expects a \'boolean\' value'); return putTwoWord16be(address, value ? 0xff00 : 0x0000); @@ -69,17 +69,5 @@ Client.RESPONSES = { rtn.push(binary.end().vars.val); } return rtn; - }, - // WRITE_SINGLE_COIL - 5: function(bufferlist) { - var rtn = []; - var binary = Binary(bufferlist) - .getWord8('byteLength').end(); - rtn.byteLength = binary.vars.byteLength; - for (var i=0, l=binary.vars.byteLength/2; i= 1 && this.bufferlist.length >= (this.requestHeader.length-2)) { // We have the complete request. if (!server.REQUESTS[this.functionCode]) { - return this.emit('error', new Error('"REQUESTS['+this.functionCode+']" in "server.js" is not implemented!')); + //this._gotRequest = true; + //this.writeException(1); + //return this.emit('error', new Error('"REQUESTS['+this.functionCode+']" in "server.js" is not implemented!')); + + console.log(JSON.stringify(this.requestHeader)); + this.request = this.requestHeader; + this._gotRequest = true; + return this.emit('not_implemented', this.request); } try { this.request = server.REQUESTS[this.functionCode].call(this, this.bufferlist); @@ -206,6 +213,7 @@ ModbusResponseStack.prototype._onData = function(chunk) { //console.log('bufferlist.length: ' + this.bufferlist.length); this._gotRequest = true; this.emit('request', this.request); + //console.log("modbus-stack.js" +JSON.stringify(this.request)); } } diff --git a/package.json b/package.json index 7cc7646..499b67d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,10 @@ "name": "modbus-stack", "description": "A `StreamStack` implementation of the MODBUS protocol.", "version": "0.2.1", - "author": "Nathan Rajlich ", + "author": { + "name": "Nathan Rajlich", + "email": "nathan@tootallnate.net" + }, "keywords": [ "MODBUS", "protocol", @@ -19,13 +22,20 @@ }, "main": "./modbus-stack", "scripts": { - "test": "expresso test/test*.js" + "test": "expresso test/test*.js", + "start": "node server.js" }, "engines": { "node": ">= 0.3.0" }, "directories": {}, - "files": [ - "" - ] -} \ No newline at end of file + "files": [], + "readme": "node-modbus-stack\n=================\n### A [StreamStack][] implementation of the [MODBUS][Modbus] protocol for [Node][].\n\nThis module exposes two concrete `StreamStack` implementations:\n`ModbusRequestStack` can be used as a MODBUS client (i.e. Master), and can write\nMODBUS compliant requests and listen for the response.\n`ModbusResponseStack` can be used to create a MODBUS server (i.e. Slave), by\nlistening for requests and providing a convenient API to respond with.\n\n[MODBUS][ModbusWiki] is an open building automation protocol that is widely used\nin various monitoring and controlling equipment. It's used with a variety of\ndifferent transports, including TCP.\n\nCurrently only communication through the TCP protocol is supported, however\nRS-485 serial support should be possible with [node-serialport]. I haven't\nhad a chance to look into it yet.\n\n\nA MODBUS Master (Client)\n------------------------\n\nYou will need to know which _Function Code_ (defined in the MODBUS specification)\nyou are invoking on the remote MODBUS slave. In this example, we'll request to\nread from the current values of the first 50 __Input Registers__ on the slave:\n\n // 'RIR' contains the \"Function Code\" that we are going to invoke on the remote device\n var RIR = require('modbus-stack').FUNCTION_CODES.READ_INPUT_REGISTERS;\n \n // IP and port of the MODBUS slave, default port is 502\n var client = require('modbus-stack/client').createClient(502, '10.0.1.50');\n \n // 'req' is an instance of the low-level `ModbusRequestStack` class\n var req = client.request(RIR, // Function Code: 4\n 0, // Start at address 0\n 50); // Read 50 contiguous registers from 0\n \n // 'response' is emitted after the entire contents of the response has been received.\n req.on('response', function(registers) {\n // An Array of length 50 filled with Numbers of the current registers.\n console.log(registers);\n client.end();\n });\n\n\nA MODBUS Slave (Server)\n-----------------------\n\n`node-modbus-stack` makes it dead simple to create a compliant MODBUS Slave (or Server)\nwritten in pure JavaScript. Here's an example of a server that would respond to the\nrequest above:\n\n var FC = require('modbus-stack').FUNCTION_CODES;\n \n // 'handlers' is an Object with keys containing the \"Function Codes\" that your MODBUS\n // server will handle. Anything function code requested without a handler defined here\n // will have the Server transparently respond with Exception Code 1 (\"Illegal Function\")\n var handlers = {};\n \n // Define a handler for \"Read Input Registers\". We'll just respond with the register\n // number requested. In a real-world situation, you'd probably look up these values from\n // a database, etc.\n handlers[FC.READ_INPUT_REGISTERS] = function(request, response) {\n var start = request.startAddress;\n var length = request.quantity;\n \n var resp = new Array(length);\n for (var i=0; i