mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-26 02:47:17 +00:00
commit
651cc73698
6 changed files with 211 additions and 160 deletions
|
|
@ -44,7 +44,8 @@
|
|||
"grunt": "*",
|
||||
"grunt-cli": "*",
|
||||
"grunt-mocha-test": "*",
|
||||
"grunt-contrib-jshint": "*"
|
||||
"grunt-contrib-jshint": "*",
|
||||
"rewire": "~2.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "0.10.x"
|
||||
|
|
@ -56,4 +57,4 @@
|
|||
"scripts": {
|
||||
"test": "grunt --verbose"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,8 +10,6 @@ var fs = require('fs');
|
|||
var stream = require('stream');
|
||||
var path = require('path');
|
||||
var async = require('async');
|
||||
var child_process = require('child_process');
|
||||
|
||||
|
||||
function SerialPortFactory() {
|
||||
|
||||
|
|
@ -201,6 +199,7 @@ function SerialPortFactory() {
|
|||
|
||||
util.inherits(SerialPort, stream.Stream);
|
||||
|
||||
|
||||
SerialPort.prototype.open = function (callback) {
|
||||
var self = this;
|
||||
this.paused = true;
|
||||
|
|
@ -334,7 +333,7 @@ function SerialPortFactory() {
|
|||
// 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, 0, function(err, bytesRead){
|
||||
var readPool = self.pool;
|
||||
var bytesRequested = toRead;
|
||||
afterRead(err, bytesRead, readPool, bytesRequested);}
|
||||
|
|
|
|||
60
test/read.js
60
test/read.js
|
|
@ -1,60 +0,0 @@
|
|||
"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;
|
||||
|
||||
describe('SerialPort', function () {
|
||||
|
||||
var port;
|
||||
|
||||
describe('opening a port and writing some bytes', function () {
|
||||
|
||||
it("it should emit the expected answer bytes", function (done) {
|
||||
port = new SerialPort('/dev/fun', {}, false);
|
||||
port.open(function() {
|
||||
|
||||
var data = new Buffer([1, 2, 3, 4]);
|
||||
|
||||
port.once('data', function(res) {
|
||||
expect(res).to.eql(data);
|
||||
done();
|
||||
});
|
||||
|
||||
port.write(data, function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('closing and reopening the previously used port and writing some bytes', function () {
|
||||
|
||||
before(function(done) {
|
||||
port.close(done);
|
||||
});
|
||||
|
||||
it("it should emit the expected answer bytes again", function (done) {
|
||||
port.open(function() {
|
||||
|
||||
var data = new Buffer([5, 6, 7, 8]);
|
||||
|
||||
port.once('data', function(res) {
|
||||
expect(res).to.eql(data);
|
||||
done();
|
||||
});
|
||||
|
||||
port.write(data, function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
|
@ -1,49 +1,82 @@
|
|||
"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;
|
||||
|
||||
// Create a port for fun and profit
|
||||
hardware.createPort('/dev/exists');
|
||||
|
||||
describe('SerialPort', function () {
|
||||
var sandbox;
|
||||
|
||||
beforeEach(function () {
|
||||
sandbox = sinon.sandbox.create();
|
||||
});
|
||||
|
||||
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;
|
||||
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) {
|
||||
chai.assert.isDefined(err, 'err is not defined');
|
||||
done();
|
||||
};
|
||||
|
||||
var port = new SerialPort('johnJacobJingleheimerSchmidt', { databits : 19 }, false, errorCallback);
|
||||
var port = new SerialPort('/dev/exists', { databits : 19 }, false, errorCallback);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('reading data', function () {
|
||||
|
||||
it('emits data events by default', function (done) {
|
||||
hardware.createPort('/dev/exists'); // clears out any previously written data
|
||||
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) {
|
||||
hardware.createPort('/dev/exists'); // clears out any previously written data
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -51,31 +84,39 @@ describe('SerialPort', function () {
|
|||
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) {
|
||||
expect(err).to.be.ok;
|
||||
done();
|
||||
});
|
||||
|
||||
stubOpen.restore();
|
||||
});
|
||||
|
||||
it("emits data after being reopened", function (done) {
|
||||
hardware.createPort('/dev/fun');
|
||||
|
||||
var data = new Buffer("Howdy!");
|
||||
var port = new SerialPort('/dev/fun', function () {
|
||||
port.close();
|
||||
port.open(function () {
|
||||
port.once('data', function (res) {
|
||||
expect(res).to.eql(data);
|
||||
done();
|
||||
});
|
||||
hardware.emitData('/dev/fun', data);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
|
|
|||
137
test_mocks/linux-hardware.js
Normal file
137
test_mocks/linux-hardware.js
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
// This takes a serial-port factory and mocks the shit out of it in complete isolation per require of this file
|
||||
|
||||
"use strict";
|
||||
var rewire = require('rewire');
|
||||
process.platform = 'darwin';
|
||||
var serialPort = rewire('../serialport');
|
||||
|
||||
var mockSerialportPoller = function (hardware) {
|
||||
var Poller = function (path, cb) {
|
||||
this.port = hardware.ports[path];
|
||||
if (!this.port) {
|
||||
throw new Error(path + " does not exist - please call hardware.createPort(path) first");
|
||||
}
|
||||
this.port.poller = this;
|
||||
this.polling = null;
|
||||
this.cb = cb;
|
||||
};
|
||||
Poller.prototype.start = function () {
|
||||
this.polling = true;
|
||||
};
|
||||
Poller.prototype.close = function () {
|
||||
this.polling = false;
|
||||
};
|
||||
Poller.prototype.detectRead = function () {
|
||||
this.cb();
|
||||
};
|
||||
return Poller;
|
||||
};
|
||||
|
||||
var Hardware = function () {
|
||||
this.ports = {};
|
||||
this.mockBinding = {
|
||||
list: this.list.bind(this),
|
||||
open: this.open.bind(this),
|
||||
write: this.write.bind(this),
|
||||
close: this.close.bind(this),
|
||||
flush: this.flush.bind(this),
|
||||
SerialportPoller: mockSerialportPoller(this)
|
||||
};
|
||||
};
|
||||
|
||||
Hardware.prototype.createPort = function (path) {
|
||||
this.ports[path] = {
|
||||
data: new Buffer(0),
|
||||
lastWrite: null,
|
||||
open: false,
|
||||
poller: null,
|
||||
info: {
|
||||
comName: path,
|
||||
manufacturer: '',
|
||||
serialNumber: '',
|
||||
pnpId: '',
|
||||
locationId: '',
|
||||
vendorId: '',
|
||||
productId: ''
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Hardware.prototype.emitData = function (path, data) {
|
||||
var port = this.ports[path];
|
||||
if(!port) {
|
||||
throw new Error(path + " does not exist - please call hardware.createPort(path) first");
|
||||
}
|
||||
port.data = Buffer.concat([port.data, data]);
|
||||
port.poller && port.poller.detectRead();
|
||||
};
|
||||
|
||||
Hardware.prototype.list = function (cb) {
|
||||
var ports = this.ports;
|
||||
var info = Object.keys(ports).map(function (path) {
|
||||
return ports[path].info;
|
||||
});
|
||||
cb && cb(info);
|
||||
};
|
||||
|
||||
Hardware.prototype.open = function (path, opt, cb) {
|
||||
var port = this.ports[path];
|
||||
if (!port) {
|
||||
return cb(new Error(path + " does not exist - please call hardware.createPort(path) first"));
|
||||
}
|
||||
port.open = true;
|
||||
port.openOpt = opt;
|
||||
cb && cb(null, path); // using path as the fd for convience
|
||||
};
|
||||
|
||||
Hardware.prototype.write = function (path, buffer, cb) {
|
||||
var port = this.ports[path];
|
||||
if (!port) {
|
||||
return cb(new Error(path + " does not exist - please call hardware.createPort(path) first"));
|
||||
}
|
||||
port.lastWrite = new Buffer(buffer); //copy
|
||||
cb && cb(null, buffer.length);
|
||||
};
|
||||
|
||||
Hardware.prototype.close = function (path, cb) {
|
||||
var port = this.ports[path];
|
||||
if (!port) {
|
||||
return cb(new Error(path + " does not exist - please call hardware.createPort(path) first"));
|
||||
}
|
||||
port.open = false;
|
||||
cb && cb(null);
|
||||
};
|
||||
|
||||
Hardware.prototype.flush = function (path, cb) {
|
||||
var port = this.ports[path];
|
||||
if (!port) {
|
||||
return cb(new Error(path + " does not exist - please call hardware.createPort(path) first"));
|
||||
}
|
||||
cb && cb(null, undefined);
|
||||
};
|
||||
|
||||
var hardware = new Hardware();
|
||||
serialPort.hardware = hardware;
|
||||
serialPort.SerialPortBinding = hardware.mockBinding;
|
||||
|
||||
serialPort.__set__('fs', {
|
||||
read: function (path, buffer, offset, length, position, cb) {
|
||||
var port = hardware.ports[path];
|
||||
if (!port) {
|
||||
return cb(new Error(path + " does not exist - please call hardware.createPort(path) first"));
|
||||
}
|
||||
if (port.data.length === 0) {
|
||||
return cb(null, 0, buffer);
|
||||
}
|
||||
if ((offset + length) > buffer.length) {
|
||||
throw new Error("Length extends beyond buffer");
|
||||
}
|
||||
var read = port.data.slice(0, length);
|
||||
port.data = port.data.slice(length);
|
||||
read.copy(buffer, offset);
|
||||
cb(null, read.length, buffer);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
module.exports = serialPort;
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
// This exports a SerialPortBinding like object that has functions
|
||||
// that will emulate a happy object. These functions can be spied on
|
||||
// mocked or otherwise abused for testing.
|
||||
|
||||
"use strict";
|
||||
|
||||
var serialPortBinding;
|
||||
module.exports = serialPortBinding = {
|
||||
open: function (path, opt, cb) {
|
||||
this.path = path;
|
||||
this.options = opt;
|
||||
this.readable = true;
|
||||
cb(null, 'fakeFileDescriptor');
|
||||
},
|
||||
write: function (fd, buffer, cb) {
|
||||
this.lastWrite = buffer;
|
||||
cb(null, buffer.length);
|
||||
},
|
||||
_read: function() {
|
||||
|
||||
var echo = this.lastWrite;
|
||||
|
||||
if (!this.readable || this.paused || this.reading) return;
|
||||
|
||||
this.reading = true;
|
||||
|
||||
this.reading = false;
|
||||
|
||||
if (echo && echo.length > 0) {
|
||||
this.lastWrite = null;
|
||||
this.options.dataCallback(echo);
|
||||
}
|
||||
|
||||
// do not emit events anymore after we declared the stream unreadable
|
||||
if (!this.readable) return;
|
||||
|
||||
var self = this;
|
||||
setTimeout(function() {
|
||||
self._read();
|
||||
}, 20);
|
||||
},
|
||||
close: function (fd, cb) {
|
||||
cb(null);
|
||||
},
|
||||
list: function (cb) {
|
||||
var fakeSerialPort = {
|
||||
comName: '/dev/really-cool-serialport',
|
||||
manufacturer: '',
|
||||
serialNumber: '',
|
||||
pnpId: '',
|
||||
locationId: '',
|
||||
vendorId: '',
|
||||
productId: ''
|
||||
};
|
||||
cb([fakeSerialPort]);
|
||||
},
|
||||
flush: function (fd, cb) {
|
||||
cb(null, undefined);
|
||||
},
|
||||
SerialportPoller: function (fd, cb) {
|
||||
serialPortBinding.currentSerialPoller = this;
|
||||
this.start = function () { serialPortBinding._read(); };
|
||||
this.close = function () { };
|
||||
this.cb = cb;
|
||||
},
|
||||
path: ''
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue