@serialpilot/parser-packet-length
v1.0.1
Published
A transform stream that frames length-prefixed packets.
Maintainers
Readme
A Transform stream for protocols that prefix each packet with its length. Reads the length field at a configured offset and emits a complete packet once that many bytes have been received.
npm install @serialpilot/parser-packet-lengthUsage
const { SerialPilot, PacketLengthParser } = require('serialpilot')
const port = new SerialPilot({ path: '/dev/ttyUSB0', baudRate: 9600 })
const parser = port.pipe(new PacketLengthParser({
delimiter: 0xa5, // packet header byte
packetOverhead: 5, // bytes outside the payload
lengthBytes: 1, // size of the length field
lengthOffset: 2, // byte offset of the length field
maxLen: 0xff, // largest valid packet
}))
parser.on('data', console.log)API
new PacketLengthParser(options)
| Option | Type | Default | Description |
| ---------------- | ------ | ------- | ------------------------------------------ |
| delimiter | number | 0xaa | Header byte that begins each packet. |
| packetOverhead | number | 2 | Non-payload bytes per packet. |
| lengthBytes | number | 1 | Size of the length field in bytes. |
| lengthOffset | number | 1 | Byte offset of the length field. |
| maxLen | number | 0xff | Maximum allowed packet length. |
See the SerialPilot README for the full set of parsers.
