Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ You can also call the open function, in this case instanciate the serialport wit
```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 //autoopen flag [default is true]
});

serialPort.open(function (error) {
if ( error ) {
Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var _options = {
rtscts: false,
databits: 8,
stopbits: 1,
buffersize: 256
buffersize: 256,
autoopen: true
};

function convertOptions(options){
Expand Down Expand Up @@ -45,7 +46,7 @@ function convertOptions(options){
return options;
}

function SerialPort(path, options, openImmediately, callback) {
function SerialPort(path, options, callback) {

EE.call(this);

Expand All @@ -59,7 +60,7 @@ function SerialPort(path, options, openImmediately, callback) {

options = (typeof options !== 'function') && options || {};

openImmediately = (openImmediately === undefined || openImmediately === null) ? true : openImmediately;
options.autoOpen = options.autoOpen || options.autoopen || _options.autoopen;

callback = callback || function (err) {
if (err) {
Expand Down Expand Up @@ -166,7 +167,7 @@ function SerialPort(path, options, openImmediately, callback) {

this.path = path;

if (openImmediately) {
if (options.autoOpen) {
process.nextTick(function () {
self.open(callback);
});
Expand Down
37 changes: 23 additions & 14 deletions test/serialport-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe('SerialPort', function () {
done();
};

var port = new SerialPort('/dev/exists', { databits : 19 }, false, errorCallback);
var port = new SerialPort('/dev/exists', { databits : 19, autoopen: false }, errorCallback);
});

it('errors with invalid stopbits', function (done) {
Expand All @@ -182,7 +182,7 @@ describe('SerialPort', function () {
done();
};

var port = new SerialPort('/dev/exists', { stopbits : 19 }, false, errorCallback);
var port = new SerialPort('/dev/exists', { stopbits : 19, autoopen: false }, errorCallback);
});

it('errors with invalid parity', function (done) {
Expand All @@ -191,7 +191,7 @@ describe('SerialPort', function () {
done();
};

var port = new SerialPort('/dev/exists', { parity : 'pumpkins' }, false, errorCallback);
var port = new SerialPort('/dev/exists', { parity : 'pumpkins', autoopen: false }, errorCallback);
});

it('errors with invalid flow control', function (done) {
Expand All @@ -200,7 +200,7 @@ describe('SerialPort', function () {
done();
};

var port = new SerialPort('/dev/exists', { flowcontrol : ['pumpkins'] }, false, errorCallback);
var port = new SerialPort('/dev/exists', { flowcontrol : ['pumpkins'], autoopen: false }, errorCallback);
});

it('errors with invalid path', function (done) {
Expand All @@ -209,7 +209,7 @@ describe('SerialPort', function () {
done();
};

var port = new SerialPort(null, false, errorCallback);
var port = new SerialPort(null, { autoopen: false }, errorCallback);
});

it('allows optional options', function (done) {
Expand All @@ -228,7 +228,8 @@ describe('SerialPort', function () {

it('write errors when serialport not open', function (done) {
var cb = function () {};
var port = new SerialPort('/dev/exists', options, false, cb);
options.autoopen = false;
var port = new SerialPort('/dev/exists', options, cb);
port.write(null, function(err){
chai.assert.isDefined(err, 'err is not defined');
done();
Expand All @@ -237,7 +238,8 @@ describe('SerialPort', function () {

it('close errors when serialport not open', function (done) {
var cb = function () {};
var port = new SerialPort('/dev/exists', options, false, cb);
options.autoopen = false;
var port = new SerialPort('/dev/exists', options, cb);
port.close(function(err){
chai.assert.isDefined(err, 'err is not defined');
done();
Expand All @@ -246,7 +248,8 @@ describe('SerialPort', function () {

it('flush errors when serialport not open', function (done) {
var cb = function () {};
var port = new SerialPort('/dev/exists', options, false, cb);
options.autoopen = false;
var port = new SerialPort('/dev/exists', options, cb);
port.flush(function(err){
chai.assert.isDefined(err, 'err is not defined');
done();
Expand All @@ -255,7 +258,8 @@ describe('SerialPort', function () {

it('set errors when serialport not open', function (done) {
var cb = function () {};
var port = new SerialPort('/dev/exists', options, false, cb);
options.autoopen = false;
var port = new SerialPort('/dev/exists', options, cb);
port.set({}, function(err){
chai.assert.isDefined(err, 'err is not defined');
done();
Expand All @@ -264,7 +268,8 @@ describe('SerialPort', function () {

it('drain errors when serialport not open', function (done) {
var cb = function () {};
var port = new SerialPort('/dev/exists', options, false, cb);
options.autoopen = false;
var port = new SerialPort('/dev/exists', options, cb);
port.drain(function(err){
chai.assert.isDefined(err, 'err is not defined');
done();
Expand Down Expand Up @@ -304,7 +309,8 @@ describe('SerialPort', function () {

it('passes the port to the bindings', function (done) {
var openSpy = sandbox.spy(options.serial, 'connect');
var port = new SerialPort('/dev/exists', options, false);
options.autoopen = false;
var port = new SerialPort('/dev/exists', options);
port.open(function (err) {
expect(err).to.not.be.ok;
expect(openSpy.calledWith('/dev/exists'));
Expand All @@ -313,7 +319,8 @@ describe('SerialPort', function () {
});

it('calls back an error when opening an invalid port', function (done) {
var port = new SerialPort('/dev/unhappy', options, false);
options.autoopen = false;
var port = new SerialPort('/dev/unhappy', options);
port.open(function (err) {
expect(err).to.be.ok;
done();
Expand Down Expand Up @@ -359,7 +366,8 @@ describe('SerialPort', function () {
describe('#send', function () {

it('errors when writing a closed port', function (done) {
var port = new SerialPort('/dev/exists', options, false);
options.autoopen = false;
var port = new SerialPort('/dev/exists', options);
port.write(new Buffer(''), function(err){
expect(err).to.be.ok;
done();
Expand Down Expand Up @@ -392,7 +400,8 @@ describe('SerialPort', function () {
});

it('errors when closing an invalid port', function (done) {
var port = new SerialPort('/dev/exists', options, false);
options.autoopen = false;
var port = new SerialPort('/dev/exists', options);
port.close(function(err){
expect(err).to.be.ok;
done();
Expand Down