mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-26 02:47:17 +00:00
byteDelimiter parser
emit row data base on a delimiter
This commit is contained in:
parent
cec7c2740b
commit
772492aa3d
2 changed files with 66 additions and 0 deletions
22
parsers.js
22
parsers.js
|
|
@ -38,5 +38,27 @@ module.exports = {
|
|||
emitter.emit('data', out);
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
//Emit a data event each time a byte sequence (delimiter is an array of byte) is found
|
||||
//Sample usage : byteDelimiter([10, 13])
|
||||
byteDelimiter: function (delimiter) {
|
||||
if( Object.prototype.toString.call( delimiter ) !== '[object Array]' ){
|
||||
delimiter = [ delimiter ];
|
||||
}
|
||||
var buf = [];
|
||||
var nextDelimIndex = 0;
|
||||
return function (emitter, buffer) {
|
||||
for (var i = 0; i < buffer.length; i++) {
|
||||
buf[buf.length] = buffer[i];
|
||||
if(buf[buf.length-1] == delimiter[nextDelimIndex])
|
||||
nextDelimIndex++;
|
||||
if(nextDelimIndex == delimiter.length) {
|
||||
emitter.emit('data', buf);
|
||||
buf = [];
|
||||
nextDelimIndex = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -44,4 +44,48 @@ describe('parsers', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('#rawdelimiter', function() {
|
||||
it('emits data events every time it meets 00x 00x', function() {
|
||||
var data = new Buffer('This could be\0\0binary data\0\0sent from a Moteino\0\0');
|
||||
var parser = parsers.byteDelimiter([0, 0]);
|
||||
var spy = sinon.spy();
|
||||
parser({ emit: spy }, data);
|
||||
expect(spy.callCount).to.equal(3);
|
||||
expect(spy.getCall(0).args[1]).to.have.length(15);
|
||||
expect(spy.getCall(0).args[1]).to.satisfy(function(d) { return d[d.length-1] == 0; });
|
||||
expect(spy.getCall(1).args[1]).to.have.length(13);
|
||||
expect(spy.getCall(1).args[1]).to.satisfy(function(d) { return d[d.length-1] == 0; });
|
||||
expect(spy.getCall(2).args[1]).to.have.length(21);
|
||||
expect(spy.getCall(2).args[1]).to.satisfy(function(d) { return d[d.length-1] == 0; });
|
||||
});
|
||||
it('accepts single byte delimiter', function() {
|
||||
var data = new Buffer('This could be\0binary data\0sent from a Moteino\0');
|
||||
var parser = parsers.byteDelimiter(0);
|
||||
var spy = sinon.spy();
|
||||
parser({ emit: spy }, data);
|
||||
expect(spy.callCount).to.equal(3);
|
||||
});
|
||||
it('Works when buffer starts with delimiter', function() {
|
||||
var data = new Buffer('\0Hello\0World\0');
|
||||
var parser = parsers.byteDelimiter(0);
|
||||
var spy = sinon.spy();
|
||||
parser({ emit: spy }, data);
|
||||
expect(spy.callCount).to.equal(3);
|
||||
});
|
||||
it('continues looking for delimiters in the next buffers', function() {
|
||||
var data1 = new Buffer('This could be\0\0binary ');
|
||||
var data2 = new Buffer('data\0\0sent from a Moteino\0\0');
|
||||
var parser = parsers.byteDelimiter([0,0]);
|
||||
var spy = sinon.spy();
|
||||
parser({ emit: spy }, data1);
|
||||
parser({ emit: spy }, data2);
|
||||
expect(spy.callCount).to.equal(3);
|
||||
expect(spy.getCall(0).args[1]).to.have.length(15);
|
||||
expect(spy.getCall(0).args[1]).to.satisfy(function(d) { return d[d.length-1] == 0; });
|
||||
expect(spy.getCall(1).args[1]).to.have.length(13);
|
||||
expect(spy.getCall(1).args[1]).to.satisfy(function(d) { return d[d.length-1] == 0; });
|
||||
expect(spy.getCall(2).args[1]).to.have.length(21);
|
||||
expect(spy.getCall(2).args[1]).to.satisfy(function(d) { return d[d.length-1] == 0; });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue