Skip to content

Commit

Permalink
feat(parsers): Add cctalk parsers (#1342)
Browse files Browse the repository at this point in the history
* Added Working CCTalk Parser
* add basic tests
* compiled docs for cctalk parser
  • Loading branch information
frank-dspeed authored and reconbot committed Oct 1, 2017
1 parent a3b8d35 commit bcb492f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
31 changes: 31 additions & 0 deletions lib/parsers/cctalk.js
Original file line number Diff line number Diff line change
@@ -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();
}
};
10 changes: 10 additions & 0 deletions lib/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
41 changes: 41 additions & 0 deletions test/parser-cctalk.js
Original file line number Diff line number Diff line change
@@ -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]));

0 comments on commit bcb492f

Please sign in to comment.