Skip to content

Commit

Permalink
Rename the transction ID's for reading and writing
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacov committed May 10, 2017
1 parent 6b61f32 commit 1e891a2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions ports/tcpport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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 }));
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions ports/tcprtubufferedport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion test/ports/tcpportrtubuffered.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
});

0 comments on commit 1e891a2

Please sign in to comment.