Skip to content

Commit

Permalink
separate transaction read and write counters
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacov committed May 9, 2017
1 parent 6cce55a commit b0edd87
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
17 changes: 9 additions & 8 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._transactionId];
var transaction = this._transactions[this._port.transactionsCounter];

this._port.write(buffer);
if (transaction) {
Expand Down Expand Up @@ -207,8 +207,9 @@ ModbusRTU.prototype.open = function(callback) {
if (callback)
callback(error);

/* init ports transaction id */
/* init ports transaction id and counter */
modbus._port._transactionId = 1;
modbus._port.transactionsCounter = 1;

/* On serial port success
* register the modbus parser functions
Expand Down Expand Up @@ -380,7 +381,7 @@ ModbusRTU.prototype.writeFC2 = function(address, dataAddress, length, next, code
code = code || 2;

// set state variables
this._transactions[this._port._transactionId] = {
this._transactions[this._port.transactionsCounter] = {
nextAddress: address,
nextCode: code,
nextLength: 3 + parseInt((length - 1) / 8 + 1) + 2,
Expand Down Expand Up @@ -433,7 +434,7 @@ ModbusRTU.prototype.writeFC4 = function(address, dataAddress, length, next, code
code = code || 4;

// set state variables
this._transactions[this._port._transactionId] = {
this._transactions[this._port.transactionsCounter] = {
nextAddress: address,
nextCode: code,
nextLength: 3 + 2 * length + 2,
Expand Down Expand Up @@ -473,7 +474,7 @@ ModbusRTU.prototype.writeFC5 = function(address, dataAddress, state, next) {
var code = 5;

// set state variables
this._transactions[this._port._transactionId] = {
this._transactions[this._port.transactionsCounter] = {
nextAddress: address,
nextCode: code,
nextLength: 8,
Expand Down Expand Up @@ -518,7 +519,7 @@ ModbusRTU.prototype.writeFC6 = function(address, dataAddress, value, next) {
var code = 6;

// set state variables
this._transactions[this._port._transactionId] = {
this._transactions[this._port.transactionsCounter] = {
nextAddress: address,
nextCode: code,
nextLength: 8,
Expand Down Expand Up @@ -560,7 +561,7 @@ ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) {
var i = 0;

// set state variables
this._transactions[this._port._transactionId] = {
this._transactions[this._port.transactionsCounter] = {
nextAddress: address,
nextCode: code,
nextLength: 8,
Expand Down Expand Up @@ -615,7 +616,7 @@ ModbusRTU.prototype.writeFC16 = function(address, dataAddress, array, next) {
var code = 16;

// set state variables
this._transactions[this._port._transactionId] = {
this._transactions[this._port.transactionsCounter] = {
nextAddress: address,
nextCode: code,
nextLength: 8,
Expand Down
9 changes: 5 additions & 4 deletions ports/tcpport.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var TcpPort = function(ip, options) {
this.ip = ip;
this.openFlag = false;
this.callback = null;
this.transactionsCounter = 1;

// options
if (typeof(options) === "undefined") options = {};
Expand Down Expand Up @@ -115,16 +116,16 @@ TcpPort.prototype.write = function(data) {
return;
}

// 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.transactionsCounter, 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;

// send buffer to slave
this._client.write(buffer);

Expand Down
10 changes: 5 additions & 5 deletions test/ports/tcpport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ describe("Modbus TCP port", function() {
port.open(function() {
port.write(new Buffer("1103006B00037687", "hex"));

if (port._client._data.equals(new Buffer("0000000000061103006B0003", "hex"))) {
port._client.receive(new Buffer("000000000006110366778899", "hex"));
if (port._client._data.equals(new Buffer("0001000000061103006B0003", "hex"))) {
port._client.receive(new Buffer("000100000006110366778899", "hex"));
}
});
});
Expand All @@ -71,8 +71,8 @@ describe("Modbus TCP port", function() {
port.open(function() {
port.write(new Buffer("1103006B00037687", "hex"));

if (port._client._data.equals(new Buffer("0001000000061103006B0003", "hex"))) {
port._client.receive(new Buffer("000100000005118304", "hex"));
if (port._client._data.equals(new Buffer("0002000000061103006B0003", "hex"))) {
port._client.receive(new Buffer("000200000005118304", "hex"));
}
});
});
Expand All @@ -81,7 +81,7 @@ describe("Modbus TCP port", function() {
describe("#write", function() {
it("should write a valid TCP message to the port", function() {
port.write(new Buffer("1103006B00037687", "hex"));
expect(port._client._data.toString("hex")).to.equal("0002000000061103006b0003");
expect(port._client._data.toString("hex")).to.equal("0003000000061103006b0003");
});
});

Expand Down

0 comments on commit b0edd87

Please sign in to comment.