Add options.platformOptions to allow setting platform specific options on .open()

This commit is contained in:
Johannes Rudolph 2014-10-27 12:18:26 +01:00
parent 5ef7fa759c
commit 38237cb18d
4 changed files with 44 additions and 13 deletions

View file

@ -9,7 +9,7 @@
var binary = require('node-pre-gyp');
var path = require('path');
var PACKAGE_JSON = path.join(__dirname,'package.json');
var PACKAGE_JSON = path.join(__dirname, 'package.json');
var binding_path = binary.find(path.resolve(PACKAGE_JSON));
var SerialPortBinding = require(binding_path);
@ -39,6 +39,17 @@ function SerialPortFactory() {
var kPoolSize = 40 * 1024;
var kMinPoolSpace = 128;
function makeDefaultPlatformOptions(){
var options = {};
if (process.platform !== 'win32') {
options.vmin = 1;
options.vtime = 0;
}
return options;
}
// The default options, can be overwritten in the 'SerialPort' constructor
var _options = {
baudrate: 9600,
@ -50,7 +61,8 @@ function SerialPortFactory() {
databits: 8,
stopbits: 1,
buffersize: 256,
parser: parsers.raw
parser: parsers.raw,
platformOptions: makeDefaultPlatformOptions()
};
function SerialPort(path, options, openImmediately, callback) {
@ -59,7 +71,7 @@ function SerialPortFactory() {
var args = Array.prototype.slice.call(arguments);
callback = args.pop();
if (typeof(callback) !== 'function') {
if (typeof (callback) !== 'function') {
callback = null;
}
@ -144,6 +156,7 @@ function SerialPortFactory() {
options.bufferSize = options.bufferSize || options.buffersize || _options.buffersize;
options.parser = options.parser || _options.parser;
options.platformOptions = options.platformOptions || _options.platformOptions;
options.dataCallback = options.dataCallback || function (data) {
options.parser(self, data);
@ -153,10 +166,10 @@ function SerialPortFactory() {
if (self.closing) {
return;
}
if(!err) {
if (!err) {
err = new Error("Disconnected");
}
self.emit("disconnect",err);
self.emit("disconnect", err);
};
if (process.platform !== 'win32') {
@ -199,8 +212,8 @@ function SerialPortFactory() {
if (process.platform !== 'win32') {
self.paused = false;
self.serialPoller = new factory.SerialPortBinding.SerialportPoller(self.fd, function (err) {
if(!err) {
self._read();
if (!err) {
self._read();
} else {
self.disconnected(err);
}
@ -275,7 +288,7 @@ function SerialPortFactory() {
if (self.fd >= 0) {
self.serialPoller.start();
}
} else if (err.code && (err.code === "EBADF" || err.code === 'ENXIO' || (err.errno===-1 || err.code === 'UNKNOWN'))) { // handle edge case were mac/unix doesn't clearly know the error.
} else if (err.code && (err.code === "EBADF" || err.code === 'ENXIO' || (err.errno === -1 || err.code === 'UNKNOWN'))) { // handle edge case were mac/unix doesn't clearly know the error.
self.disconnected(err);
} else {
self.fd = null;
@ -372,7 +385,7 @@ function SerialPortFactory() {
try {
factory.SerialPortBinding.close(fd, function (err) {
if(err) {
if (err) {
console.log('Disconnect completed with error:' + err);
} else {
console.log('Disconnect completed');
@ -420,7 +433,7 @@ function SerialPortFactory() {
self.readable = false;
self.serialPoller.close();
}
try {
factory.SerialPortBinding.close(fd, function (err) {
@ -563,4 +576,4 @@ function SerialPortFactory() {
util.inherits(SerialPortFactory, EventEmitter);
module.exports = new SerialPortFactory();
module.exports = new SerialPortFactory();

View file

@ -52,6 +52,9 @@ NAN_METHOD(Open) {
baton->xoff = options->Get(NanNew<v8::String>("xoff"))->ToBoolean()->BooleanValue();
baton->xany = options->Get(NanNew<v8::String>("xany"))->ToBoolean()->BooleanValue();
v8::Local<v8::Object> platformOptions = options->Get(NanNew<v8::String>("platformOptions")).As<v8::Object>();
NanAssignPersistent(baton->platformOptions, platformOptions);
baton->callback = new NanCallback(callback);
baton->dataCallback = new NanCallback(options->Get(NanNew<v8::String>("dataCallback")).As<v8::Function>());
baton->disconnectedCallback = new NanCallback(options->Get(NanNew<v8::String>("disconnectedCallback")).As<v8::Function>());
@ -86,6 +89,7 @@ void EIO_AfterOpen(uv_work_t* req) {
data->callback->Call(2, argv);
NanDisposePersistent(data->platformOptions);
delete data->callback;
delete data;
delete req;

View file

@ -72,6 +72,7 @@ public:
bool dsrdtr;
SerialPortParity parity;
SerialPortStopBits stopBits;
v8::Persistent<v8::Object> platformOptions;
char errorString[ERROR_STRING_SIZE];
};

View file

@ -29,6 +29,12 @@ Boolean lockInitialised = FALSE;
#include <linux/serial.h>
#endif
struct UnixPlatformOptions {
public:
uint8_t vmin;
uint8_t vtime;
};
int ToBaudConstant(int baudRate);
int ToDataBitsConstant(int dataBits);
int ToStopBitsConstant(SerialPortStopBits stopBits);
@ -110,6 +116,12 @@ int ToDataBitsConstant(int dataBits) {
void EIO_Open(uv_work_t* req) {
OpenBaton* data = static_cast<OpenBaton*>(req->data);
// parse platform specific options
UnixPlatformOptions platformOptions;
platformOptions.vmin = (uint8_t)(data->platformOptions->Get(NanNew<v8::String>("vmin"))->ToUint32()->Uint32Value());
platformOptions.vtime = (uint8_t)(data->platformOptions->Get(NanNew<v8::String>("vtime"))->ToUint32()->Uint32Value());
int baudRate = ToBaudConstant(data->baudRate);
// #if not ( defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4) )
@ -281,8 +293,9 @@ void EIO_Open(uv_work_t* req) {
// ICANON makes partial lines not readable. It should be otional.
// It works with ICRNL. -Giseburt
options.c_lflag = 0; //ICANON;
options.c_cc[VMIN]=1;
options.c_cc[VTIME]=0;
options.c_cc[VMIN]= platformOptions.vmin;
options.c_cc[VTIME]= platformOptions.vtime;
// removed this unneeded sleep.
// sleep(1);