diff --git a/.docs/README.hbs b/.docs/README.hbs index 75f1fca..44329ed 100644 --- a/.docs/README.hbs +++ b/.docs/README.hbs @@ -233,13 +233,12 @@ When opening a serial port, you can specify (in this order). ### Opening a Port -Constructing a `SerialPort` object will open a port on `nextTick`. You can bind events while the port is opening but you must wait until it is open to `write()` to it. (Most port functions require an open port.) You can call code when a port is opened in three ways. +Constructing a `SerialPort` object will open a port immediately. While you can read and write at any time (it will be queued into the port is open), most port functions require an open port. You can call code when a port is opened in three ways. - The `open` event is always emitted when the port is opened - The constructor's openCallback is passed to `.open()` when the `autoOpen` option hasn't been disabled, if you have disabled it the callback is ignored. - The `.open()` function takes a callback that is called after the port is opened. This can be used if you disabled the `autoOpen` option or have previously closed an open port. - ```js var SerialPort = require('serialport'); var port = new SerialPort('/dev/tty-usbserial1'); @@ -276,7 +275,6 @@ var port = new SerialPort('/dev/tty-usbserial1', function (err) { ``` When disabling the `autoOpen` option you'll need to open the port on your own. - ```js var SerialPort = require('serialport'); var port = new SerialPort('/dev/tty-usbserial1', { autoOpen: false }); @@ -313,6 +311,21 @@ port.write(new Buffer('Hi Mom!')); Enjoy and do cool things with this code. +### Debugging + +We make use of the [debug](https://www.npmjs.com/package/debug) package and log under the `serialport` namespace. We log; + + - `serialport:main` for all high level main logging + - `serialport:binding` for all low level logging + +You can enable logging through environment variables. Check out the [debug](https://www.npmjs.com/package/debug) docs for more information. + +```bash +DEBUG=serialport:main node myapp.js +DEBUG=serialport:* node myapp.js +DEBUG=* node myapp.js +``` + {{#module name="serialport"}} {{>members~}} {{/module}} diff --git a/README.md b/README.md index 60cf408..2d27c5d 100644 --- a/README.md +++ b/README.md @@ -279,13 +279,12 @@ When opening a serial port, you can specify (in this order). ### Opening a Port -Constructing a `SerialPort` object will open a port on `nextTick`. You can bind events while the port is opening but you must wait until it is open to `write()` to it. (Most port functions require an open port.) You can call code when a port is opened in three ways. +Constructing a `SerialPort` object will open a port immediately. While you can read and write at any time (it will be queued into the port is open), most port functions require an open port. You can call code when a port is opened in three ways. - The `open` event is always emitted when the port is opened - The constructor's openCallback is passed to `.open()` when the `autoOpen` option hasn't been disabled, if you have disabled it the callback is ignored. - The `.open()` function takes a callback that is called after the port is opened. This can be used if you disabled the `autoOpen` option or have previously closed an open port. - ```js var SerialPort = require('serialport'); var port = new SerialPort('/dev/tty-usbserial1'); @@ -322,7 +321,6 @@ var port = new SerialPort('/dev/tty-usbserial1', function (err) { ``` When disabling the `autoOpen` option you'll need to open the port on your own. - ```js var SerialPort = require('serialport'); var port = new SerialPort('/dev/tty-usbserial1', { autoOpen: false }); @@ -359,6 +357,21 @@ port.write(new Buffer('Hi Mom!')); Enjoy and do cool things with this code. +### Debugging + +We make use of the [debug](https://www.npmjs.com/package/debug) package and log under the `serialport` namespace. We log; + + - `serialport:main` for all high level main logging + - `serialport:binding` for all low level logging + +You can enable logging through environment variables. Check out the [debug](https://www.npmjs.com/package/debug) docs for more information. + +```bash +DEBUG=serialport:main node myapp.js +DEBUG=serialport:* node myapp.js +DEBUG=* node myapp.js +``` + ### SerialPort ⏏ diff --git a/lib/bindings-auto-detect.js b/lib/bindings-auto-detect.js index 2e61e61..5de472c 100644 --- a/lib/bindings-auto-detect.js +++ b/lib/bindings-auto-detect.js @@ -1,5 +1,7 @@ 'use strict'; +var debug = require('debug')('serialport:binding'); + /** * @module serialport */ @@ -18,12 +20,15 @@ switch (process.platform) { case 'win32': + debug('loading WindowsBinding'); module.exports = require('./bindings-win32'); break; case 'darwin': + debug('loading DarwinBinding'); module.exports = require('./bindings-darwin'); break; default: + debug('loading LinuxBinding'); module.exports = require('./bindings-linux'); } diff --git a/lib/serialport.js b/lib/serialport.js index c368ef0..a340aa9 100644 --- a/lib/serialport.js +++ b/lib/serialport.js @@ -7,7 +7,7 @@ // 3rd Party Dependencies var stream = require('readable-stream'); -var debug = require('debug')('serialport'); +var debug = require('debug')('serialport:main'); // Internal Dependencies var mixInPushMethods = require('./push-methods'); @@ -21,7 +21,6 @@ var DATABITS = [5, 6, 7, 8]; var STOPBITS = [1, 1.5, 2]; var PARITY = ['none', 'even', 'mark', 'odd', 'space']; var FLOWCONTROLS = ['xon', 'xoff', 'xany', 'rtscts']; -var SET_OPTIONS = ['brk', 'cts', 'dtr', 'dts', 'rts']; var defaultSettings = { autoOpen: true, @@ -213,6 +212,10 @@ SerialPort.prototype._error = function(error, callback) { } }; +SerialPort.prototype._asyncError = function(error, callback) { + process.nextTick(this._error.bind(this, error,callback)); +}; + /** * The `open` event's callback is called with no arguments when the port is opened and ready for writing. This happens if you have the constructor open immediately (which opens in the next tick) or if you open the port manually with `open()`. See [Useage/Opening a Port](#opening-a-port) for more information. * @event module:serialport#open @@ -225,11 +228,11 @@ SerialPort.prototype._error = function(error, callback) { */ SerialPort.prototype.open = function(callback) { if (this.isOpen) { - return this._error(new Error('Port is already open'), callback); + return this._asyncError(new Error('Port is already open'), callback); } if (this.opening) { - return this._error(new Error('Port is opening'), callback); + return this._asyncError(new Error('Port is opening'), callback); } this.opening = true; @@ -254,7 +257,7 @@ SerialPort.prototype.open = function(callback) { SerialPort.prototype.update = function(options, callback) { if (!this.isOpen) { debug('update attempted, but port is not open'); - return this._error(new Error('Port is not open'), callback); + return this._asyncError(new Error('Port is not open'), callback); } var settings = assign({}, defaultSettings, options); @@ -366,7 +369,7 @@ SerialPort.prototype._disconnected = function(err) { SerialPort.prototype.close = function(callback) { if (!this.isOpen) { debug('close attempted, but port is not open'); - return this._error(new Error('Port is not open'), callback); + return this._asyncError(new Error('Port is not open'), callback); } this.closing = true; @@ -395,7 +398,7 @@ SerialPort.prototype.close = function(callback) { SerialPort.prototype.set = function(options, callback) { if (!this.isOpen) { debug('set attempted, but port is not open'); - return this._error(new Error('Port is not open'), callback); + return this._asyncError(new Error('Port is not open'), callback); } options = options || {}; @@ -404,16 +407,8 @@ SerialPort.prototype.set = function(options, callback) { options = {}; } - var settings = {}; - for (var i = SET_OPTIONS.length - 1; i >= 0; i--) { - var flag = SET_OPTIONS[i]; - if (options[flag] !== undefined) { - settings[flag] = options[flag]; - } else { - settings[flag] = defaultSetFlags[flag]; - } - } - + var settings = assign({}, defaultSetFlags, options); + debug('set', settings); this.binding.set(settings, function afterSet(err) { if (err) { debug('Binding #set had an error', err); @@ -431,9 +426,10 @@ SerialPort.prototype.set = function(options, callback) { 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); + return this._asyncError(new Error('Port is not open'), callback); } + debug('get'); this.binding.get(function afterGet(err, status) { if (err) { debug('Binding #get had an error', err); @@ -450,9 +446,10 @@ SerialPort.prototype.get = function(callback) { SerialPort.prototype.flush = function(callback) { if (!this.isOpen) { debug('flush attempted, but port is not open'); - return this._error(new Error('Port is not open'), callback); + return this._asyncError(new Error('Port is not open'), callback); } + debug('flush'); this.binding.flush(function afterFlush(err) { if (err) { debug('Binding #flush had an error', err); @@ -479,9 +476,9 @@ function writeAndDrain (data, callback) { SerialPort.prototype.drain = function(callback) { if (!this.isOpen) { debug('drain attempted, but port is not open'); - return this._error(new Error('Port is not open'), callback); + return this._asyncError(new Error('Port is not open'), callback); } - + debug('drain'); this.binding.drain(function afterDrain(err) { if (err) { debug('Binding #drain had an error', err);