From a1cecbee1276bfe78b0491f8d13544c70859ff36 Mon Sep 17 00:00:00 2001 From: Suz Hinton Date: Sun, 17 Jul 2016 15:12:38 -0400 Subject: [PATCH 1/2] update deprecated API patterns in sync with serialport 4.x --- README.md | 9 +++++---- index.js | 17 ++++++++--------- test/serialport-basic.js | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4358060..ad38571 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ To Use Opening a serial port: ```js -var SerialPort = require("browser-serialport").SerialPort +var SerialPort = require("browser-serialport"); var serialPort = new SerialPort("/dev/tty-usbserial1", { baudrate: 57600 }); @@ -84,13 +84,14 @@ serialPort.on("open", function () { }); ``` -You can also call the open function, in this case instanciate the serialport with an additional flag. +You can also call the open function, in this case instantiate the serialport with an additional `autoOpen` property set to `false`. ```js var SerialPort = require("browser-serialport").SerialPort var serialPort = new SerialPort("/dev/tty-usbserial1", { - baudrate: 57600 -}, false); // this is the openImmediately flag [default is true] + baudrate: 57600, + autoOpen: false +}); serialPort.open(function (error) { if ( error ) { diff --git a/index.js b/index.js index 67c8b7c..72610de 100644 --- a/index.js +++ b/index.js @@ -45,7 +45,7 @@ function convertOptions(options){ return options; } -function SerialPort(path, options, openImmediately, callback) { +function SerialPort(path, options, callback) { EE.call(this); @@ -59,7 +59,7 @@ function SerialPort(path, options, openImmediately, callback) { options = (typeof options !== 'function') && options || {}; - openImmediately = (openImmediately === undefined || openImmediately === null) ? true : openImmediately; + var autoOpen = (options.autoOpen === undefined || options.autoOpen === null) ? true : options.autoOpen; callback = callback || function (err) { if (err) { @@ -166,7 +166,7 @@ function SerialPort(path, options, openImmediately, callback) { this.path = path; - if (openImmediately) { + if (autoOpen) { process.nextTick(function () { self.open(callback); }); @@ -411,9 +411,8 @@ function toBuffer(ab) { return buffer; } -module.exports = { - SerialPort: SerialPort, - list: SerialPortList, - buffer2ArrayBuffer: buffer2ArrayBuffer, - used: [] //TODO: Populate this somewhere. -}; +SerialPort.buffer2ArrayBuffer = buffer2ArrayBuffer; +SerialPort.list = SerialPortList; +SerialPort.used = []; + +module.exports = SerialPort; diff --git a/test/serialport-basic.js b/test/serialport-basic.js index e58a127..5d3ff58 100644 --- a/test/serialport-basic.js +++ b/test/serialport-basic.js @@ -6,7 +6,7 @@ var without = require('lodash/array/without'); var expect = chai.expect; var MockedSerialPort = require('../'); -var SerialPort = MockedSerialPort.SerialPort; +var SerialPort = MockedSerialPort; var options; From c8628c41c11890d3058875994c15f83f2df8185b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Philippe=20C=C3=B4t=C3=A9?= Date: Sun, 4 Dec 2016 19:27:09 -0500 Subject: [PATCH 2/2] Added isOpen() method Added isOpen() method to improve compatibility with node-serialport. --- index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.js b/index.js index 72610de..4e21ac2 100644 --- a/index.js +++ b/index.js @@ -360,6 +360,10 @@ SerialPort.prototype.set = function (options, callback) { }); }; +SerialPort.prototype.isOpen = function () { + return this.connectionId > -1; +}; + function SerialPortList(callback) { if (typeof chrome != 'undefined' && chrome.serial) { chrome.serial.getDevices(function(ports) {