From 38237cb18d70e023069ffcc667c81fc3bae57fa3 Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Mon, 27 Oct 2014 12:18:26 +0100 Subject: [PATCH] Add options.platformOptions to allow setting platform specific options on .open() --- serialport.js | 35 ++++++++++++++++++++++++----------- src/serialport.cpp | 4 ++++ src/serialport.h | 1 + src/serialport_unix.cpp | 17 +++++++++++++++-- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/serialport.js b/serialport.js index 4ce025c..f0165ae 100644 --- a/serialport.js +++ b/serialport.js @@ -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(); \ No newline at end of file diff --git a/src/serialport.cpp b/src/serialport.cpp index 083f4b8..93e620d 100644 --- a/src/serialport.cpp +++ b/src/serialport.cpp @@ -52,6 +52,9 @@ NAN_METHOD(Open) { baton->xoff = options->Get(NanNew("xoff"))->ToBoolean()->BooleanValue(); baton->xany = options->Get(NanNew("xany"))->ToBoolean()->BooleanValue(); + v8::Local platformOptions = options->Get(NanNew("platformOptions")).As(); + NanAssignPersistent(baton->platformOptions, platformOptions); + baton->callback = new NanCallback(callback); baton->dataCallback = new NanCallback(options->Get(NanNew("dataCallback")).As()); baton->disconnectedCallback = new NanCallback(options->Get(NanNew("disconnectedCallback")).As()); @@ -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; diff --git a/src/serialport.h b/src/serialport.h index 011520b..0505e25 100644 --- a/src/serialport.h +++ b/src/serialport.h @@ -72,6 +72,7 @@ public: bool dsrdtr; SerialPortParity parity; SerialPortStopBits stopBits; + v8::Persistent platformOptions; char errorString[ERROR_STRING_SIZE]; }; diff --git a/src/serialport_unix.cpp b/src/serialport_unix.cpp index 42a52b2..ad4faab 100644 --- a/src/serialport_unix.cpp +++ b/src/serialport_unix.cpp @@ -29,6 +29,12 @@ Boolean lockInitialised = FALSE; #include #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(req->data); + // parse platform specific options + + UnixPlatformOptions platformOptions; + platformOptions.vmin = (uint8_t)(data->platformOptions->Get(NanNew("vmin"))->ToUint32()->Uint32Value()); + platformOptions.vtime = (uint8_t)(data->platformOptions->Get(NanNew("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);