encapsulation of open function

This commit is contained in:
Adriano Raiano 2013-03-14 16:43:53 +01:00
parent 07ca55b210
commit 657c4e3f3f
2 changed files with 79 additions and 45 deletions

View file

@ -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 () {
});
</pre>
You can also call the open function, in this case instanciate the serialport with an additional flag.
<pre>
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);
});
});
</pre>
List Ports
----------

View file

@ -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) {