diff --git a/README.md b/README.md index 6666b3f..c339bb4 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ This library is admittedly a base level toolkit for building amazing things with * [Arduino Drinking Game Extravaganza](http://jsconf.eu/2011/arduino_drinking_game_extravaganza.html) - AKA "The Russian" a hexidecimal drinking game for geeks by Uxebu presented at JSConf EU 2011. * [Arduino controlling popcorn.js](https://gist.github.com/968773) - Controlling a popcorn.js video with an Arduino kit. * [Robotic JavaScript](http://jsconf.eu/2010/speaker/livingroombindmotion_function.html) - The first live presentation of the node-serialport code set as presented at JSConf EU 2010. +* [devicestack](https://github.com/adrai/devicestack) - This module helps you to represent a device and its protocol. For getting started with node-serialport, we recommend you begin with the following articles: @@ -114,6 +115,26 @@ serialPort.on("open", function () { }); +You can also call the open function, in this case instanciate the serialport with an additional flag. + +
+var SerialPort = require("serialport").SerialPort
+var serialPort = new SerialPort("/dev/tty-usbserial1", {
+  baudrate: 57600
+}, false); // this is the openImmediately flag [default is true]
+
+serialPort.open(function () {
+  console.log('open');
+  serialPort.on('data', function(data) {
+    console.log('data received: ' + data);
+  });  
+  serialPort.write("ls\n", function(err, results) {
+    console.log('err ' + err);
+    console.log('results ' + results);
+  });  
+});
+
+ List Ports ---------- diff --git a/serialport.js b/serialport.js index 577fc10..1311d6a 100644 --- a/serialport.js +++ b/serialport.js @@ -49,9 +49,12 @@ var _options = { buffersize: 255, parser: parsers.raw }; -function SerialPort (path, options) { +function SerialPort (path, options, openImmediately) { options = options || {}; options.__proto__ = _options; + openImmediately = (openImmediately === undefined || openImmediately === null) ? true : openImmediately; + + var self = this; if (BAUDRATES.indexOf(options.baudrate) == -1) { throw new Error('Invalid "baudrate": ' + options.baudrate); @@ -74,59 +77,69 @@ function SerialPort (path, options) { stream.Stream.call(this); - var self = this; - process.nextTick(function () { - options = options || {}; - options.baudRate = options.baudRate || options.baudrate || 9600; - options.dataBits = options.dataBits || options.databits || 8; - options.parity = options.parity || 'none'; - options.stopBits = options.stopBits || options.stopbits || 1; - options.bufferSize = options.bufferSize || options.buffersize || 100; - if (!('flowControl' in options)) { - options.flowControl = false; + options = options || {}; + options.baudRate = options.baudRate || options.baudrate || 9600; + options.dataBits = options.dataBits || options.databits || 8; + options.parity = options.parity || 'none'; + options.stopBits = options.stopBits || options.stopbits || 1; + options.bufferSize = options.bufferSize || options.buffersize || 100; + if (!('flowControl' in options)) { + options.flowControl = false; + } + options.dataCallback = function (data) { + options.parser(self, data); + }; + options.errorCallback = function (err) { + self.emit('error', err); + }; + options.disconnectedCallback = function () { + if (self.closing) { + return; } - options.dataCallback = function (data) { - options.parser(self, data); - }; - options.errorCallback = function (err) { - self.emit('error', err); - }; - options.disconnectedCallback = function () { - if (self.closing) { - return; - } - self.emit('error', new Error("Disconnected")); - self.close(); - }; + self.emit('error', new Error("Disconnected")); + self.close(); + }; - if (process.platform == 'win32') { - path = '\\\\.\\' + path; - } + if (process.platform == 'win32') { + path = '\\\\.\\' + path; + } - SerialPortBinding.open(path, options, function (err, fd) { - self.fd = fd; - if (err) { - return self.emit('error', err); - } - if (process.platform !== 'win32') { - self.readStream = fs.createReadStream(path, { bufferSize: options.bufferSize, fd: fd }); - self.readStream.on("data", options.dataCallback); - self.readStream.on("error", options.errorCallback); - self.readStream.on("close", function () { - self.close(); - }); - self.readStream.on("end", function () { - self.emit('end'); - }); - } + this.options = options; + this.path = path; - self.emit('open'); + if (openImmediately) { + process.nextTick(function () { + self.open(); }); - }); + } } util.inherits(SerialPort, stream.Stream); +SerialPort.prototype.open = function (callback) { + var self = this; + SerialPortBinding.open(this.path, this.options, function (err, fd) { + self.fd = fd; + if (err) { + return self.emit('error', err); + } + if (process.platform !== 'win32') { + self.readStream = fs.createReadStream(self.path, { bufferSize: self.options.bufferSize, fd: fd }); + self.readStream.on("data", self.options.dataCallback); + self.readStream.on("error", self.options.errorCallback); + self.readStream.on("close", function () { + self.close(); + }); + self.readStream.on("end", function () { + self.emit('end'); + }); + } + + self.emit('open'); + if (callback) { callback(err); } + }); +}; + SerialPort.prototype.write = function (buffer, callback) { var self = this; if (!this.fd) {