diff --git a/index.js b/index.js index efea92e..2649ecd 100644 --- a/index.js +++ b/index.js @@ -135,7 +135,7 @@ function _readFC16(data, next) { * @private */ function _writeBufferToPort(buffer) { - var transaction = this._transactions[this._port.transactionsCounter]; + var transaction = this._transactions[this._port._transactionIdWrite]; this._port.write(buffer); if (transaction) { @@ -208,17 +208,17 @@ ModbusRTU.prototype.open = function(callback) { callback(error); /* init ports transaction id and counter */ - modbus._port._transactionId = 1; - modbus._port.transactionsCounter = 1; + modbus._port._transactionIdRead = 1; + modbus._port._transactionIdWrite = 1; /* On serial port success * register the modbus parser functions */ modbus._port.on("data", function(data) { // set locale helpers variables - var transaction = modbus._transactions[modbus._port._transactionId]; + var transaction = modbus._transactions[modbus._port._transactionIdRead]; - // the _transactionId can be missing, ignore wrong transaction it's + // the _transactionIdRead can be missing, ignore wrong transaction it's if (!transaction) { return; } @@ -381,7 +381,7 @@ ModbusRTU.prototype.writeFC2 = function(address, dataAddress, length, next, code code = code || 2; // set state variables - this._transactions[this._port.transactionsCounter] = { + this._transactions[this._port._transactionIdWrite] = { nextAddress: address, nextCode: code, nextLength: 3 + parseInt((length - 1) / 8 + 1) + 2, @@ -434,7 +434,7 @@ ModbusRTU.prototype.writeFC4 = function(address, dataAddress, length, next, code code = code || 4; // set state variables - this._transactions[this._port.transactionsCounter] = { + this._transactions[this._port._transactionIdWrite] = { nextAddress: address, nextCode: code, nextLength: 3 + 2 * length + 2, @@ -474,7 +474,7 @@ ModbusRTU.prototype.writeFC5 = function(address, dataAddress, state, next) { var code = 5; // set state variables - this._transactions[this._port.transactionsCounter] = { + this._transactions[this._port._transactionIdWrite] = { nextAddress: address, nextCode: code, nextLength: 8, @@ -519,7 +519,7 @@ ModbusRTU.prototype.writeFC6 = function(address, dataAddress, value, next) { var code = 6; // set state variables - this._transactions[this._port.transactionsCounter] = { + this._transactions[this._port._transactionIdWrite] = { nextAddress: address, nextCode: code, nextLength: 8, @@ -561,7 +561,7 @@ ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) { var i = 0; // set state variables - this._transactions[this._port.transactionsCounter] = { + this._transactions[this._port._transactionIdWrite] = { nextAddress: address, nextCode: code, nextLength: 8, @@ -616,7 +616,7 @@ ModbusRTU.prototype.writeFC16 = function(address, dataAddress, array, next) { var code = 16; // set state variables - this._transactions[this._port.transactionsCounter] = { + this._transactions[this._port._transactionIdWrite] = { nextAddress: address, nextCode: code, nextLength: 8, diff --git a/ports/tcpport.js b/ports/tcpport.js index 958c259..18b84fe 100644 --- a/ports/tcpport.js +++ b/ports/tcpport.js @@ -22,7 +22,7 @@ var TcpPort = function(ip, options) { this.ip = ip; this.openFlag = false; this.callback = null; - this.transactionsCounter = 1; + this._transactionIdWrite = 1; // options if (typeof(options) === "undefined") options = {}; @@ -53,7 +53,7 @@ var TcpPort = function(ip, options) { buffer.writeUInt16LE(crc, buffer.length - CRC_LENGTH); // update transaction id - modbus._transactionId = data.readUInt16BE(0); + modbus._transactionIdRead = data.readUInt16BE(0); modbusSerialDebug({ action: "receive tcp port", data: data, buffer: buffer }); modbusSerialDebug(JSON.stringify({ action: "receive tcp port strings", data: data, buffer: buffer })); @@ -118,13 +118,13 @@ TcpPort.prototype.write = function(data) { // remove crc and add mbap var buffer = new Buffer(data.length + MIN_MBAP_LENGTH - CRC_LENGTH); - buffer.writeUInt16BE(this.transactionsCounter, 0); + buffer.writeUInt16BE(this._transactionIdWrite, 0); buffer.writeUInt16BE(0, 2); buffer.writeUInt16BE(data.length - CRC_LENGTH, 4); data.copy(buffer, MIN_MBAP_LENGTH); // set next transaction id - this.transactionsCounter = (this.transactionsCounter + 1) % MAX_TRANSACTIONS; + this._transactionIdWrite = (this._transactionIdWrite + 1) % MAX_TRANSACTIONS; // send buffer to slave this._client.write(buffer); diff --git a/ports/tcprtubufferedport.js b/ports/tcprtubufferedport.js index 60d1ee6..878879f 100644 --- a/ports/tcprtubufferedport.js +++ b/ports/tcprtubufferedport.js @@ -28,6 +28,7 @@ var TcpRTUBufferedPort = function(ip, options) { this.ip = ip; this.openFlag = false; this.callback = null; + this._transactionIdWrite = 1; // options if (typeof(options) === "undefined") options = {}; @@ -128,7 +129,7 @@ TcpRTUBufferedPort.prototype._emitData = function(start, length) { this._buffer = this._buffer.slice(start + length); // update transaction id - this._transactionId = data.readUInt16BE(0); + this._transactionIdRead = data.readUInt16BE(0); if (data.length > 0) { var buffer = Buffer.alloc(data.length + CRC_LENGTH); @@ -211,16 +212,16 @@ TcpRTUBufferedPort.prototype.write = function(data) { break; } - // get next transaction id - var transactionsId = (this._transactionId + 1) % MAX_TRANSACTIONS; - // remove crc and add mbap var buffer = new Buffer(data.length + MIN_MBAP_LENGTH - CRC_LENGTH); - buffer.writeUInt16BE(transactionsId, 0); + buffer.writeUInt16BE(this._transactionIdWrite, 0); buffer.writeUInt16BE(0, 2); buffer.writeUInt16BE(data.length - CRC_LENGTH, 4); data.copy(buffer, MIN_MBAP_LENGTH); + // get next transaction id + this._transactionIdWrite = (this._transactionIdWrite + 1) % MAX_TRANSACTIONS; + // send buffer to slave this._client.write(buffer); diff --git a/test/ports/tcpportrtubuffered.test.js b/test/ports/tcpportrtubuffered.test.js index d9101c1..8ca65f7 100644 --- a/test/ports/tcpportrtubuffered.test.js +++ b/test/ports/tcpportrtubuffered.test.js @@ -172,7 +172,7 @@ describe("Modbus TCP RTU buffered port", function() { describe("#write", function() { it("should write a valid TCP RTU message to the port", function() { port.write(new Buffer("0103000500045408", "hex")); - expect(port._client._data.toString("hex")).to.equal("000400000006010300050004"); + expect(port._client._data.toString("hex")).to.equal("000700000006010300050004"); }); }); });