mirror of
https://github.com/siemens-mobile-hacks/node-serialport-bindings-cpp.git
synced 2026-11-25 18:37:19 +00:00
feat(parsers): ByteLength is now more efficient (#1402)
And better documented
This commit is contained in:
parent
dcd256d27a
commit
f7eb2f0df4
1 changed files with 18 additions and 11 deletions
|
|
@ -5,14 +5,16 @@ const Transform = require('stream').Transform;
|
|||
/**
|
||||
* A transform stream that emits data as a buffer after a specific number of bytes are received.
|
||||
* @extends Transform
|
||||
* @param {Object} options
|
||||
* @param {Number} options.length the number of bytes on each data event
|
||||
* @example
|
||||
To use the `ByteLength` parser, provide the length of the number of bytes:
|
||||
To use the `ByteLength` parser:
|
||||
```js
|
||||
const SerialPort = require('serialport');
|
||||
const ByteLength = SerialPort.parsers.ByteLength
|
||||
const port = new SerialPort('/dev/tty-usbserial1');
|
||||
const parser = port.pipe(new ByteLength({length: 8}));
|
||||
parser.on('data', console.log);
|
||||
parser.on('data', console.log); // will have 8 bytes per data event
|
||||
```
|
||||
*/
|
||||
class ByteLengthParser extends Transform {
|
||||
|
|
@ -29,23 +31,28 @@ class ByteLengthParser extends Transform {
|
|||
}
|
||||
|
||||
this.length = options.length;
|
||||
this.buffer = Buffer.alloc(0);
|
||||
this.position = 0;
|
||||
this.buffer = Buffer.alloc(this.length);
|
||||
}
|
||||
|
||||
_transform(chunk, encoding, cb) {
|
||||
let data = Buffer.concat([this.buffer, chunk]);
|
||||
while (data.length >= this.length) {
|
||||
const out = data.slice(0, this.length);
|
||||
this.push(out);
|
||||
data = data.slice(this.length);
|
||||
let cursor = 0;
|
||||
while (cursor < chunk.length) {
|
||||
this.buffer[this.position] = chunk[cursor];
|
||||
cursor++;
|
||||
this.position++;
|
||||
if (this.position === this.length) {
|
||||
this.push(this.buffer);
|
||||
this.buffer = Buffer.alloc(this.length);
|
||||
this.position = 0;
|
||||
}
|
||||
}
|
||||
this.buffer = data;
|
||||
cb();
|
||||
}
|
||||
|
||||
_flush(cb) {
|
||||
this.push(this.buffer);
|
||||
this.buffer = Buffer.alloc(0);
|
||||
this.push(this.buffer.slice(0, this.position));
|
||||
this.buffer = Buffer.alloc(this.length);
|
||||
cb();
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue