diff --git a/README.md b/README.md index d011bfd..86ca582 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ For getting started with node-serialport, we recommend you begin with the follow * [`.resume()`](#module_serialport--SerialPort+resume) * [`.close(callback)`](#module_serialport--SerialPort+close) * [`.set([options], [callback])`](#module_serialport--SerialPort+set) + * [`.get([callback])`](#module_serialport--SerialPort+get) * [`.flush([callback])`](#module_serialport--SerialPort+flush) * [`.drain([callback])`](#module_serialport--SerialPort+drain) * [`Event: "data"`](#module_serialport--SerialPort+event_data) @@ -83,6 +84,7 @@ For getting started with node-serialport, we recommend you begin with the follow * [`.list`](#module_serialport--SerialPort.list) : function * _inner_ * [`~errorCallback`](#module_serialport--SerialPort..errorCallback) : function + * [`~modemBitsCallback`](#module_serialport--SerialPort..modemBitsCallback) : function * [`~openOptions`](#module_serialport--SerialPort..openOptions) : Object * [`~listCallback`](#module_serialport--SerialPort..listCallback) : function * [Command Line Tools](#command-line-tools) @@ -451,6 +453,21 @@ Sets flags on an open port. Uses [`SetCommMask`](https://msdn.microsoft.com/en-u | [callback] | [errorCallback](#module_serialport--SerialPort..errorCallback) | | Called once the port's flags have been set. | +- + + + +#### `serialPort.get([callback])` +Returns the modem status bits (CTS, DSR, DCD) on the open port. +Uses [`GetCommModemStatus`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363258(v=vs.85).aspx) for windows and [`ioctl`](http://linux.die.net/man/4/tty_ioctl) for mac and linux. + +**Kind**: instance method of [SerialPort](#exp_module_serialport--SerialPort) + +| Param | Type | Description | +| --- | --- | --- | +| [callback] | [modemBitsCallback](#module_serialport--SerialPort..modemBitsCallback) | Called once the modem bits have been retrieved. | + + - @@ -646,6 +663,24 @@ A callback called with an error or null. | error | error | +- + + + +#### `SerialPort~modemBitsCallback` : function +A callback called with an error or an object with the modem line values (cts, dsr, dcd). + +**Kind**: inner typedef of [SerialPort](#exp_module_serialport--SerialPort) + +| Param | Type | Default | +| --- | --- | --- | +| error | error | | +| status | object | | +| [status.cts] | boolean | false | +| [status.dsr] | boolean | false | +| [status.dcd] | boolean | false | + + - diff --git a/lib/bindings.js b/lib/bindings.js index 66da6a2..64dce2c 100644 --- a/lib/bindings.js +++ b/lib/bindings.js @@ -21,6 +21,7 @@ module.exports = { open: bindings.open, SerialportPoller: bindings.SerialportPoller, set: bindings.set, + get: bindings.get, update: bindings.update, write: bindings.write, platformOptions: platformOptions diff --git a/lib/serialport.js b/lib/serialport.js index 81e3a62..3436e05 100644 --- a/lib/serialport.js +++ b/lib/serialport.js @@ -61,6 +61,16 @@ var defaultSetFlags = { * @param {?error} error */ + /** + * A callback called with an error or an object with the modem line values (cts, dsr, dcd). + * @typedef {function} modemBitsCallback + * @param {?error} error + * @param {?object} status + * @param {boolean} [status.cts=false] + * @param {boolean} [status.dsr=false] + * @param {boolean} [status.dcd=false] + */ + /** * @typedef {Object} openOptions * @property {boolean} [autoOpen=true] - Automatically opens the port on `nextTick` @@ -521,6 +531,26 @@ SerialPort.prototype.set = function(options, callback) { }.bind(this)); }; +/** + * Returns the modem status bits (CTS, DSR, DCD) on the open port. + * Uses [`GetCommModemStatus`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363258(v=vs.85).aspx) for windows and [`ioctl`](http://linux.die.net/man/4/tty_ioctl) for mac and linux. + * @param {module:serialport~modemBitsCallback=} callback Called once the modem bits have been retrieved. + */ +SerialPort.prototype.get = function(callback) { + if (!this.isOpen) { + debug('get attempted, but port is not open'); + return this._error(new Error('Port is not open'), callback); + } + + SerialPortBinding.get(this.fd, function(err, status) { + if (err) { + debug('SerialPortBinding.get had an error', err); + return this._error(err, callback); + } + if (callback) { callback.call(this, null, status) } + }.bind(this)); +}; + /** * Flush discards data received but not read and written but not transmitted. For more technical details see [`tcflush(fd, TCIFLUSH)`](http://linux.die.net/man/3/tcflush) for Mac/Linux and [`FlushFileBuffers`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364439) for Windows. * @param {module:serialport~errorCallback=} callback Called once the flush operation finishes. diff --git a/src/serialport.cpp b/src/serialport.cpp index d6ef516..dafe617 100644 --- a/src/serialport.cpp +++ b/src/serialport.cpp @@ -586,6 +586,62 @@ void EIO_AfterSet(uv_work_t* req) { delete req; } +NAN_METHOD(Get) { + // file descriptor + if (!info[0]->IsInt32()) { + Nan::ThrowTypeError("First argument must be an int"); + return; + } + int fd = info[0]->ToInt32()->Int32Value(); + + // callback + if (!info[1]->IsFunction()) { + Nan::ThrowTypeError("Second argument must be a function"); + return; + } + v8::Local callback = info[1].As(); + + GetBaton* baton = new GetBaton(); + memset(baton, 0, sizeof(GetBaton)); + baton->fd = fd; + baton->cts = false; + baton->dsr = false; + baton->dcd = false; + baton->callback = new Nan::Callback(callback); + + uv_work_t* req = new uv_work_t(); + req->data = baton; + uv_queue_work(uv_default_loop(), req, EIO_Get, (uv_after_work_cb)EIO_AfterGet); + + return; +} + +void EIO_AfterGet(uv_work_t* req) { + Nan::HandleScope scope; + + GetBaton* data = static_cast(req->data); + + v8::Local argv[2]; + + if (data->errorString[0]) { + argv[0] = v8::Exception::Error(Nan::New(data->errorString).ToLocalChecked()); + } else { + + v8::Local results = Nan::New(); + results->Set(Nan::New("cts").ToLocalChecked(), Nan::New(data->cts)); + results->Set(Nan::New("dsr").ToLocalChecked(), Nan::New(data->dsr)); + results->Set(Nan::New("dcd").ToLocalChecked(), Nan::New(data->dcd)); + + argv[0] = Nan::Null(); + argv[1] = results; + } + data->callback->Call(2, argv); + + delete data->callback; + delete data; + delete req; +} + NAN_METHOD(Drain) { // file descriptor if (!info[0]->IsInt32()) { @@ -665,6 +721,7 @@ extern "C" { void init(v8::Handle target) { Nan::HandleScope scope; Nan::SetMethod(target, "set", Set); + Nan::SetMethod(target, "get", Get); Nan::SetMethod(target, "open", Open); Nan::SetMethod(target, "update", Update); Nan::SetMethod(target, "write", Write); diff --git a/src/serialport.h b/src/serialport.h index aa2d692..50167c4 100644 --- a/src/serialport.h +++ b/src/serialport.h @@ -39,6 +39,10 @@ NAN_METHOD(Set); void EIO_Set(uv_work_t* req); void EIO_AfterSet(uv_work_t* req); +NAN_METHOD(Get); +void EIO_Get(uv_work_t* req); +void EIO_AfterGet(uv_work_t* req); + NAN_METHOD(Drain); void EIO_Drain(uv_work_t* req); void EIO_AfterDrain(uv_work_t* req); @@ -170,6 +174,15 @@ struct SetBaton { bool brk; }; +struct GetBaton { + int fd; + Nan::Callback* callback; + char errorString[ERROR_STRING_SIZE]; + bool cts; + bool dsr; + bool dcd; +}; + struct VoidBaton { int fd; Nan::Callback* callback; diff --git a/src/serialport_unix.cpp b/src/serialport_unix.cpp index 6a27e50..eec3edd 100644 --- a/src/serialport_unix.cpp +++ b/src/serialport_unix.cpp @@ -714,6 +714,17 @@ void EIO_Set(uv_work_t* req) { } } +void EIO_Get(uv_work_t* req) { + GetBaton* data = static_cast(req->data); + + int bits; + ioctl(data->fd, TIOCMGET, &bits); + + data->cts = bits & TIOCM_CTS; + data->dsr = bits & TIOCM_DSR; + data->dcd = bits & TIOCM_CD; +} + void EIO_Flush(uv_work_t* req) { VoidBaton* data = static_cast(req->data); diff --git a/src/serialport_win.cpp b/src/serialport_win.cpp index f577d2b..17490ce 100644 --- a/src/serialport_win.cpp +++ b/src/serialport_win.cpp @@ -238,6 +238,16 @@ void EIO_Set(uv_work_t* req) { } } +void EIO_Get(uv_work_t* req) { + GetBaton* data = static_cast(req->data); + + DWORD bits = 0; + GetCommModemStatus((HANDLE)data->fd, &bits); + + data->cts = bits & MS_CTS_ON; + data->dsr = bits & MS_DSR_ON; + data->dcd = bits & MS_RLSD_ON; +} void EIO_WatchPort(uv_work_t* req) { WatchPortBaton* data = static_cast(req->data); diff --git a/test/bindings.js b/test/bindings.js index 84ae914..9d22c76 100644 --- a/test/bindings.js +++ b/test/bindings.js @@ -375,4 +375,43 @@ describe('SerialPortBinding', function() { }); }); }); + + describe('#get', function() { + it('errors when given a bad fd', function(done) { + SerialPortBinding.drain(44, function(err) { + assert.instanceOf(err, Error); + done(); + }); + }); + + if (!testPort) { + it('Cannot be tested further. Set the TEST_PORT env var with an available serialport for more testing.'); + return; + } + + beforeEach(function(done) { + SerialPortBinding.open(testPort, defaultPortOpenOptions, function(err, fd) { + assert.isNull(err); + assert.isNumber(fd); + this.fd = fd; + done(); + }.bind(this)); + }); + + afterEach(function(done) { + SerialPortBinding.close(this.fd, done); + this.fd = null; + }); + + it('gets modem line status from the port', function(done) { + SerialPortBinding.get(this.fd, function(err, status) { + assert.isNull(err); + assert.isObject(status); + assert.isDefined(status.cts); + assert.isDefined(status.dsr); + assert.isDefined(status.dcd); + done(); + }); + }); + }); }); diff --git a/test/mocks/darwin-hardware.js b/test/mocks/darwin-hardware.js index 1fe841c..bb2aa31 100644 --- a/test/mocks/darwin-hardware.js +++ b/test/mocks/darwin-hardware.js @@ -44,6 +44,7 @@ var Hardware = function() { open: this.open.bind(this), SerialportPoller: mockSerialportPoller(this), set: this.set.bind(this), + get: this.get.bind(this), update: this.update.bind(this), write: this.write.bind(this) }; @@ -160,6 +161,18 @@ Hardware.prototype.set = function(fd, options, cb) { callLater(cb, null, undefined); }; +Hardware.prototype.get = function(fd, cb) { + var port = this.fds[fd]; + if (!port) { + return cb(new Error(fd + ' does not exist - please call hardware.createPort(path) first')); + } + callLater(cb, null, { + cts: false, + dsr: false, + dcd: false + }); +}; + Hardware.prototype.drain = function(fd, cb) { var port = this.fds[fd]; if (!port) { diff --git a/test/serialport.js b/test/serialport.js index 78facbe..5db6e87 100644 --- a/test/serialport.js +++ b/test/serialport.js @@ -530,6 +530,36 @@ describe('SerialPort', function() { }); }); + describe('#get', function() { + it('errors when serialport not open', function(done) { + var cb = function() {}; + var port = new SerialPort('/dev/exists', { autoOpen: false }, cb); + port.get(function(err) { + assert.instanceOf(err, Error); + done(); + }); + }); + + it('gets the status from the ports bindings', function(done) { + var status = { + cts: true, + dsr: true, + dcd: true + }; + + sandbox.stub(bindings, 'get', function(fd, cb) { + cb(status); + }); + + var port = new SerialPort('/dev/exists', function() { + port.get(function(modemStatus) { + assert.deepEqual(modemStatus, status); + done(); + }); + }); + }); + }); + it('flush errors when serialport not open', function(done) { var cb = function() {}; var port = new SerialPort('/dev/exists', { autoOpen: false }, cb);