mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-25 18:37:19 +00:00
Merge pull request #967 from jgillick/master
Get modem status bits (CTS, DSR, DCD)
This commit is contained in:
commit
0d757fd72c
10 changed files with 239 additions and 0 deletions
35
README.md
35
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) : <code>function</code>
|
||||
* _inner_
|
||||
* [`~errorCallback`](#module_serialport--SerialPort..errorCallback) : <code>function</code>
|
||||
* [`~modemBitsCallback`](#module_serialport--SerialPort..modemBitsCallback) : <code>function</code>
|
||||
* [`~openOptions`](#module_serialport--SerialPort..openOptions) : <code>Object</code>
|
||||
* [`~listCallback`](#module_serialport--SerialPort..listCallback) : <code>function</code>
|
||||
* [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] | <code>[errorCallback](#module_serialport--SerialPort..errorCallback)</code> | | Called once the port's flags have been set. |
|
||||
|
||||
|
||||
-
|
||||
|
||||
<a name="module_serialport--SerialPort+get"></a>
|
||||
|
||||
#### `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 <code>[SerialPort](#exp_module_serialport--SerialPort)</code>
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| [callback] | <code>[modemBitsCallback](#module_serialport--SerialPort..modemBitsCallback)</code> | Called once the modem bits have been retrieved. |
|
||||
|
||||
|
||||
-
|
||||
|
||||
<a name="module_serialport--SerialPort+flush"></a>
|
||||
|
|
@ -646,6 +663,24 @@ A callback called with an error or null.
|
|||
| error | <code>error</code> |
|
||||
|
||||
|
||||
-
|
||||
|
||||
<a name="module_serialport--SerialPort..modemBitsCallback"></a>
|
||||
|
||||
#### `SerialPort~modemBitsCallback` : <code>function</code>
|
||||
A callback called with an error or an object with the modem line values (cts, dsr, dcd).
|
||||
|
||||
**Kind**: inner typedef of <code>[SerialPort](#exp_module_serialport--SerialPort)</code>
|
||||
|
||||
| Param | Type | Default |
|
||||
| --- | --- | --- |
|
||||
| error | <code>error</code> | |
|
||||
| status | <code>object</code> | |
|
||||
| [status.cts] | <code>boolean</code> | <code>false</code> |
|
||||
| [status.dsr] | <code>boolean</code> | <code>false</code> |
|
||||
| [status.dcd] | <code>boolean</code> | <code>false</code> |
|
||||
|
||||
|
||||
-
|
||||
|
||||
<a name="module_serialport--SerialPort..openOptions"></a>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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<v8::Function> callback = info[1].As<v8::Function>();
|
||||
|
||||
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<GetBaton*>(req->data);
|
||||
|
||||
v8::Local<v8::Value> argv[2];
|
||||
|
||||
if (data->errorString[0]) {
|
||||
argv[0] = v8::Exception::Error(Nan::New<v8::String>(data->errorString).ToLocalChecked());
|
||||
} else {
|
||||
|
||||
v8::Local<v8::Object> results = Nan::New<v8::Object>();
|
||||
results->Set(Nan::New<v8::String>("cts").ToLocalChecked(), Nan::New<v8::Boolean>(data->cts));
|
||||
results->Set(Nan::New<v8::String>("dsr").ToLocalChecked(), Nan::New<v8::Boolean>(data->dsr));
|
||||
results->Set(Nan::New<v8::String>("dcd").ToLocalChecked(), Nan::New<v8::Boolean>(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<v8::Object> 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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -714,6 +714,17 @@ void EIO_Set(uv_work_t* req) {
|
|||
}
|
||||
}
|
||||
|
||||
void EIO_Get(uv_work_t* req) {
|
||||
GetBaton* data = static_cast<GetBaton*>(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<VoidBaton*>(req->data);
|
||||
|
||||
|
|
|
|||
|
|
@ -238,6 +238,16 @@ void EIO_Set(uv_work_t* req) {
|
|||
}
|
||||
}
|
||||
|
||||
void EIO_Get(uv_work_t* req) {
|
||||
GetBaton* data = static_cast<GetBaton*>(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<WatchPortBaton*>(req->data);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue