mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-25 18:37:19 +00:00
feat(parsers): Add cctalk parsers (#1342)
* Added Working CCTalk Parser * add basic tests * compiled docs for cctalk parser
This commit is contained in:
parent
a3b8d35597
commit
bcb492f386
4 changed files with 91 additions and 0 deletions
|
|
@ -788,6 +788,15 @@ const parser = port.pipe(new Regex({ regex: /[\r\n]+/ }));
|
|||
parser.on('data', console.log);
|
||||
```
|
||||
|
||||
To use the `CCTalk` parser you need to provide nothing. CCTalk Messages get emitted as buffer.
|
||||
```js
|
||||
const SerialPort = require('serialport');
|
||||
const CCTalk = SerialPort.parsers.CCTalk;
|
||||
const port = new SerialPort('/dev/ttyUSB0');
|
||||
const parser = port.pipe(new CCtalk());
|
||||
parser.on('data', console.log);
|
||||
```
|
||||
|
||||
* * *
|
||||
|
||||
<a name="module_serialport--SerialPort.list"></a>
|
||||
|
|
|
|||
31
lib/parsers/cctalk.js
Normal file
31
lib/parsers/cctalk.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
'use strict';
|
||||
const Transform = require('stream').Transform;
|
||||
const Buffer = require('safe-buffer').Buffer;
|
||||
|
||||
module.exports = class CCTalkParser extends Transform {
|
||||
constructor() {
|
||||
super();
|
||||
this.array = [];
|
||||
this.cursor = 0;
|
||||
}
|
||||
_transform(buffer, _, cb) {
|
||||
this.cursor += buffer.length;
|
||||
// TODO: Better Faster es7 no supported by node 4
|
||||
// ES7 allows directly push [...buffer]
|
||||
// this.array = this.array.concat(Array.from(buffer)); //Slower ?!?
|
||||
Array.from(buffer)
|
||||
.map((byte) => this.array.push(byte));
|
||||
while (this.cursor > 1 && this.cursor >= this.array[1] + 5) {
|
||||
// full frame accumulated
|
||||
// copy command from the array
|
||||
const FullMsgLength = this.array[1] + 5;
|
||||
|
||||
const frame = Buffer.from(this.array.slice(0, FullMsgLength));
|
||||
// Preserve Extra Data
|
||||
this.array = this.array.slice(frame.length, this.array.length);
|
||||
this.cursor -= FullMsgLength;
|
||||
this.push(frame);
|
||||
}
|
||||
cb();
|
||||
}
|
||||
};
|
||||
|
|
@ -71,11 +71,21 @@ const Regex = SerialPort.parsers.Regex;
|
|||
const port = new SerialPort('/dev/tty-usbserial1');
|
||||
const parser = port.pipe(new Regex({ regex: /[\r\n]+/ }));
|
||||
parser.on('data', console.log);
|
||||
```
|
||||
|
||||
To use the `CCTalk` parser you need to provide nothing. CCTalk Messages get emitted as buffer.
|
||||
```js
|
||||
const SerialPort = require('serialport');
|
||||
const CCTalk = SerialPort.parsers.CCTalk;
|
||||
const port = new SerialPort('/dev/ttyUSB0');
|
||||
const parser = port.pipe(new CCtalk());
|
||||
parser.on('data', console.log);
|
||||
```
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
ByteLength: require('./byte-length'),
|
||||
CCTalk: require('./cctalk'),
|
||||
Delimiter: require('./delimiter'),
|
||||
Readline: require('./readline'),
|
||||
Ready: require('./ready'),
|
||||
|
|
|
|||
41
test/parser-cctalk.js
Normal file
41
test/parser-cctalk.js
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
'use strict';
|
||||
/* eslint-disable no-new */
|
||||
|
||||
const Buffer = require('safe-buffer').Buffer;
|
||||
const sinon = require('sinon');
|
||||
const CCTalkParser = require('../lib/parsers/cctalk');
|
||||
|
||||
describe('CCTalkParser', () => {
|
||||
it('emits data for a default length message', () => {
|
||||
const data = Buffer.from([2, 0, 1, 254, 217]);
|
||||
const spy = sinon.spy();
|
||||
const parser = new CCTalkParser();
|
||||
parser.on('data', spy);
|
||||
parser.write(data);
|
||||
assert.equal(spy.callCount, 1);
|
||||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 0, 1, 254, 217]));
|
||||
});
|
||||
|
||||
it('emits data for a 7 byte length message', () => {
|
||||
const parser = new CCTalkParser();
|
||||
const spy = sinon.spy();
|
||||
parser.on('data', spy);
|
||||
parser.write(Buffer.from([2, 2, 1, 254, 1, 1, 217]));
|
||||
assert.equal(spy.callCount, 1);
|
||||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 2, 1, 254, 1, 1, 217]));
|
||||
});
|
||||
|
||||
it('emits 2 times data first length 7 secund length 5', () => {
|
||||
const parser = new CCTalkParser();
|
||||
const spy = sinon.spy();
|
||||
parser.on('data', spy);
|
||||
parser.write(Buffer.from([2, 2, 1]));
|
||||
parser.write(Buffer.from([254, 1, 1]));
|
||||
parser.write(Buffer.from([217, 2]));
|
||||
parser.write(Buffer.from([0, 1, 254, 217]));
|
||||
assert.equal(spy.callCount, 2);
|
||||
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 2, 1, 254, 1, 1, 217]));
|
||||
assert.deepEqual(spy.getCall(1).args[0], Buffer.from([2, 0, 1, 254, 217]));
|
||||
});
|
||||
});
|
||||
// TODO: parser.write(Buffer.from([2, 2, 1, 254, 1, 1, 217, 2, 0, 1, 254, 217, 2, 2, 1, 251, 1, 1, 217, 2, 2, 1, 252, 1, 1, 217, 2, 0, 1, 253, 217]));
|
||||
Loading…
Add table
Reference in a new issue