1.2.6 • Published 5 years ago
stream-helper v1.2.6
stream-helper
Description
Install
$ npm install stream-helper
Usage
Define payload
export class DataTest extends Serializable {
@SerializableInfo.position(0)
@SerializableInfo.numberType(NumberType.UInt32)
public number: number;
@SerializableInfo.position(4)
@SerializableInfo.textEncoding(TextEncoding.ASCII)
@SerializableInfo.length(10)
public text: string;
}
Create streams
Builder, write-only stream:
let builder = new StreamBuilder();
builder.startByte = 0x7E;
// Optional
builder.checksum = (d: Buffer) => CRC.default("CRC16_CCITT_FALSE").computeBuffer(d);
Parser, read-only stream:
let parser = new StreamParser(plManager);
parser.startByte = 0x7E;
// Optional
parser.crcFunction = (d: Buffer) => CRC.default("CRC16_CCITT_FALSE").computeBuffer(d);
Duplex, read-write stream:
var duplex = new StreamDuplex(plManager);
duplex.startByte = 0x7E;
// Optional
duplex.checksum = (d: Buffer) => CRC.default('CRC16_CCITT_FALSE').computeBuffer(d);
Configure I/O
builder.pipe(serialport);
serialport.pipe(parser);
serialport.pipe(duplex);
duplex.builder.pipe(serialport);
Send payload
let data = new DataTest();
data.number = 55;
data.text = "Hello!"
builder.write(data);
Configure payload manager using head
var plManager = new PayloadManager(1);
plManager.registerMessage([2], new DataTest());
Send payload with head
let data = new DataTest();
data.number = 55;
data.text = "Hello!"
builder.write(data);
// Or
duplex.send(data);
Receive and parse data
parser.on("data", (c) => console.info(c));
Log data
parser.trace = true;
parser.logFunction = (direction, start, len, head, payload, checksum) =>
console.log(`${Buffer.from(direction).toString('hex')}
${Buffer.from(start).toString('hex')}
${Buffer.from(len).toString('hex')}
${Buffer.from(head).toString('hex').replace(/(\S{2})/g,"$1 ")}
${Buffer.from(payload).toString('hex').replace(/(\S{2})/g,"$1 ")}
${Buffer.from(checksum).toString('hex').replace(/(\S{2})/g,"$1 ")}`);
Samples
To build and run samples:
git clone https://github.com/RioloGiuseppe/stream-helper.git
cd stream-helper
npm run create-samples
# npm run build-samples
node build/demo/examples/duplex-serialport.js
License
stream-helper packages are all MIT licensed and all it's dependencies are MIT licensed.
Related
serial-port
- Serial port librarycrc-full
- Crc librarybyte-serializer
- Crc library