Skip to content

Commit

Permalink
All Transports now emit a request event right before passing the AD…
Browse files Browse the repository at this point in the history
…U to the underlying Connection.
  • Loading branch information
morkai committed Feb 14, 2014
1 parent 4ae8138 commit 126a853
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions example/init-serial-rtu.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ connection.on('data', function(data)
console.log('[connection#data]', data);
});

var transport = master.getTransport();

transport.on('request', function(transaction)
{
console.log('[transport#request] %s', transaction.getRequest());
});

master.once('connected', function()
{
var t1 = master.readDiscreteInputs(0x0000, 8, {
Expand Down
5 changes: 5 additions & 0 deletions example/verbose-init-tcp-ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ connection.on('data', function(data)
// periods and CRC checksum.
var transport = new IpTransport(connection);

transport.on('request', function(transaction)
{
console.log('[transport#request] %s', transaction.getRequest());
});

// Master is responsible for managing and executing transactions.
// Transactions, when executed, are placed in a queue and sent only if a number
// of currently sent requests is less than the `maxConcurrentRequests` option.
Expand Down
8 changes: 7 additions & 1 deletion lib/transports/AsciiTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var FRAME_LF = 0x0A;
* @constructor
* @extends {Transport}
* @param {Connection} connection
* @event request Emitted right before the ADU is passed to the underlying
* `Connection`.
*/
function AsciiTransport(connection)
{
Expand Down Expand Up @@ -99,7 +101,11 @@ AsciiTransport.prototype.sendRequest = function(transaction)

this.transaction = transaction;

this.connection.write(this.getAdu(transaction));
var adu = this.getAdu(transaction);

this.emit('request', transaction);

this.connection.write(adu);

transaction.start(this.handleTimeout);
};
Expand Down
4 changes: 4 additions & 0 deletions lib/transports/IpTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module.exports = IpTransport;
* @constructor
* @extends {Transport}
* @param {Connection} connection
* @event request Emitted right before the ADU is passed to the underlying
* `Connection`.
*/
function IpTransport(connection)
{
Expand Down Expand Up @@ -156,6 +158,8 @@ IpTransport.prototype.sendRequest = function(transaction)

this.transactions[id] = transaction;

this.emit('request', transaction);

this.connection.write(adu);

transaction.start(this.createTimeoutHandler(id));
Expand Down
8 changes: 7 additions & 1 deletion lib/transports/RtuTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ var CRC_TABLE = [
* @constructor
* @extends {Transport}
* @param {RtuTransport.Options|object} options
* @event request Emitted right before the ADU is passed to the underlying
* `Connection`.
*/
function RtuTransport(options)
{
Expand Down Expand Up @@ -169,7 +171,11 @@ RtuTransport.prototype.sendRequest = function(transaction)

this.transaction = transaction;

this.connection.write(this.getAdu(transaction));
var adu = this.getAdu(transaction);

this.emit('request', transaction);

this.connection.write(adu);

transaction.start(this.handleTimeout);
};
Expand Down

0 comments on commit 126a853

Please sign in to comment.