From d2c6bae0f6464c3d047b56a2b5ff56120e466dfb Mon Sep 17 00:00:00 2001 From: "stefan.poeter@cloud-automation.de" Date: Sat, 26 Nov 2016 13:43:33 +0100 Subject: [PATCH] Revert changes to 1.2.5 and retagged this as 1.2.6 so that no errors occure in 1.2.6. --- .gitignore | 1 - package.json | 5 +- src/handler/client/ReadCoils.js | 17 +- src/handler/client/ReadDiscreteInputs.js | 21 +-- src/handler/client/ReadHoldingRegisters.js | 12 +- src/handler/client/ReadInputRegisters.js | 12 +- src/handler/client/WriteMultipleCoils.js | 38 ++--- src/handler/client/WriteMultipleRegisters.js | 33 ++-- src/handler/client/WriteSingleCoil.js | 11 +- src/handler/client/WriteSingleRegister.js | 18 +- src/handler/server/ReadCoils.js | 42 ++--- src/handler/server/ReadDiscreteInputs.js | 36 ++-- src/handler/server/ReadHoldingRegisters.js | 38 ++--- src/handler/server/ReadInputRegisters.js | 33 ++-- src/handler/server/WriteMultipleCoils.js | 33 ++-- src/handler/server/WriteMultipleRegisters.js | 41 +++-- src/handler/server/WriteSingleCoil.js | 42 ++--- src/handler/server/WriteSingleRegister.js | 28 ++-- src/modbus-client-core.js | 5 +- src/modbus-serial-client.js | 52 +++--- src/modbus-server-core.js | 7 +- src/modbus-tcp-client.js | 16 +- src/modbus-tcp-server.js | 16 +- src/modbus.js | 2 +- test/modbus-client-core.test.js | 165 +++++++++++++------ test/modbus-server-core.test.js | 127 +++++++------- 26 files changed, 430 insertions(+), 421 deletions(-) diff --git a/.gitignore b/.gitignore index 6632bcd..1bd7226 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ node_modules *.swp -*.idea diff --git a/package.json b/package.json index dbc97ef..71e49f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jsmodbus", - "version": "1.2.6", + "version": "1.2.5", "description": "Implementation for the Serial/TCP Modbus protocol.", "author": "Stefan Poeter ", "main": "./src/modbus.js", @@ -9,7 +9,8 @@ "url": "https://github.com/Cloud-Automation/node-modbus" }, "dependencies": { - "bluebird": "^3.4.6", + "put": "0.0.6", + "q": "1.0.1", "crc": "3.4.0", "serialport": "^4.0.1", "stampit": "^2.1.2", diff --git a/src/handler/client/ReadCoils.js b/src/handler/client/ReadCoils.js index 5da206b..361a5bb 100644 --- a/src/handler/client/ReadCoils.js +++ b/src/handler/client/ReadCoils.js @@ -1,5 +1,7 @@ var Stampit = require('stampit'), - Promise = require('bluebird') + Q = require('q'), + Put = require('put'); + module.exports = Stampit() .init(function () { @@ -12,10 +14,11 @@ module.exports = Stampit() var onResponse = function (pdu, request) { - this.log.debug("handling read coils response."); + this.log.debug("handeling read coils response."); var fc = pdu.readUInt8(0), - byteCount = pdu.readUInt8(1); + byteCount = pdu.readUInt8(1), + bitCount = byteCount * 8; var resp = { fc : fc, @@ -47,12 +50,8 @@ module.exports = Stampit() this.readCoils = function (start, quantity) { var fc = 1, - defer = Promise.defer(), - pdu = Buffer.allocUnsafe(5) - - pdu.writeUInt8(fc,0) - pdu.writeUInt16BE(start,1) - pdu.writeUInt16BE(quantity,3) + defer = Q.defer(), + pdu = Put().word8(fc).word16be(start).word16be(quantity).buffer(); this.queueRequest(fc, pdu, defer); diff --git a/src/handler/client/ReadDiscreteInputs.js b/src/handler/client/ReadDiscreteInputs.js index a9dff37..850eda2 100644 --- a/src/handler/client/ReadDiscreteInputs.js +++ b/src/handler/client/ReadDiscreteInputs.js @@ -1,5 +1,7 @@ var Stampit = require('stampit'), - Promise = require('bluebird') + Q = require('q'), + Put = require('put'); + module.exports = Stampit() .init(function () { @@ -12,7 +14,7 @@ module.exports = Stampit() var onResponse = function (pdu, request) { - this.log.debug("handling read discrete inputs response."); + this.log.debug("handeling read discrete inputs response."); var fc = pdu.readUInt8(0), byteCount = pdu.readUInt8(1), @@ -29,9 +31,9 @@ module.exports = Stampit() return; } - for (var i = 0; i < byteCount; i += 1) { + for (var i = 0; i < byteCount; i+=1) { var h = 1, cur = pdu.readUInt8(2 + i); - for (var j = 0; j < 8; j += 1) { + for (var j = 0; j < 8; j+=1) { resp.coils[cntr] = (cur & h) > 0 ; h = h << 1; cntr += 1; @@ -45,17 +47,16 @@ module.exports = Stampit() this.readDiscreteInputs = function (start, quantity) { var fc = 2, - defer = Promise.defer(), - pdu = Buffer.allocUnsafe(5); - - pdu.writeUInt8(fc); - pdu.writeUInt16BE(start, 1); - pdu.writeUInt16BE(quantity, 3); + defer = Q.defer(), + pdu = Put().word8be(2).word16be(start).word16be(quantity).buffer(); if (quantity > 2000) { defer.reject(); + return defer.promise; + + } this.queueRequest(fc, pdu, defer); diff --git a/src/handler/client/ReadHoldingRegisters.js b/src/handler/client/ReadHoldingRegisters.js index 4a41314..e233988 100644 --- a/src/handler/client/ReadHoldingRegisters.js +++ b/src/handler/client/ReadHoldingRegisters.js @@ -1,5 +1,7 @@ var Stampit = require('stampit'), - Promise = require('bluebird') + Q = require('q'), + Put = require('put'); + module.exports = Stampit() .init(function () { @@ -45,12 +47,8 @@ module.exports = Stampit() this.log.debug('Starting read holding registers request.'); var fc = 3, - defer = Promise.defer(), - pdu = Buffer.allocUnsafe(5) - - pdu.writeUInt8(fc) - pdu.writeUInt16BE(start,1) - pdu.writeUInt16BE(quantity,3) + defer = Q.defer(), + pdu = Put().word8be(3).word16be(start).word16be(quantity).buffer(); this.queueRequest(fc, pdu, defer); diff --git a/src/handler/client/ReadInputRegisters.js b/src/handler/client/ReadInputRegisters.js index 7dd28cd..3fff98e 100644 --- a/src/handler/client/ReadInputRegisters.js +++ b/src/handler/client/ReadInputRegisters.js @@ -1,5 +1,7 @@ var Stampit = require('stampit'), - Promise = require('bluebird') + Q = require('q'), + Put = require('put'); + module.exports = Stampit() .init(function () { @@ -43,12 +45,8 @@ module.exports = Stampit() this.readInputRegisters = function (start, quantity) { var fc = 4, - defer = Promise.defer(), - pdu = Buffer.allocUnsafe(5) - - pdu.writeUInt8(fc) - pdu.writeUInt16BE(start,1) - pdu.writeUInt16BE(quantity,3) + defer = Q.defer(), + pdu = Put().word8be(4).word16be(start).word16be(quantity).buffer(); this.queueRequest(fc, pdu, defer); diff --git a/src/handler/client/WriteMultipleCoils.js b/src/handler/client/WriteMultipleCoils.js index f54dfe0..27ad40c 100644 --- a/src/handler/client/WriteMultipleCoils.js +++ b/src/handler/client/WriteMultipleCoils.js @@ -1,5 +1,7 @@ var stampit = require('stampit'), - Promise = require('bluebird') + Q = require('q'), + Put = require('put'); + module.exports = stampit() .init(function () { @@ -36,20 +38,17 @@ module.exports = stampit() this.writeMultipleCoils = function (startAddress, coils, N) { - var defer = Promise.defer(); + var defer = Q.defer(); var fc = 15, - basePdu = Buffer.allocUnsafe(6) - var pdu - - basePdu.writeUInt8(fc,0) - basePdu.writeUInt16BE(startAddress,1) + pdu = Put() + .word8(fc) + .word16be(startAddress); if (coils instanceof Buffer) { - basePdu.writeUInt16BE(N, 3) - basePdu.writeUInt8(coils.length, 5) - pdu = Buffer.concat([basePdu, coils]) - + pdu.word16be(N) + .word8(coils.length) + .put(coils); } else if (coils instanceof Array) { if (coils.length > 1968) { @@ -59,12 +58,10 @@ module.exports = stampit() var byteCount = Math.ceil(coils.length / 8), curByte = 0, - curByteIdx = 0, - cntr = 0 - var payloadPdu = Buffer.allocUnsafe(byteCount) + cntr = 0; - basePdu.writeUInt16BE(coils.length, 3) - basePdu.writeUInt8(byteCount, 5) + pdu.word16be(coils.length) + .word8(byteCount); for (var i = 0; i < coils.length; i += 1) { @@ -73,15 +70,14 @@ module.exports = stampit() cntr = (cntr + 1) % 8; if (cntr === 0 || i === coils.length - 1 ) { - payloadPdu.writeUInt8(curByte, curByteIdx) - curByteIdx = curByteIdx + 1 - curByte = 0 + pdu.word8(curByte); + curByte = 0; } } - - pdu = Buffer.concat([basePdu, payloadPdu]) } + pdu = pdu.buffer(); + this.queueRequest(fc, pdu, defer); return defer.promise; diff --git a/src/handler/client/WriteMultipleRegisters.js b/src/handler/client/WriteMultipleRegisters.js index edbccf8..44dba46 100644 --- a/src/handler/client/WriteMultipleRegisters.js +++ b/src/handler/client/WriteMultipleRegisters.js @@ -1,5 +1,7 @@ var stampit = require('stampit'), - Promise = require('bluebird') + Q = require('q'), + Put = require('put'); + module.exports = stampit() .init(function () { @@ -36,13 +38,11 @@ module.exports = stampit() this.writeMultipleRegisters = function (startAddress, register) { - var defer = Promise.defer(); + var defer = Q.defer(); var fc = 16, - basePdu = Buffer.allocUnsafe(6), - pdu - - basePdu.writeUInt8(fc) - basePdu.writeUInt16BE(startAddress, 1) + pdu = Put() + .word8(fc) + .word16be(startAddress) if(register instanceof Buffer) { @@ -50,10 +50,9 @@ module.exports = stampit() defer.reject(); } - basePdu.writeUInt16BE(register.length/2,3) - basePdu.writeUInt8(register.length,5) - - pdu = Buffer.concat([basePdu, register]) + pdu.word16be(register.length/2) + .word8(register.length) + .put(register) } else if(register instanceof Array) { @@ -65,20 +64,18 @@ module.exports = stampit() var byteCount = Math.ceil(register.length * 2), curByte = 0 - var payloadPdu = Buffer.allocUnsafe(byteCount) - - basePdu.writeUInt16BE(register.length, 3) - basePdu.writeUInt8(byteCount, 5) + pdu.word16be(register.length) + .word8(byteCount) for (var i = 0; i < register.length; i += 1) { - payloadPdu.writeUInt16BE(register[i],2*i) + pdu.word16be(register[i]); } - - pdu = Buffer.concat([basePdu, payloadPdu]) } else { defer.reject(); } + pdu = pdu.buffer(); + this.queueRequest(fc, pdu, defer); return defer.promise; diff --git a/src/handler/client/WriteSingleCoil.js b/src/handler/client/WriteSingleCoil.js index bfb10e6..51b7ef9 100644 --- a/src/handler/client/WriteSingleCoil.js +++ b/src/handler/client/WriteSingleCoil.js @@ -1,5 +1,6 @@ var Stampit = require('stampit'), - Promise = require('bluebird') + Q = require('q'), + Put = require('put'); module.exports = Stampit() .init(function () { @@ -36,13 +37,9 @@ module.exports = Stampit() this.writeSingleCoil = function (address, value) { var fc = 5, - defer = Promise.defer(), + defer = Q.defer(), payload = (value instanceof Buffer) ? (value.readUInt8(0) > 0) : value, - pdu = Buffer.allocUnsafe(5) - - pdu.writeUInt8(fc,0) - pdu.writeUInt16BE(address,1) - pdu.writeUInt16BE(payload ? 0xff00:0x0000,3) + pdu = Put().word8be(5).word16be(address).word16be(payload?0xff00:0x0000).buffer(); this.queueRequest(fc, pdu, defer); diff --git a/src/handler/client/WriteSingleRegister.js b/src/handler/client/WriteSingleRegister.js index 214dd77..e701dc1 100644 --- a/src/handler/client/WriteSingleRegister.js +++ b/src/handler/client/WriteSingleRegister.js @@ -1,5 +1,7 @@ var Stampit = require('stampit'), - Promise = require('bluebird') + Q = require('q'), + Put = require('put'); + module.exports = Stampit() .init(function () { @@ -22,8 +24,8 @@ module.exports = Stampit() fc : fc, registerAddress : registerAddress, registerValue : registerValue, - registerAddressRaw: pdu.slice(1,3), - registerValueRaw: pdu.slice(3,5) + registerAddressRaw: pdu.slice(1,2), + registerValueRaw: pdu.slice(3,2) }; if (fc !== 6) { @@ -38,13 +40,9 @@ module.exports = Stampit() this.writeSingleRegister = function (address, value) { var fc = 6, - defer = Promise.defer(), - payload = (value instanceof Buffer) ? value.readUInt16BE(0) : value, - pdu = Buffer.allocUnsafe(5) - - pdu.writeUInt8(fc, 0) - pdu.writeUInt16BE(address, 1) - pdu.writeUInt16BE(payload, 3) + defer = Q.defer(), + payload = (value instanceof Buffer) ? value : Put().word16be(value).buffer(), + pdu = Put().word8be(6).word16be(address).put(payload).buffer(); this.queueRequest(fc, pdu, defer); diff --git a/src/handler/server/ReadCoils.js b/src/handler/server/ReadCoils.js index e3d4c75..1c813da 100644 --- a/src/handler/server/ReadCoils.js +++ b/src/handler/server/ReadCoils.js @@ -1,4 +1,6 @@ -var stampit = require('stampit') +var stampit = require('stampit'), + Put = require('put'); + module.exports = stampit() .init(function () { @@ -24,15 +26,12 @@ module.exports = stampit() if (pdu.length !== 5) { - var buf = Buffer.allocUnsafe(2) + cb(Put().word8(0x81).word8(0x02).buffer()); + return; - buf.writeUInt8(0x81, 0); - buf.writeUInt8(0x02, 1); - cb(buf); - return; } - var //fc = pdu.readUInt8(0), // unused + var fc = pdu.readUInt8(0), start = pdu.readUInt16BE(1), quantity = pdu.readUInt16BE(3); @@ -42,41 +41,34 @@ module.exports = stampit() if (start > mem.length * 8 || start + quantity > mem.length * 8) { - var buf = Buffer.allocUnsafe(2); - buf.writeUInt8(0x81, 0); - buf.writeUInt8(0x02, 1); - cb(buf); - return; + cb(Put().word8(0x81).word8(0x02).buffer()); + return; + } var val = 0, thisByteBitCount = 0, - byteIdx = 2, - byteCount = Math.ceil(quantity / 8), - response = Buffer.allocUnsafe(2 + byteCount) - - response.writeUInt8(0x01, 0); - response.writeUInt8(byteCount, 1); + response = Put().word8(0x01).word8(Math.floor(quantity / 8) + (quantity % 8 === 0 ? 0 : 1)); for (var totalBitCount = start; totalBitCount < start + quantity; totalBitCount += 1) { - var buf = mem.readUInt8(Math.floor(totalBitCount / 8)); - var mask = 1 << (totalBitCount % 8); + var buf = mem.readUInt8(Math.floor(totalBitCount / 8)) + var mask = 1 << (totalBitCount % 8) - if (buf & mask) { - val += 1 << (thisByteBitCount % 8); + if(buf & mask) { + val += 1 << (thisByteBitCount % 8) } thisByteBitCount += 1; if (thisByteBitCount % 8 === 0 || totalBitCount === (start + quantity) - 1) { - response.writeUInt8(val, byteIdx); - val = 0; byteIdx = byteIdx + 1; + response.word8(val); + val = 0; } } - cb(response); + cb(response.buffer()); }.bind(this), this.responseDelay); diff --git a/src/handler/server/ReadDiscreteInputs.js b/src/handler/server/ReadDiscreteInputs.js index d66ad86..21c6b77 100644 --- a/src/handler/server/ReadDiscreteInputs.js +++ b/src/handler/server/ReadDiscreteInputs.js @@ -1,4 +1,5 @@ -var stampit = require('stampit') +var stampit = require('stampit'), + Put = require('put'); var handler = stampit() .init(function () { @@ -25,15 +26,9 @@ var handler = stampit() if (pdu.length !== 5) { - this.log.debug('wrong pdu length.'); + cb(Put().word8(0x82).word8(0x02).buffer()); + return; - var buf = Buffer.allocUnsafe(2) - - buf.writeUInt8(0x82, 0) - buf.writeUInt8(0x02, 1) - cb(buf) - - return; } var fc = pdu.readUInt8(0), @@ -46,25 +41,14 @@ var handler = stampit() if (start > mem.length * 8 || start + quantity > mem.length * 8) { - this.log.debug('wrong pdu length.'); - - var buf = Buffer.allocUnsafe(2) + cb(Put().word8(0x82).word8(0x02).buffer()); + return; - buf.writeUInt8(0x82, 0) - buf.writeUInt8(0x02, 1) - cb(buf) - - return } var val = 0, thisByteBitCount = 0, - byteIdx = 2, - byteCount = Math.ceil(quantity / 8), - response = Buffer.allocUnsafe(2 + byteCount) - - response.writeUInt8(0x02, 0) - response.writeUInt8(byteCount, 1); + response = Put().word8(0x02).word8(Math.floor(quantity / 8) + (quantity % 8 === 0 ? 0 : 1)); for (var totalBitCount = start; totalBitCount < start + quantity; totalBitCount += 1) { @@ -79,12 +63,12 @@ var handler = stampit() if (thisByteBitCount % 8 === 0 || totalBitCount === (start + quantity) - 1) { - response.writeUInt8(val, byteIdx) - val = 0; byteIdx = byteIdx + 1 + response.word8(val); + val = 0; } } - cb(response); + cb(response.buffer()); }.bind(this), this.responseDelay); diff --git a/src/handler/server/ReadHoldingRegisters.js b/src/handler/server/ReadHoldingRegisters.js index c784302..5595135 100644 --- a/src/handler/server/ReadHoldingRegisters.js +++ b/src/handler/server/ReadHoldingRegisters.js @@ -1,4 +1,6 @@ -var stampit = require('stampit') +var stampit = require('stampit'), + Put = require('put'); + module.exports = stampit() .init(function () { @@ -23,18 +25,14 @@ module.exports = stampit() if (pdu.length !== 5) { - this.log.debug('wrong pdu length.'); - - var buf = Buffer.allocUnsafe(2); + this.log.debug('wrong pdu length.'); - buf.writeUInt8(0x83, 0); - buf.writeUInt8(0x02, 1); - cb(buf); + cb(Put().word8(0x83).word8(0x02).buffer()); + return; - return; } - var //fc = pdu.readUInt8(0), //unused + var fc = pdu.readUInt8(0), start = pdu.readUInt16BE(1), byteStart = start * 2, quantity = pdu.readUInt16BE(3); @@ -45,25 +43,23 @@ module.exports = stampit() if (byteStart > mem.length || byteStart + (quantity * 2) > mem.length) { - this.log.debug('request outside register boundaries.'); - var buf = Buffer.allocUnsafe(2); + this.log.debug('request outside register boundaries.'); + cb(Put().word8(0x83).word8(0x02).buffer()); + return; - buf.writeUInt8(0x83, 0); - buf.writeUInt8(0x02, 1); - cb(buf); - return; } - var head = Buffer.allocUnsafe(2); - - head.writeUInt8(0x03, 0); - head.writeUInt8(quantity * 2, 1); + var response = Put().word8(0x03).word8(quantity * 2); - var response = Buffer.concat([head, mem.slice(byteStart, byteStart + quantity * 2)]); + for (var i = byteStart; i < byteStart + (quantity * 2); i += 2) { + + response.word16be(mem.readUInt16BE(i)); + + } this.log.debug('finished read holding register request.'); - cb(response); + cb(response.buffer()); }.bind(this), this.responseDelay); diff --git a/src/handler/server/ReadInputRegisters.js b/src/handler/server/ReadInputRegisters.js index bb1a58c..98c2eb0 100644 --- a/src/handler/server/ReadInputRegisters.js +++ b/src/handler/server/ReadInputRegisters.js @@ -1,4 +1,6 @@ -var stampit = require('stampit') +var stampit = require('stampit'), + Put = require('put'); + module.exports = stampit() .init(function () { @@ -23,15 +25,12 @@ module.exports = stampit() if (pdu.length !== 5) { - var buf = Buffer.allocUnsafe(2); - - buf.writeUInt8(0x84, 0); - buf.writeUInt8(0x02, 1); - cb(buf); + cb(Put().word8(0x84).word8(0x02).buffer()); return; + } - var //fc = pdu.readUInt8(0), //unused + var fc = pdu.readUInt8(0), start = pdu.readUInt16BE(1), byteStart = start * 2, quantity = pdu.readUInt16BE(3); @@ -42,22 +41,20 @@ module.exports = stampit() if (byteStart > mem.length || byteStart + (quantity * 2) > mem.length) { - var buf = Buffer.allocUnsafe(2); + cb(Put().word8(0x84).word8(0x02).buffer()); + return; - buf.writeUInt8(0x84, 0); - buf.writeUInt8(0x02, 1); - cb(buf); - return; } - var head = Buffer.allocUnsafe(2); - - head.writeUInt8(0x04, 0); - head.writeUInt8(quantity * 2, 1); + var response = Put().word8(0x04).word8(quantity * 2); - var response = Buffer.concat([head, mem.slice(byteStart, byteStart + quantity * 2)]); + for (var i = byteStart; i < byteStart + (quantity * 2); i += 2) { + + response.word16be(mem.readUInt16BE(i)); + + } - cb(response); + cb(response.buffer()); }.bind(this), this.responseDelay); diff --git a/src/handler/server/WriteMultipleCoils.js b/src/handler/server/WriteMultipleCoils.js index 53d435a..8865f66 100644 --- a/src/handler/server/WriteMultipleCoils.js +++ b/src/handler/server/WriteMultipleCoils.js @@ -1,4 +1,6 @@ -var stampit = require('stampit') +var stampit = require('stampit'), + Put = require('put'); + module.exports = stampit() .init(function () { @@ -23,15 +25,12 @@ module.exports = stampit() if (pdu.length < 3) { - var buf = Buffer.allocUnsafe(2); + cb(Put().word8(0x8F).word8(0x02).buffer()); + return; - buf.writeUInt8(0x8F, 0); - buf.writeUInt8(0x02, 1); - cb(buf); - return; } - var //fc = pdu.readUInt8(0), // unused + var fc = pdu.readUInt8(0), start = pdu.readUInt16BE(1), quantity = pdu.readUInt16BE(3), byteCount = pdu.readUInt8(5); @@ -43,23 +42,15 @@ module.exports = stampit() // error response if (start > mem.length * 8 || start + quantity > mem.length * 8) { - var buf = Buffer.allocUnsafe(2); + cb(Put().word8(0x8F).word8(0x02).buffer()); + return; - buf.writeUInt8(0x8F, 0); - buf.writeUInt8(0x02, 1); - cb(buf); - return; } - var response = Buffer.allocUnsafe(5); - - response.writeUInt8(0x0F, 0); - response.writeUInt16BE(start, 1); - response.writeUInt16BE(quantity, 3); + var response = Put().word8(0x0F).word16be(start).word16be(quantity).buffer(), + oldValue, newValue, current = pdu.readUInt8(6 + 0), j = 0; - var oldValue, newValue, current = pdu.readUInt8(6 + 0), j = 0; - - for (var i = start; i < start + quantity; i += 1) { + for (var i = start; i < start + quantity; i += 1 ) { // reading old value from the coils register oldValue = mem.readUInt8(Math.floor(i / 8)); @@ -81,7 +72,9 @@ module.exports = stampit() if (j % 8 === 0 && j < quantity) { current = pdu.readUInt8(6 + Math.floor(j / 8)); + } + } this.emit('postWriteMultipleCoilsRequest', start, quantity, byteCount); diff --git a/src/handler/server/WriteMultipleRegisters.js b/src/handler/server/WriteMultipleRegisters.js index c0723f0..33b7992 100644 --- a/src/handler/server/WriteMultipleRegisters.js +++ b/src/handler/server/WriteMultipleRegisters.js @@ -1,4 +1,6 @@ -var stampit = require('stampit'); +var stampit = require('stampit'), + Put = require('put'); + module.exports = stampit() .init(function () { @@ -23,15 +25,12 @@ module.exports = stampit() if (pdu.length < 3) { - var buf = Buffer.allocUnsafe(2); + cb(Put().word8(0x90).word8(0x02).buffer()); + return; - buf.writeUInt8(0x90, 0); - buf.writeUInt8(0x02, 1); - cb(buf); - return; } - var //fc = pdu.readUInt8(0), //unused + var fc = pdu.readUInt8(0), start = pdu.readUInt16BE(1), byteStart = start * 2, quantity = pdu.readUInt16BE(3), @@ -39,12 +38,9 @@ module.exports = stampit() if (quantity > 0x007b) { - var buf = Buffer.allocUnsafe(2); - - buf.writeUInt8(0x90, 0); - buf.writeUInt8(0x03, 1); - cb(buf); + cb(Put().word8(0x90).word8(0x03).buffer()); return; + } this.emit('preWriteMultipleRegistersRequest', byteStart, quantity, byteCount); @@ -53,20 +49,21 @@ module.exports = stampit() if (byteStart > mem.length || byteStart + (quantity * 2) > mem.length) { - var buf = Buffer.allocUnsafe(2); + cb(Put().word8(0x90).word8(0x02).buffer()); + return; - buf.writeUInt8(0x90, 0); - buf.writeUInt8(0x02, 1); - cb(buf); - return; } - var response = Buffer.allocUnsafe(5); - response.writeUInt8(0x10, 0); - response.writeUInt16BE(start, 1); - response.writeUInt16BE(quantity, 3); + var response = Put().word8(0x10).word16be(start).word16be(quantity).buffer(), + j = 0, currentByte; - pdu.copy(mem, byteStart, 6, 6 + byteCount); + for (var i = byteStart; i < byteStart + byteCount; i += 1) { + + mem.writeUInt8(pdu.readUInt8(6 + j + 0), i); + + j += 1; + + } this.emit('postWriteMultipleRegistersRequest', byteStart, quantity, byteCount); diff --git a/src/handler/server/WriteSingleCoil.js b/src/handler/server/WriteSingleCoil.js index e2f648e..7d71b19 100644 --- a/src/handler/server/WriteSingleCoil.js +++ b/src/handler/server/WriteSingleCoil.js @@ -1,4 +1,6 @@ -var stampit = require('stampit'); +var stampit = require('stampit'), + Put = require('put'); + module.exports = stampit() .init(function () { @@ -23,26 +25,20 @@ module.exports = stampit() if (pdu.length !== 5) { - var buf = Buffer.allocUnsafe(2); + cb(Put().word8(0x85).word8(0x02).buffer()); + return; - buf.writeUInt8(0x85, 0); - buf.writeUInt8(0x02, 1); - cb(buf); - return; } - var //fc = pdu.readUInt8(0), // unused + var fc = pdu.readUInt8(0), address = pdu.readUInt16BE(1), - value = (pdu.readUInt16BE(3) === 0x0000) ? false : true; + value = pdu.readUInt16BE(3) === 0x0000?false:true; if (pdu.readUInt16BE(3) !== 0x0000 && pdu.readUInt16BE(3) !== 0xFF00) { - var buf = Buffer.allocUnsafe(2); - - buf.writeUInt8(0x85, 0); - buf.writeUInt8(0x03, 1); - cb(buf); - return; + cb(Put().word8(0x85).word8(0x03).buffer()); + return; + } this.emit('preWriteSingleCoilRequest', address, value); @@ -51,21 +47,13 @@ module.exports = stampit() if (address > mem.length * 8) { - var buf = Buffer.allocUnsafe(2); - - buf.writeUInt8(0x85, 0); - buf.writeUInt8(0x02, 1); - cb(buf); + cb(Put().word8(0x85).word8(0x02).buffer()); return; - } - - var response = Buffer.allocUnsafe(5); - response.writeUInt8(5, 0); - response.writeUInt16BE(address, 1); - response.writeUInt16BE(value ? 0xFF00 : 0x0000, 3); + } - var oldValue = mem.readUInt8(Math.floor(address / 8)), + var response = Put().word8(0x05).word16be(address).word16be(value?0xFF00:0x0000), + oldValue = mem.readUInt8(Math.floor(address / 8)), newValue; if (value) { @@ -78,7 +66,7 @@ module.exports = stampit() this.emit('postWriteSingleCoilRequest', address, value); - cb(response); + cb(response.buffer()); }.bind(this), this.responseDelay); diff --git a/src/handler/server/WriteSingleRegister.js b/src/handler/server/WriteSingleRegister.js index 8807def..6b2290e 100644 --- a/src/handler/server/WriteSingleRegister.js +++ b/src/handler/server/WriteSingleRegister.js @@ -1,4 +1,6 @@ -var stampit = require('stampit'); +var stampit = require('stampit'), + Put = require('put'); + module.exports = stampit() .init(function () { @@ -22,16 +24,13 @@ module.exports = stampit() this.log.debug('handling write single register request.'); if (pdu.length !== 5) { - - var buf = Buffer.allocUnsafe(2); - - buf.writeUInt8(0x86, 0); - buf.writeUInt8(0x02, 1); - cb(buf); + + cb(Put().word8(0x86).word8(0x02).buffer()); return; + } - var //fc = pdu.readUInt8(0), // unused + var fc = pdu.readUInt8(0), address = pdu.readUInt16BE(1), byteAddress = address * 2, value = pdu.readUInt16BE(3); @@ -42,19 +41,12 @@ module.exports = stampit() if (byteAddress > mem.length) { - var buf = Buffer.allocUnsafe(2); - - buf.writeUInt8(0x86, 0); - buf.writeUInt8(0x02, 1); - cb(buf); + cb(Put().word8(0x86).word8(0x02).buffer()); return; - } - var response = Buffer.allocUnsafe(5); + } - response.writeUInt8(0x06); - response.writeUInt16BE(address, 1); - response.writeUInt16BE(value, 3); + var response = Put().word8(0x06).word16be(address).word16be(value).buffer(); mem.writeUInt16BE(value, byteAddress); diff --git a/src/modbus-client-core.js b/src/modbus-client-core.js index 00baa3d..0ee862b 100644 --- a/src/modbus-client-core.js +++ b/src/modbus-client-core.js @@ -1,4 +1,7 @@ -var stampit = require('stampit'), + +var Util = require('util'), + Put = require('put'), + stampit = require('stampit'), Log = require('stampit-log'), StateMachine = require('stampit-state-machine'); diff --git a/src/modbus-serial-client.js b/src/modbus-serial-client.js index c6c1572..1b14fff 100644 --- a/src/modbus-serial-client.js +++ b/src/modbus-serial-client.js @@ -1,5 +1,6 @@ var stampit = require('stampit'), crc = require('crc'), + Put = require('put'), ModbusCore = require('./modbus-client-core.js'); module.exports = stampit() @@ -84,39 +85,38 @@ module.exports = stampit() var onSend = function (pdu) { - var crc = 0; - - var base = Buffer.allocUnsafe(1) - base.writeUInt8(1) - var buf = Buffer.concat([base, pdu]) - - for (var i = 0; i < buf.length; i += 1) { - crc = (buf.readUInt8(i) + crc) % 0xFFFF; + var pkt = Put() + .word8(this.unitId) + .put(pdu), + buf = pkt.buffer(); + + crc16 = 0; + crc16= crc.crc16modbus(buf); + pkt = pkt.word16le(crc16).buffer(); + + + for (var j = 0; j < pkt.length; j += 1) { + console.log(pkt.readUInt8(j).toString(16)); } - - crc16 = crc.crc16modbus(buf); - - crcBuf = Buffer.allocUnsafe(2) - crcBuf.writeUInt16LE(crc16, 0) - - var pkt = Buffer.concat([buf, crcBuf]) - + serialport.write(pkt, function (err) { - if (err) { - this.emit('error', err); - return; - } + + if (err) { + this.emit('error', err); + return; + } + }.bind(this)); - + }.bind(this); this.close = function () { - - serialport.close(); - + + serialport.close(); + }; init(); - - + + }); diff --git a/src/modbus-server-core.js b/src/modbus-server-core.js index 47138a6..99205d0 100644 --- a/src/modbus-server-core.js +++ b/src/modbus-server-core.js @@ -1,4 +1,5 @@ var stampit = require('stampit'), + Put = require('put'), EventBus = require('stampit-event-bus'), Log = require('stampit-log'); @@ -47,11 +48,7 @@ var stampit = require('stampit'), this.log.debug('no handler for fc', fc); - var buf = Buffer.alloc(2) - buf.writeUInt8(fc + 0x80, 0) - buf.writeUInt8(0x01, 1) - - callback(buf) + callback(Put().word8(fc + 0x80).word8(0x01).buffer()); return; diff --git a/src/modbus-tcp-client.js b/src/modbus-tcp-client.js index b13b825..b71cdb9 100644 --- a/src/modbus-tcp-client.js +++ b/src/modbus-tcp-client.js @@ -1,4 +1,5 @@ var stampit = require('stampit'), + Put = require('put'), Net = require('net'), ModbusCore = require('./modbus-client-core.js'); @@ -146,14 +147,13 @@ module.exports = stampit() reqId += 1; - var head = Buffer.allocUnsafe(7) - - head.writeUInt16BE(reqId, 0) - head.writeUInt16BE(this.protocolVersion, 2) - head.writeUInt16BE(pdu.length + 1, 4) - head.writeUInt8(this.unitId, 6) - - var pkt = Buffer.concat([head, pdu]) + var pkt = Put() + .word16be(reqId) // transaction id + .word16be(this.protocolVersion) // protocol version + .word16be(pdu.length + 1) // pdu length + .word8(this.unitId) // unit id + .put(pdu) // the actual pdu + .buffer(); currentRequestId = reqId; diff --git a/src/modbus-tcp-server.js b/src/modbus-tcp-server.js index 86cd12c..f87cfc2 100644 --- a/src/modbus-tcp-server.js +++ b/src/modbus-tcp-server.js @@ -1,6 +1,7 @@ var stampit = require('stampit'), ModbusServerCore = require('./modbus-server-core.js'), StateMachine = require('stampit-state-machine'), + Put = require('put'), net = require('net'); module.exports = stampit() @@ -111,14 +112,13 @@ module.exports = stampit() this.log.debug('sending tcp data'); - var head = Buffer.allocUnsafe(7) - - head.writeUInt16BE(current.request.trans_id, 0) - head.writeUInt16BE(current.request.protocol_ver, 2) - head.writeUInt16BE(response.length + 1, 4) - head.writeUInt8(current.request.unit_id, 6) - - var pkt = Buffer.concat([head, response]) + var pkt = Put() + .word16be(current.request.trans_id) // transaction id + .word16be(current.request.protocol_ver) // protocol version + .word16be(response.length + 1) // pdu length + .word8(current.request.unit_id) // unit id + .put(response) // the actual pdu + .buffer(); current.socket.write(pkt); diff --git a/src/modbus.js b/src/modbus.js index 25be15b..b244338 100644 --- a/src/modbus.js +++ b/src/modbus.js @@ -31,7 +31,7 @@ fs.readdirSync(__dirname + '/handler/client') exports.server = { tcp : { core : require('./modbus-tcp-server.js'), - complete : require('./modbus-tcp-server.js') + complete : require('./modbus-tcp-server.js'), }, handler : { } }; diff --git a/test/modbus-client-core.test.js b/test/modbus-client-core.test.js index 3d887ed..ea807b7 100644 --- a/test/modbus-client-core.test.js +++ b/test/modbus-client-core.test.js @@ -1,5 +1,8 @@ + + var stampit = require('stampit'), - assert = require('assert'), + assert = require("assert"), + Put = require('put'), sinon = require('sinon'), util = require('util'), eventEmitter = require('events').EventEmitter; @@ -24,7 +27,7 @@ describe("Modbus Serial Client", function () { assert.equal(resp.fc, 1); assert.equal(resp.byteCount, 2); assert.equal(resp.coils.length, 16); - assert.deepEqual(resp.payload, Buffer.from([85, 1])) + assert.deepEqual(resp.payload, new Buffer([85, 1])) assert.deepEqual(resp.coils, [true, false, true, false, true, false, true, false, true, false, false, false, false, false, false, false]); done(); @@ -32,21 +35,23 @@ describe("Modbus Serial Client", function () { }).done(); client.setState('ready'); - client.emit('data', Buffer.from([1,2,85,1])); + client.emit('data', Put().word8(1).word8be(2).word8be(85).word8be(1).buffer() ); + + }); it("Should fail reading coils.", function (done) { var client = ModbusClient(); - client.readCoils(0, 10).catch(function (resp) { + client.readCoils(0, 10).fail(function (resp) { done(); }).done(); client.setState('ready'); - client.emit('data', Buffer.from([0x81, 0x01])) + client.emit('data', Put().word8be(0x81).word8be(1).buffer()); }); @@ -66,7 +71,7 @@ describe("Modbus Serial Client", function () { assert.equal(resp.fc, 2); assert.equal(resp.byteCount, 1); assert.equal(resp.coils.length, 8); - assert.deepEqual(resp.payload, Buffer.from([15])); + assert.deepEqual(resp.payload, new Buffer([15])); assert.deepEqual(resp.coils, [true, true, true, true, false, false, false, false]); done(); @@ -74,7 +79,7 @@ describe("Modbus Serial Client", function () { }).done(); client.setState('ready'); - client.emit('data', Buffer.from([0x02, 0x01, 0x0F])) + client.emit('data', Put().word8be(2).word8be(1).word8be(15).buffer()); }); @@ -86,14 +91,14 @@ describe("Modbus Serial Client", function () { assert.ok(false); - }).catch(function (err) { + }).fail(function (err) { done(); }).done(); client.setState('ready'); - client.emit('data', Buffer.from([0x82, 0x02])) + client.emit('data', Put().word8be(0x82).word8be(0x02).buffer()); }); @@ -113,7 +118,7 @@ describe("Modbus Serial Client", function () { assert.equal(resp.fc, 3); assert.equal(resp.byteCount, 10); - assert.deepEqual(resp.payload, Buffer.from([0,1,0,2,0,3,0,4,0,5])); + assert.deepEqual(resp.payload, new Buffer([0,1,0,2,0,3,0,4,0,5])); assert.deepEqual(resp.register, [1, 2, 3, 4, 5]); done(); @@ -123,7 +128,15 @@ describe("Modbus Serial Client", function () { client.setState('ready'); client.emit( 'data', - Buffer.from([0x03,0x0A,0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,0x00,0x05]) + Put() + .word8be(3) + .word8be(10) + .word16be(1) + .word16be(2) + .word16be(3) + .word16be(4) + .word16be(5) + .buffer() ); }); @@ -136,14 +149,14 @@ describe("Modbus Serial Client", function () { assert.ok(false); - }).catch(function (err) { + }).fail(function (err) { done(); }).done(); client.setState('ready'); - client.emit('data', Buffer.from([0x83,0x03])) + client.emit('data', Put().word8be(0x83).word8be(0x03).buffer()); }); @@ -163,7 +176,7 @@ describe("Modbus Serial Client", function () { assert.equal(resp.fc, 4); assert.equal(resp.byteCount, 10); - assert.deepEqual(resp.payload, Buffer.from([0,5,0,4,0,3,0,2,0,1])) + assert.deepEqual(resp.payload, new Buffer([0,5,0,4,0,3,0,2,0,1])) assert.deepEqual(resp.register, [5, 4, 3, 2, 1]); done(); @@ -173,7 +186,15 @@ describe("Modbus Serial Client", function () { client.setState('ready'); client.emit( 'data', - Buffer.from([0x04,0x0A,0x00,0x05,0x00,0x04,0x00,0x03,0x00,0x02,0x00,0x01]) + Put() + .word8be(4) + .word8be(10) + .word16be(5) + .word16be(4) + .word16be(3) + .word16be(2) + .word16be(1) + .buffer() ); }); @@ -186,14 +207,14 @@ describe("Modbus Serial Client", function () { assert.ok(false); - }).catch(function (err) { + }).fail(function (err) { done(); }).done(); client.setState('ready'); - client.emit('data', Buffer.from([0x84,0x03])) + client.emit('data', Put().word8be(0x84).word8be(0x03).buffer()); }); @@ -228,7 +249,11 @@ describe("Modbus Serial Client", function () { client.setState('ready'); client.emit( 'data', - Buffer.from([0x05,0x00,0x03,0xFF,0x00]) + Put() + .word8be(5) + .word16be(3) + .word16be(0xFF00) + .buffer() ); }); @@ -236,7 +261,7 @@ describe("Modbus Serial Client", function () { var client = ModbusClient(true); - client.writeSingleCoil(3, Buffer.from([1])).then(function (resp) { + client.writeSingleCoil(3, new Buffer([1])).then(function (resp) { assert.equal(resp.fc, 5); assert.equal(resp.outputAddress, 3); @@ -255,7 +280,11 @@ describe("Modbus Serial Client", function () { client.setState('ready'); client.emit( 'data', - Buffer.from([0x05,0x00,0x03,0xFF,0x00]) + Put() + .word8be(5) + .word16be(3) + .word16be(0xFF00) + .buffer() ); }); @@ -263,7 +292,7 @@ describe("Modbus Serial Client", function () { var client = ModbusClient(true); - client.writeSingleCoil(3, Buffer.from([0])).then(function (resp) { + client.writeSingleCoil(3, new Buffer([0])).then(function (resp) { assert.equal(resp.fc, 5); assert.equal(resp.outputAddress, 3); @@ -282,7 +311,11 @@ describe("Modbus Serial Client", function () { client.setState('ready'); client.emit( 'data', - Buffer.from([0x05,0x00,0x03,0x00,0x00]) + Put() + .word8be(5) + .word16be(3) + .word16be(0x0000) + .buffer() ); }); @@ -294,14 +327,15 @@ describe("Modbus Serial Client", function () { done(true); - }).catch(function (err) { + }).fail(function (err) { done(); }).done(); client.setState('ready'); - client.emit('data', Buffer.from([0x85,0x04])) + client.emit('data', Put().word8be(0x85).word8be(0x04).buffer()); + }); @@ -320,9 +354,7 @@ describe("Modbus Serial Client", function () { assert.equal(resp.fc, 6); assert.equal(resp.registerAddress, 3); - assert.deepEqual(resp.registerAddressRaw, Buffer.from([0x00,0x03])); assert.equal(resp.registerValue, 123); - assert.deepEqual(resp.registerValueRaw, Buffer.from([0x00,0x7b])); done(); @@ -337,7 +369,11 @@ describe("Modbus Serial Client", function () { client.setState('ready'); client.emit( 'data', - Buffer.from([0x06,0x00,0x03,0x00,0x7B]) + Put() + .word8be(6) + .word16be(3) + .word16be(123) + .buffer() ); }); @@ -346,13 +382,11 @@ describe("Modbus Serial Client", function () { var client = ModbusClient(true); - client.writeSingleRegister(3, Buffer.from([0x00, 0x7b])).then(function (resp) { + client.writeSingleRegister(3, new Buffer([0x00, 0x7b])).then(function (resp) { assert.equal(resp.fc, 6); assert.equal(resp.registerAddress, 3); assert.equal(resp.registerValue, 123); - assert.deepEqual(resp.registerAddressRaw, Buffer.from([0x00,0x03])); - assert.deepEqual(resp.registerValueRaw, Buffer.from([0x00,0x7b])); done(); @@ -367,7 +401,11 @@ describe("Modbus Serial Client", function () { client.setState('ready'); client.emit( 'data', - Buffer.from([0x06,0x00,0x03,0x00,0x7B]) + Put() + .word8be(6) + .word16be(3) + .word16be(123) + .buffer() ); }); @@ -379,14 +417,14 @@ describe("Modbus Serial Client", function () { done(true); - }).catch(function (err) { + }).fail(function (err) { done(); }).done(); client.setState('ready'); - client.emit('data', Buffer.from([0x86,0x01])) + client.emit('data', Put().word8be(0x86).word8be(0x01).buffer()); }); @@ -426,7 +464,14 @@ describe("Modbus Serial Client", function () { client.setState('ready'); client.emit( 'data', - Buffer.from([0x0F,0x00,0x14,0x00,0x0A,0x02,0xCD,0x01]) + Put() + .word8be(15) + .word16be(20) + .word16be(10) + .word8be(2) + .word8be(0xCD) + .word8be(0x01) + .buffer() ); }); @@ -434,7 +479,7 @@ describe("Modbus Serial Client", function () { var client = ModbusClient(true); - client.writeMultipleCoils(20, Buffer.from([0xCD, 0x01]), 10) + client.writeMultipleCoils(20, new Buffer([0xCD, 0x01]), 10) .then(function (resp) { assert.equal(resp.fc, 15); @@ -458,7 +503,14 @@ describe("Modbus Serial Client", function () { client.setState('ready'); client.emit( 'data', - Buffer.from([0x0F,0x00,0x14,0x00,0x0A,0x02,0xCD,0x01]) + Put() + .word8be(15) + .word16be(20) + .word16be(10) + .word8be(2) + .word8be(0xCD) + .word8be(0x01) + .buffer() ); }); @@ -471,14 +523,14 @@ describe("Modbus Serial Client", function () { done(true); - }).catch(function (err) { + }).fail(function (err) { done(); }).done(); client.setState('ready'); - client.emit('data', Buffer.from([0x8F, 0x02])) + client.emit('data', Put().word8be(0x8F).word8be(0x02).buffer()); }); @@ -517,7 +569,14 @@ describe("Modbus Serial Client", function () { client.setState('ready'); client.emit( 'data', - Buffer.from([0x10,0x00,0x03,0x00,0x03,0x00,0x01,0x00,0x02,0x01,0x6F]) + Put() + .word8be(16) + .word16be(3) + .word16be(3) + .word16be(1) + .word16be(2) + .word16be(350) + .buffer() ); }); @@ -525,7 +584,7 @@ describe("Modbus Serial Client", function () { var client = ModbusClient(true); - client.writeMultipleRegisters(3, Buffer.from([0x00, 0xc4])) + client.writeMultipleRegisters(3, new Buffer([0x00, 0xc4])) var pdu = client.queueSpy().pdu @@ -545,14 +604,14 @@ describe("Modbus Serial Client", function () { done(true); - }).catch(function (err) { + }).fail(function (err) { done(); }).done(); client.setState('ready'); - client.emit('data', Buffer.from([0x90, 0x02])) + client.emit('data', Put().word8be(0x90).word8be(0x02).buffer()); }); @@ -569,12 +628,14 @@ describe("Modbus Serial Client", function () { var client = ModbusClient({ 'timeout' : 200 }); client.readHoldingRegisters(3, 10).then(function (resp) { + + done(true); - }).catch(function (err) { + }).fail(function (err) { - assert.equal(err.err, 'timeout'); - done() - }); + done(err.err !== 'timeout'); + + }).done(); client.setState('ready'); @@ -588,7 +649,7 @@ describe("Modbus Serial Client", function () { done(true); - }).catch(function (err) { + }).fail(function (err) { assert.equal(err.err, 'timeout'); @@ -600,7 +661,15 @@ describe("Modbus Serial Client", function () { client.emit( 'data', - Buffer.from([0x03,0x0A,0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,0x00,0x05]) + Put() + .word8be(3) + .word8be(10) + .word16be(1) + .word16be(2) + .word16be(3) + .word16be(4) + .word16be(5) + .buffer() ); done(); diff --git a/test/modbus-server-core.test.js b/test/modbus-server-core.test.js index 883a271..b9d1112 100644 --- a/test/modbus-server-core.test.js +++ b/test/modbus-server-core.test.js @@ -1,5 +1,6 @@ var stampit = require('stampit'), - assert = require('assert'), + assert = require("assert"), + Put = require('put'), sinon = require('sinon'), util = require('util'), eventEmitter = require('events').EventEmitter; @@ -13,13 +14,15 @@ describe("Modbus Server Core Tests.", function () { it('should answer with 0x8x status code due to missing handler.', function (done) { var core = ModbusCore(), - request = Buffer.from([0x01,0x00,0x00,0x00,0x0A]), - exResponse = Buffer.from([0x81,0x01]) + request = Put().word8(1).word16be(0).word16be(10).buffer(), + exResponse = Put().word8(0x81).word8(0x01).buffer(); var resp = function (response) { assert.equal(response.compare(exResponse), 0); + done(); + }; core.onData(request, resp); @@ -36,8 +39,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read coils request just fine.', function (done) { var core = Core(), - request = Buffer.from([0x01,0x00,0x00,0x00,0x05]), - exResponse = Buffer.from([0x01,0x01,0x15]); + request = Put().word8(0x01).word16be(0).word16be(5).buffer(), + exResponse = Put().word8(0x01).word8(1).word8(0x15).buffer(); core.getCoils().writeUInt8(0x15, 0); @@ -56,8 +59,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read coils request with odd start address just fine.', function (done) { var core = Core(), - request = Buffer.from([0x01,0x00,0x02,0x00,0x05]), - exResponse = Buffer.from([0x01,0x01,0x05]); + request = Put().word8(0x01).word16be(2).word16be(5).buffer(), + exResponse = Put().word8(0x01).word8(1).word8(0x05).buffer(); core.getCoils().writeUInt8(0x15, 0); @@ -76,8 +79,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read coils request with a start address outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x01,0x20,0x01,0x00,10]), - exResponse = Buffer.from([0x81,0x02]); + request = Put().word8(0x01).word16be(1024 * 8 + 1).word16be(10).buffer(), + exResponse = Put().word8(0x81).word8be(0x02).buffer(); var resp = function (response) { @@ -94,8 +97,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read coils request with a quantity value outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x01,0x1f,0xf8,0x00,9]), - exResponse = Buffer.from([0x81,0x02]); + request = Put().word8(0x01).word16be(1023 * 8).word16be(9).buffer(), + exResponse = Put().word8(0x81).word8be(0x02).buffer(); var resp = function (response) { @@ -119,8 +122,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read discrete inputs request just fine.', function (done) { var core = Core(), - request = Buffer.from([0x02,0x00,0,0x00,5]), - exResponse = Buffer.from([0x02,1,0x15]); + request = Put().word8(0x02).word16be(0).word16be(5).buffer(), + exResponse = Put().word8(0x02).word8(1).word8(0x15).buffer(); core.getInput().writeUInt8(0x15, 0); @@ -139,8 +142,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read discrete inputs request with odd start address just fine.', function (done) { var core = Core(), - request = Buffer.from([0x02,0x00,2,0x00,5]), - exResponse = Buffer.from([0x02,1,0x05]); + request = Put().word8(0x02).word16be(2).word16be(5).buffer(), + exResponse = Put().word8(0x02).word8(1).word8(0x05).buffer(); core.getInput().writeUInt8(0x15, 0); @@ -159,8 +162,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read discrete inputs request with a start address outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x02,0x20,0x01,0x00,10]), - exResponse = Buffer.from([0x82,0x02]); + request = Put().word8(0x02).word16be(1024 * 8 + 1).word16be(10).buffer(), + exResponse = Put().word8(0x82).word8be(0x02).buffer(); var resp = function (response) { @@ -177,8 +180,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read discrete inputs request with a quantity value outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x02,0x1F,0xF8,0x00,9]), - exResponse = Buffer.from([0x82,0x02]); + request = Put().word8(0x02).word16be(1023 * 8).word16be(9).buffer(), + exResponse = Put().word8(0x82).word8be(0x02).buffer(); var resp = function (response) { @@ -202,8 +205,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read holding registers request just fine.', function (done) { var core = Core(), - request = Buffer.from([0x03,0x00,0x01,0x00,0x04]), - exResponse = Buffer.from([0x03,0x08,0x00,0x02,0x00,0x03,0x00,0x04,0x00,0x05]); + request = Put().word8(0x03).word16be(0).word16be(5).buffer(), + exResponse = Put().word8(0x03).word8(10).word16be(1).word16be(2).word16be(3).word16be(4).word16be(5).buffer(); core.getHolding().writeUInt16BE(0x01, 0); core.getHolding().writeUInt16BE(0x02, 2); core.getHolding().writeUInt16BE(0x03, 4); @@ -213,7 +216,9 @@ describe("Modbus Server Core Tests.", function () { var resp = function (response) { assert.equal(response.compare(exResponse), 0); + done(); + }; core.onData(request, resp); @@ -223,8 +228,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read holding registers request with a start address outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x03,0x04,0x05,0x00,10]), - exResponse = Buffer.from([0x83,0x02]); + request = Put().word8(0x03).word16be(1024 + 1).word16be(10).buffer(), + exResponse = Put().word8(0x83).word8be(0x02).buffer(); var resp = function (response) { @@ -241,8 +246,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read holding registers request with a quantity value outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x03,0x04,0x03,0x00,1]), - exResponse = Buffer.from([0x83,0x02]); + request = Put().word8(0x03).word16be(1023).word16be(1).buffer(), + exResponse = Put().word8(0x83).word8be(0x02).buffer(); var resp = function (response) { @@ -267,8 +272,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read input registers request just fine.', function (done) { var core = Core(), - request = Buffer.from([0x04,0x00,0x01,0x00,0x04]), - exResponse = Buffer.from([0x04,0x08,0x00,4,0x00,3,0x00,2,0x00,1]); + request = Put().word8(0x04).word16be(0).word16be(5).buffer(), + exResponse = Put().word8(0x04).word8(10).word16be(5).word16be(4).word16be(3).word16be(2).word16be(1).buffer(); core.getInput().writeUInt16BE(0x05, 0); core.getInput().writeUInt16BE(0x04, 2); core.getInput().writeUInt16BE(0x03, 4); @@ -290,8 +295,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read input registers request with a start address outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x04,0x04,0x05,0x00,10]), - exResponse = Buffer.from([0x84,0x02]); + request = Put().word8(0x04).word16be(1024 +1).word16be(10).buffer(), + exResponse = Put().word8(0x84).word8be(0x02).buffer(); var resp = function (response) { @@ -308,8 +313,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a read input registers request with a quantity value outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x04,0x04,0x03,0x00,1]), - exResponse = Buffer.from([0x84,0x02]); + request = Put().word8(0x04).word16be(1023).word16be(1).buffer(), + exResponse = Put().word8(0x84).word8be(0x02).buffer(); var resp = function (response) { @@ -332,8 +337,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write single coil request just fine.', function (done) { var core = Core(), - request = Buffer.from([0x05,0x00,8,0xff,0x00]), - exResponse = Buffer.from([0x05,0x00,8,0xff,0x00]); + request = Put().word8(0x05).word16be(8).word16be(0xff00).buffer(), + exResponse = Put().word8(0x05).word16be(8).word16be(0xff00).buffer(); core.getCoils().writeUInt8(0, 1); @@ -353,8 +358,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write single coil request with a start address outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x05,0x20,0x01,0x00,0xff00]), - exResponse = Buffer.from([0x85,0x02]); + request = Put().word8(0x05).word16be(1024 * 8 + 1).word16be(0xff00).buffer(), + exResponse = Put().word8(0x85).word8be(0x02).buffer(); var resp = function (response) { @@ -371,8 +376,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write single coil request with a another value than 0x0000 (false) and 0xff00 (true).', function (done) { var core = Core(), - request = Buffer.from([0x05,0x00,8,0xf3,0x00]), - exResponse = Buffer.from([0x85,0x03]); + request = Put().word8(0x05).word16be(8).word16be(0xf300).buffer(), + exResponse = Put().word8(0x85).word8be(0x03).buffer(); var resp = function (response) { @@ -398,8 +403,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write single register request just fine.', function (done) { var core = Core(), - request = Buffer.from([0x06,0x00,8,0x01,0x23]), - exResponse = Buffer.from([0x06,0x00,8,0x01,0x23]); + request = Put().word8(0x06).word16be(8).word16be(0x0123).buffer(), + exResponse = Put().word8(0x06).word16be(8).word16be(0x0123).buffer(); core.getHolding().writeUInt16BE(0x0123, 8); @@ -419,8 +424,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write single register request with a start address outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x06,0x04,0x05,0x01,0x23]), - exResponse = Buffer.from([0x86,0x02]); + request = Put().word8(0x06).word16be(1024 + 1).word16be(0x0123).buffer(), + exResponse = Put().word8(0x86).word8be(0x02).buffer(); var resp = function (response) { @@ -444,8 +449,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write multiple coils request just fine.', function (done) { var core = Core(), - request = Buffer.from([0x0F,0x00,12,0x00,4,1,0x0F]), - exResponse = Buffer.from([0x0F,0x00,12,0x00,4]); + request = Put().word8(0x0F).word16be(12).word16be(4).word8(1).word8(0x0F).buffer(), + exResponse = Put().word8(0x0F).word16be(12).word16be(4).buffer(); core.getCoils().writeUInt8(0x0F, 1); @@ -465,8 +470,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write multiple coils request with a start address outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x0F,0x20,0x01,0x00,0x04,0x01,0x0F]), - exResponse = Buffer.from([0x8F,0x02]); + request = Put().word8(0x0F).word16be(1024 * 8 + 1).word16be(4).word8(1).word8(0x0F).buffer(), + exResponse = Put().word8(0x8F).word8be(0x02).buffer(); var resp = function (response) { @@ -483,8 +488,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write multiple coils request with a start and quantity outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x0F,0x1F,0xFD,0x00,4,1,0x0F]), - exResponse = Buffer.from([0x8F,0x02]); + request = Put().word8(0x0F).word16be(1024 * 8 - 3).word16be(4).word8(1).word8(0x0F).buffer(), + exResponse = Put().word8(0x8F).word8be(0x02).buffer(); var resp = function (response) { @@ -509,8 +514,20 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write multiple registers request just fine.', function (done) { var core = Core(), - request = Buffer.from([0x10,0x00,0x05,0x00,0x03,0x06,0x00,0x01,0x00,0x02,0x00,0x03]), - exResponse = Buffer.from([0x10,0x00,0x05,0x00,0x03]) + request = Put() + .word8(0x10) + .word16be(5) + .word16be(3) + .word8(6) + .word16be(0x0001) + .word16be(0x0002) + .word16be(0x0003) + .buffer(), + exResponse = Put() + .word8(0x10) + .word16be(5) + .word16be(3) + .buffer(); core.getHolding().writeUInt16BE(0x0000, 10); core.getHolding().writeUInt16BE(0x0000, 12); @@ -534,8 +551,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write multiple registers request with a start address outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x10,0x04,0x05,0x00,0x02,0x04,0x00,0x01,0x00,0x02]), - exResponse = Buffer.from([0x90,0x02]); + request = Put().word8(0x10).word16be(1025).word16be(2).word8(4).word16be(0x01).word16be(0x02).buffer(), + exResponse = Put().word8(0x90).word8be(0x02).buffer(); var resp = function (response) { @@ -552,8 +569,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write multiple registers request with a start and quantity outside the address space.', function (done) { var core = Core(), - request = Buffer.from([0x10,0x04,0x02,0x00,0x02,0x04,0x00,0x01,0x00,0x02]), - exResponse = Buffer.from([0x90,0x02]); + request = Put().word8(0x10).word16be(1022).word16be(2).word8(4).word16be(0x01).word16be(0x02).buffer(), + exResponse = Put().word8(0x90).word8be(0x02).buffer(); var resp = function (response) { @@ -570,8 +587,8 @@ describe("Modbus Server Core Tests.", function () { it('should handle a write multiple registers request with a quantity greater 0x007b.', function (done) { var core = Core(), - request = Buffer.from([0x10,0x00,0x00,0x00,0x7c,0xf8,0x00,0x01,0x00,0x02]), - exResponse = Buffer.from([0x90,0x03]); + request = Put().word8(0x10).word16be(0).word16be(0x007c).word8(0xf8).word16be(0x01).word16be(0x02).buffer(), + exResponse = Put().word8(0x90).word8be(0x03).buffer(); var resp = function (response) {