Skip to content

Commit

Permalink
Merge pull request #24 from yaacov/add-close-method
Browse files Browse the repository at this point in the history
Add a close method
  • Loading branch information
yaacov committed May 30, 2016
2 parents ba9d237 + 265134b commit bf85f68
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare namespace ModbusRTU {
new(port?: any): IModbusRTU;

open(callback: Function): void;
close(callback: Function): void;

writeFC1(address: number, dataAddress: number, length: number, next: NodeStyleCallback<ReadCoilResult>): void;
writeFC2(address: number, dataAddress: number, length: number, next: NodeStyleCallback<ReadCoilResult>): void;
Expand Down
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ ModbusRTU.prototype.open = function (callback) {
});
};

/**
* Close the serial port
*
* @param {Function} callback the function to call next on close success
* or failure.
*/
ModbusRTU.prototype.close = function (callback) {
// close the serial port
this._port.close(callback);
};

/**
* Write a Modbus "Read Coil Status" (FC=01) to serial port.
*
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ describe('ModbusRTU', function() {
});
});

describe('#close() - close serial port.', function () {
it('should close the port without errors', function (done) {
modbusRTU.close(function(err) {
expect(err).to.be.a('null');

done();
});
});
});

describe('#writeFC3() - read holding registers.', function () {
it('should read 3 registers [0xa12b, 0xffff, 0xb21a] without errors', function (done) {
modbusRTU.writeFC3(1, 8, 3, function(err, data) {
Expand Down

0 comments on commit bf85f68

Please sign in to comment.