Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

serialport

Package Overview
Dependencies
Maintainers
1
Versions
175
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serialport - npm Package Compare versions

Comparing version 1.2.5 to 1.3.0

examples/drain.js

4

changelog.md

@@ -0,1 +1,5 @@

Version 1.3.0
-------------
- Merged NAN integration for Node 0.8->0.11+ compatibility (#270)
Version 1.2.5

@@ -2,0 +6,0 @@ -------------

14

package.json
{
"name": "serialport",
"version": "1.2.5",
"version": "1.3.0",
"description": "Welcome your robotic javascript overlords. Better yet, program them!",

@@ -31,2 +31,6 @@ "author": {

"email": "giseburt@gmail.com"
},
{
"name": "Francis Gulotta",
"email": "wizard@roborooter.com"
}

@@ -38,3 +42,4 @@ ],

"sf": "0.1.6",
"optimist": "~0.3.4"
"optimist": "~0.3.4",
"nan": "~0.7.0"
},

@@ -49,3 +54,4 @@ "devDependencies": {

"grunt-mocha-test": "*",
"grunt-contrib-jshint": "*"
"grunt-contrib-jshint": "*",
"sandboxed-module": "~0.3.0"
},

@@ -62,2 +68,2 @@ "engines": {

}
}
}

@@ -17,3 +17,3 @@ ```

Version: 1.2.5 - Released November 11, 2013
Version: 1.3.0 - Released January 15, 2014

@@ -58,16 +58,21 @@ *****

* Install Windows 7 or Windows 8.
* Install [Visual Studio Express 2013 Preview for Windows Desktop](http://www.microsoft.com/visualstudio/eng/2013-downloads#d-2013-express) or [Visual Studio Express 2012 for Windows Desktop](http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-for-windows-desktop).
* Windows 7 or Windows 8.1 are supported.
* Install [Visual Studio Express 2013 for Windows Desktop](http://www.microsoft.com/visualstudio/eng/2013-downloads#d-2013-express).
* If you are hacking on an Arduino, be sure to install [the drivers](http://arduino.cc/en/Guide/windows#toc4).
* Install [node.js 0.10.x](http://nodejs.org/) matching the bitness (32 or 64) of your operating system.
* Install [Python 2.7.5](http://www.python.org/download/releases/2.7.5/) matching the bitness of your operating system. For any questions, please refer to their [FAQ](http://docs.python.org/2/faq/windows.html). Default settings are perfect.
* Open the 'Visual Studio Command Prompt' and add Python to the path. When installing serialport, you need to tell the build system (known as node-gyp) that you are using a newer compiler by using:
* Install [Python 2.7.6](http://www.python.org/download/releases/2.7.6/) matching the bitness of your operating system. For any questions, please refer to their [FAQ](http://docs.python.org/2/faq/windows.html). Default settings are perfect.
* Open the 'Visual Studio Command Prompt' and add Python to the path.
Visual Studio 2013 is not yet supported by node 0.10.x but the latest version of node-gyp on npm does support it. Use the
below workaround until node 0.10.x is updated.
```Batchfile
set path=%path%;C:\Python27
npm install serialport --msvs_version=2012
npm install node-gyp -g
git clone https://github.com/voodootikigod/node-serialport.git
cd node-serialport
node-gyp configure
node-gyp build
```
This switch works for both Visual Studio Express 2012 and 2013.
### Mac OS X:

@@ -187,3 +192,4 @@

```js
serialport.list(function (err, ports) {
var serialPort = require("serialport");
serialPort.list(function (err, ports) {
ports.forEach(function(port) {

@@ -238,1 +244,115 @@ console.log(port.comName);

Enjoy and do cool things with this code.
Reference Guide
---------------
## Methods
### SerialPort (path, options, openImmediately, callback)
Create a new serial port on `path`.
**_path_**
The system path of the serial port to open. For example, `/dev/tty` on Mac/Linux or `COM1` on Windows.
**_options (optional)_**
Port configuration options.
* `baudRate`
* `dataBits`
* `stopBits`
* `parity`
* `rtscts`
* `xon`
* `xoff`
* `xany`
* `flowControl`
* `bufferSize`
* `parser`
* `encoding`
* `dataCallback`
* `disconnectedCallback`
**_openImmediately (optional)_**
Attempts to open a connection to the serial port on `process.nextTick`. The default is `true`. Set to `false` to manually call `open()` at a later time.
**_callback (optional)_**
Called when a connection has been opened. The callback should be a function that looks like: `function (error) { ... }`
### .open (callback)
Opens a connection to the given serial port.
**_callback (optional)_**
Called when a connection has been opened. The callback should be a function that looks like: `function (error) { ... }`
### .write (buffer, callback)
Writes data to the given serial port.
**_buffer_**
The `buffer` parameter accepts a [`Buffer` ](http://nodejs.org/api/buffer.html) object, or a type that is accepted by the `Buffer` constructor (ex. an array of bytes or a string).
**_callback (optional)_**
Called once the write operation returns. The callback should be a function that looks like: `function (error) { ... }` _Note: The write operation is non-blocking. When it returns, data may still have not actually been written to the serial port. See `drain()`._
### .pause ()
Pauses an open connection.
### .resume ()
Resumes a paused connection.
### .flush (callback)
Flushes data received but not read. See [`tcflush()`](http://linux.die.net/man/3/tcflush) for Mac/Linux and [`FlushFileBuffers`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364439) for Windows.
**_callback (optional)_**
Called once the flush operation returns. The callback should be a function that looks like: `function (error) { ... }`
### .drain (callback)
Waits until all output data has been transmitted to the serial port. See [`tcdrain()`](http://linux.die.net/man/3/tcdrain) for more information.
**_callback (optional)_**
Called once the drain operation returns. The callback should be a function that looks like: `function (error) { ... }`
**Example**
Writes `data` and waits until it has finish transmitting to the target serial port before calling the callback.
```
function writeAndDrain (data, callback) {
sp.write(data, function () {
sp.drain(callback);
});
}
```
### .close (callback)
Closes an open connection.
**_callback (optional)_**
Called once a connection is closed. Closing a connection will also remove all event listeners. The callback should be a function that looks like: `function (error) { ... }`
## Events
### .on('open', callback)
### .on('data', callback)
### .on('close', callback)
### .on('error', callback)

@@ -7,2 +7,3 @@ /*jslint node: true */

var SerialPortBinding = require("bindings")("serialport.node");
var parsers = require('./parsers');
var EventEmitter = require('events').EventEmitter;

@@ -14,5 +15,3 @@ var util = require('util');

var async = require('async');
var child_process = require('child_process');
function SerialPortFactory() {

@@ -36,26 +35,2 @@

var parsers = {
raw: function (emitter, buffer) {
emitter.emit("data", buffer);
},
//encoding: ascii utf8 utf16le ucs2 base64 binary hex
//More: http://nodejs.org/api/buffer.html#buffer_buffer
readline: function (delimiter, encoding) {
if (typeof delimiter === "undefined" || delimiter === null) { delimiter = "\r"; }
if (typeof encoding === "undefined" || encoding === null) { encoding = "utf8"; }
// Delimiter buffer saved in closure
var data = "";
return function (emitter, buffer) {
// Collect data
data += buffer.toString(encoding);
// Split collected data by delimiter
var parts = data.split(delimiter);
data = parts.pop();
parts.forEach(function (part, i, array) {
emitter.emit('data', part);
});
};
}
};
// The default options, can be overwritten in the 'SerialPort' constructor

@@ -75,3 +50,3 @@ var _options = {

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

@@ -86,3 +61,4 @@ var self = this;

options = options || {};
options = (typeof options !== 'function') && options || {};
openImmediately = (openImmediately === undefined || openImmediately === null) ? true : openImmediately;

@@ -101,9 +77,5 @@

options.baudRate = options.baudRate || options.baudrate || _options.baudrate;
// Removing check for valid BaudRates due to ticket: #140
// if (BAUDRATES.indexOf(options.baudrate) == -1) {
// throw new Error('Invalid "baudrate": ' + options.baudrate);
// }
options.dataBits = options.dataBits || options.databits || _options.databits;
if (DATABITS.indexOf(options.dataBits) == -1) {
if (DATABITS.indexOf(options.dataBits) === -1) {
err = new Error('Invalid "databits": ' + options.dataBits);

@@ -115,3 +87,3 @@ callback(err);

options.stopBits = options.stopBits || options.stopbits || _options.stopbits;
if (STOPBITS.indexOf(options.stopBits) == -1) {
if (STOPBITS.indexOf(options.stopBits) === -1) {
err = new Error('Invalid "stopbits": ' + options.stopbits);

@@ -123,3 +95,3 @@ callback(err);

options.parity = options.parity || _options.parity;
if (PARITY.indexOf(options.parity) == -1) {
if (PARITY.indexOf(options.parity) === -1) {
err = new Error('Invalid "parity": ' + options.parity);

@@ -144,3 +116,3 @@ callback(err);

if (typeof fc == 'boolean') {
if (typeof fc === 'boolean') {
options.rtscts = true;

@@ -152,3 +124,3 @@ } else {

if (idx < 0) {
var err = new Error('Invalid "flowControl": ' + fcup + ". Valid options: "+FLOWCONTROLS.join(", "));
var err = new Error('Invalid "flowControl": ' + fcup + ". Valid options: " + FLOWCONTROLS.join(", "));
callback(err);

@@ -177,6 +149,2 @@ return;

// options.dataReadyCallback = function () {
// self.readStream._read(4024);
// };
options.disconnectedCallback = options.disconnectedCallback || function () {

@@ -188,8 +156,5 @@ if (self.closing) {

callback(err);
// self.close();
};
if (process.platform == 'win32') {
path = '\\\\.\\' + path;
} else {
if (process.platform !== 'win32') {
// All other platforms:

@@ -202,4 +167,5 @@ this.fd = null;

if (options.encoding)
if (options.encoding) {
this.setEncoding(this.encoding);
}
}

@@ -219,2 +185,3 @@

SerialPort.prototype.open = function (callback) {

@@ -236,15 +203,4 @@ var self = this;

if (process.platform !== 'win32') {
// self.readStream = new SerialStream(self.fd, { bufferSize: self.options.bufferSize });
// 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 () {
// console.log(">>END");
// self.emit('end');
// });
// self.readStream.resume();
self.paused = false;
self.serialPoller = new factory.SerialPortBinding.SerialportPoller(self.fd, function() {self._read();});
self.serialPoller = new factory.SerialPortBinding.SerialportPoller(self.fd, function () { self._read(); });
self.serialPoller.start();

@@ -285,7 +241,9 @@ }

if (process.platform !== 'win32') {
SerialPort.prototype._read = function() {
SerialPort.prototype._read = function () {
var self = this;
// console.log(">>READ");
if (!self.readable || self.paused || self.reading) return;
if (!self.readable || self.paused || self.reading) {
return;
}

@@ -314,5 +272,6 @@ self.reading = true;

if (err) {
if (err.code && err.code == 'EAGAIN') {
if (self.fd >= 0)
if (err.code && err.code === 'EAGAIN') {
if (self.fd >= 0) {
self.serialPoller.start();
}
} else {

@@ -329,7 +288,6 @@ self.fd = null;

// console.log(">>ACTUALLY READ: ", bytesRead);
if (bytesRead === 0) {
if (self.fd >= 0)
if (self.fd >= 0) {
self.serialPoller.start();
}
} else {

@@ -347,4 +305,5 @@ var b = self.pool.slice(start, start + bytesRead);

// do not emit events anymore after we declared the stream unreadable
if (!self.readable) return;
if (!self.readable) {
return;
}
self._read();

@@ -354,11 +313,7 @@ }

// debug this device's pool offset
// console.log(self.path + ' >> POOL OFFSET: ', self.pool.used);
// console.log(">>REQUEST READ: ", toRead);
fs.read(self.fd, self.pool, self.pool.used, toRead, self.pos, function(err, bytesRead){
fs.read(self.fd, self.pool, self.pool.used, toRead, null, function (err, bytesRead) {
var readPool = self.pool;
var bytesRequested = toRead;
afterRead(err, bytesRead, readPool, bytesRequested);}
);
afterRead(err, bytesRead, readPool, bytesRequested);
});

@@ -369,13 +324,7 @@ self.pool.used += toRead;

SerialPort.prototype._emitData = function(d) {
var self = this;
// if (self._decoder) {
// var string = self._decoder.write(d);
// if (string.length) self.options.dataCallback(string);
// } else {
self.options.dataCallback(d);
// }
SerialPort.prototype._emitData = function (data) {
this.options.dataCallback(data);
};
SerialPort.prototype.pause = function() {
SerialPort.prototype.pause = function () {
var self = this;

@@ -385,4 +334,3 @@ self.paused = true;

SerialPort.prototype.resume = function() {
SerialPort.prototype.resume = function () {
var self = this;

@@ -398,4 +346,5 @@ self.paused = false;

// No longer open?
if (null === self.fd)
if (null === self.fd) {
return;
}

@@ -427,9 +376,2 @@ self._read();

try {
if (self.readStream) {
// Make sure we clear the readStream's fd, or it'll try to close() it.
// We already close()d it.
self.readStream.fd = null;
self.readStream.destroy();
}
factory.SerialPortBinding.close(fd, function (err) {

@@ -470,3 +412,3 @@

function listUnix (callback) {
function listUnix(callback) {
fs.readdir("/dev/serial/by-id", function (err, files) {

@@ -507,33 +449,2 @@ if (err) {

});
// Suspect code per ticket: #104 removed for deeper inspection.
// fs.readdir("/dev/serial/by-path", function(err_path, paths) {
// if (err_path) {
// if (err.errno === 34) return callback(null, []);
// return console.log(err);
// }
// var dirName, items;
// //check if multiple devices of the same id are connected
// if (files.length !== paths.length) {
// dirName = "/dev/serial/by-path";
// items = paths;
// } else {
// dirName = "/dev/serial/by-id";
// items = files;
// }
// async.map(items, function (file, callback) {
// var fileName = path.join(dirName, file);
// fs.readlink(fileName, function (err, link) {
// if (err) {
// return callback(err);
// }
// link = path.resolve(dirName, link);
// callback(null, {
// comName: link,
// manufacturer: undefined,
// pnpId: file
// });
// });
// }, callback);
}, callback);

@@ -564,2 +475,4 @@ });

}
} else {
callback(err, result);
}

@@ -569,2 +482,29 @@ });

SerialPort.prototype.drain = function (callback) {
var self = this;
var fd = this.fd;
if (!fd) {
var err = new Error("Serialport not open.");
if (callback) {
callback(err);
} else {
self.emit('error', err);
}
return;
}
factory.SerialPortBinding.drain(fd, function (err, result) {
if (err) {
if (callback) {
callback(err, result);
} else {
self.emit('error', err);
}
} else {
callback(err, result);
}
});
};
factory.SerialPort = SerialPort;

@@ -571,0 +511,0 @@ factory.parsers = parsers;

"use strict";
var serialPort = require('../serialport');
var chai = require('chai');

@@ -8,3 +7,3 @@ var sinonChai = require("sinon-chai");

var parsers = serialPort.parsers;
var parsers = require('../parsers');

@@ -11,0 +10,0 @@ describe("parsers", function () {

"use strict";
var serialPort = require('../serialport');
var sinon = require("sinon");
var chai = require('chai');
var mockBinding = require('../test_mocks/serial-port-binding');
var expect = chai.expect;
var SerialPort = serialPort.SerialPort;
serialPort.SerialPortBinding = mockBinding;
var MockedSerialPort = require('../test_mocks/linux-hardware');
var SerialPort = MockedSerialPort.SerialPort;
var hardware = MockedSerialPort.hardware;
describe('SerialPort', function () {
var sandbox;
beforeEach(function () {
sandbox = sinon.sandbox.create();
// Create a port for fun and profit
hardware.reset();
hardware.createPort('/dev/exists');
});
afterEach(function () {
sandbox.restore();
});
describe('Constructor', function () {
it("opens the port immediately", function (done) {
var port = new SerialPort('/dev/fun', function (err) {
var port = new SerialPort('/dev/exists', function (err) {
expect(err).to.not.be.ok;

@@ -22,21 +34,13 @@ done();

it('emits an error on the factory when erroring without a callback', function (done) {
// setup the bindings to report an error during open
var stubOpen = sinon.stub(mockBinding, 'open', function (path, opt, cb) {
cb('fakeErrr!');
});
// finish the test on error
serialPort.once('error', function (err) {
MockedSerialPort.once('error', function (err) {
chai.assert.isDefined(err, "didn't get an error");
stubOpen.restore();
done();
});
var port = new SerialPort('johnJacobJingleheimerSchmidt');
var port = new SerialPort('/dev/johnJacobJingleheimerSchmidt');
});
it('errors with invalid databits', function (done) {
var errorCallback = function (err) {

@@ -47,27 +51,56 @@ chai.assert.isDefined(err, 'err is not defined');

var port = new SerialPort('johnJacobJingleheimerSchmidt', { databits : 19 }, false, errorCallback);
var port = new SerialPort('/dev/exists', { databits : 19 }, false, errorCallback);
});
it('allows optional options', function (done) {
var cb = function () {};
var port = new SerialPort('/dev/exists', cb);
expect(typeof port.options).to.eq('object');
done();
});
});
describe('reading data', function () {
it('emits data events by default', function (done) {
var testData = new Buffer("I am a really short string");
var port = new SerialPort('/dev/exists', function () {
port.once('data', function(recvData) {
expect(recvData).to.eql(testData);
done();
});
hardware.emitData('/dev/exists', testData);
});
});
it('calls the dataCallback if set', function (done) {
var testData = new Buffer("I am a really short string");
var opt = {
dataCallback: function (recvData) {
expect(recvData).to.eql(testData);
done();
}
};
var port = new SerialPort('/dev/exists', opt, function () {
hardware.emitData('/dev/exists', testData);
});
});
});
describe('#open', function () {
it('passes the port to the bindings', function (done) {
var openSpy = sinon.spy(mockBinding, 'open');
var port = new SerialPort('/dev/happyPort', {}, false);
var openSpy = sandbox.spy(MockedSerialPort.SerialPortBinding, 'open');
var port = new SerialPort('/dev/exists', {}, false);
port.open(function (err) {
expect(err).to.not.be.ok;
expect(openSpy.calledWith('/dev/happyPort'));
openSpy.restore();
expect(openSpy.calledWith('/dev/exists'));
done();
});
});
it('calls back an error when opening an invalid port', function (done) {
var stubOpen = sinon.stub(mockBinding, 'open', function (path, opt, cb) {
cb('fakeErrr!');
});
var port = new SerialPort('johnJacobJingleheimerSchmidt', {}, false);
var port = new SerialPort('/dev/unhappy', {}, false);
port.open(function (err) {

@@ -77,10 +110,58 @@ expect(err).to.be.ok;

});
});
stubOpen.restore();
it("emits data after being reopened", function (done) {
var data = new Buffer("Howdy!");
var port = new SerialPort('/dev/exists', function () {
port.close();
port.open(function () {
port.once('data', function (res) {
expect(res).to.eql(data);
done();
});
hardware.emitData('/dev/exists', data);
});
});
});
});
describe('close', function () {
it("fires a close event when it's closed", function (done) {
var port = new SerialPort('/dev/exists', function () {
var closeSpy = sandbox.spy();
port.on('close', closeSpy);
port.close();
expect(closeSpy.calledOnce);
done();
});
});
it("fires a close event after being reopened", function (done) {
var port = new SerialPort('/dev/exists', function () {
var closeSpy = sandbox.spy();
port.on('close', closeSpy);
port.close();
port.open();
port.close();
expect(closeSpy.calledTwice);
done();
});
});
});
describe('disconnect', function () {
it("fires a disconnect event", function (done) {
var port = new SerialPort('/dev/exists', {
disconnectedCallback: function (err) {
expect(err).to.not.be.ok;
done();
}
}, function () {
hardware.disconnect('/dev/exists');
});
});
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc