Skip to content

Commit bcb492f

Browse files
frank-dspeedreconbot
authored andcommitted
feat(parsers): Add cctalk parsers (#1342)
* Added Working CCTalk Parser * add basic tests * compiled docs for cctalk parser
1 parent a3b8d35 commit bcb492f

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,15 @@ const parser = port.pipe(new Regex({ regex: /[\r\n]+/ }));
788788
parser.on('data', console.log);
789789
```
790790

791+
To use the `CCTalk` parser you need to provide nothing. CCTalk Messages get emitted as buffer.
792+
```js
793+
const SerialPort = require('serialport');
794+
const CCTalk = SerialPort.parsers.CCTalk;
795+
const port = new SerialPort('/dev/ttyUSB0');
796+
const parser = port.pipe(new CCtalk());
797+
parser.on('data', console.log);
798+
```
799+
791800
* * *
792801

793802
<a name="module_serialport--SerialPort.list"></a>

lib/parsers/cctalk.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const Transform = require('stream').Transform;
3+
const Buffer = require('safe-buffer').Buffer;
4+
5+
module.exports = class CCTalkParser extends Transform {
6+
constructor() {
7+
super();
8+
this.array = [];
9+
this.cursor = 0;
10+
}
11+
_transform(buffer, _, cb) {
12+
this.cursor += buffer.length;
13+
// TODO: Better Faster es7 no supported by node 4
14+
// ES7 allows directly push [...buffer]
15+
// this.array = this.array.concat(Array.from(buffer)); //Slower ?!?
16+
Array.from(buffer)
17+
.map((byte) => this.array.push(byte));
18+
while (this.cursor > 1 && this.cursor >= this.array[1] + 5) {
19+
// full frame accumulated
20+
// copy command from the array
21+
const FullMsgLength = this.array[1] + 5;
22+
23+
const frame = Buffer.from(this.array.slice(0, FullMsgLength));
24+
// Preserve Extra Data
25+
this.array = this.array.slice(frame.length, this.array.length);
26+
this.cursor -= FullMsgLength;
27+
this.push(frame);
28+
}
29+
cb();
30+
}
31+
};

lib/parsers/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,21 @@ const Regex = SerialPort.parsers.Regex;
7171
const port = new SerialPort('/dev/tty-usbserial1');
7272
const parser = port.pipe(new Regex({ regex: /[\r\n]+/ }));
7373
parser.on('data', console.log);
74+
```
75+
76+
To use the `CCTalk` parser you need to provide nothing. CCTalk Messages get emitted as buffer.
77+
```js
78+
const SerialPort = require('serialport');
79+
const CCTalk = SerialPort.parsers.CCTalk;
80+
const port = new SerialPort('/dev/ttyUSB0');
81+
const parser = port.pipe(new CCtalk());
82+
parser.on('data', console.log);
7483
```
7584
*/
7685

7786
module.exports = {
7887
ByteLength: require('./byte-length'),
88+
CCTalk: require('./cctalk'),
7989
Delimiter: require('./delimiter'),
8090
Readline: require('./readline'),
8191
Ready: require('./ready'),

test/parser-cctalk.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
/* eslint-disable no-new */
3+
4+
const Buffer = require('safe-buffer').Buffer;
5+
const sinon = require('sinon');
6+
const CCTalkParser = require('../lib/parsers/cctalk');
7+
8+
describe('CCTalkParser', () => {
9+
it('emits data for a default length message', () => {
10+
const data = Buffer.from([2, 0, 1, 254, 217]);
11+
const spy = sinon.spy();
12+
const parser = new CCTalkParser();
13+
parser.on('data', spy);
14+
parser.write(data);
15+
assert.equal(spy.callCount, 1);
16+
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 0, 1, 254, 217]));
17+
});
18+
19+
it('emits data for a 7 byte length message', () => {
20+
const parser = new CCTalkParser();
21+
const spy = sinon.spy();
22+
parser.on('data', spy);
23+
parser.write(Buffer.from([2, 2, 1, 254, 1, 1, 217]));
24+
assert.equal(spy.callCount, 1);
25+
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 2, 1, 254, 1, 1, 217]));
26+
});
27+
28+
it('emits 2 times data first length 7 secund length 5', () => {
29+
const parser = new CCTalkParser();
30+
const spy = sinon.spy();
31+
parser.on('data', spy);
32+
parser.write(Buffer.from([2, 2, 1]));
33+
parser.write(Buffer.from([254, 1, 1]));
34+
parser.write(Buffer.from([217, 2]));
35+
parser.write(Buffer.from([0, 1, 254, 217]));
36+
assert.equal(spy.callCount, 2);
37+
assert.deepEqual(spy.getCall(0).args[0], Buffer.from([2, 2, 1, 254, 1, 1, 217]));
38+
assert.deepEqual(spy.getCall(1).args[0], Buffer.from([2, 0, 1, 254, 217]));
39+
});
40+
});
41+
// 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 commit comments

Comments
 (0)