npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

packet-buffer-parser

v1.1.0

Published

Methods to parse binding packet buffers into structured data.

Readme

packet-buffer-parser

A library to support binary packet buffers of the form:

Byte 1: start byte; must be 0x13
Byte 2: number of data bytes
Bytes 3-n: data bytes. Byte 2 specifies the number of these bytes
Byte n+1: checksum - see details below

This is the format of packet buffers read off the serial port of an iRobot Create using their object interface and it will be the format of the sensor data I will return from my Arduino robot(s).

This parser can handle reading incomplete data sequentially until it has a complete and valid packet. This is a fairly typical situation when reading data off a serial port.

The parser makes no assumption and passes no judgement about the content of the data bytes, it is concerned solely with the structure as defined above.

Checksum

Checksums are useful in a situation like this due to the possibility of corrupt data being read off potentially flaky interfaces. They are a way ensuring that the data being processed is 'sane'.

There are a variety of ways to handle checksum processing. This particular parser will handle checksums created by the iRobot Create correctly.

The easiest way to explain how it works is by example. Let's say the packet is composed of the following bytes:

var buffer = [0x13,0x0b,0x07,0x00,0x13,0x23,0x18,0x14,0x00,0x00,0x21,0x01,0x1f,0x38];

If you add up all the bytes then mask only one byte, it should be 0 to be valid.

var chksum = 0;
for (var i=0; i<buffer.length; i++) {
  chksum += buffer[i];
}

chksum & 0xff === 0

That's all there is to it.

Usage

Assuming you're reading data off a serial port:

const ppb = require('parse-packet-buffer');
...
serial.on('data', function(data) {
  var packet = ppb.parse(data);
  if (packet) {
    // This means we read a complete packet...
    // Do something with it
    ...
    ppb.reset(); // Empty out the current packet to
                 // get ready for the next packet
});

To simplify the process of reading sensor values out of the buffer, you can use the 'parseAndExtract() method. This function allows you to specify the types of sensor values that can be read and the method will return the sensor values already parsed and ready to go.

const ppb = require('parse-packet-buffer');

const SENSOR_TYPES = {
  {
    name: 'sensor1', // The name for the sensor value
    startByte: 0x14, // The value of the start byte for the sensor packet
    numBytes: 1      // The number of bytes for the value
  },
  {
    name: 'sensor2',
    startByte: 0x29,
    numBytes: 2
  }
};

...
serial.on('data', function(data) {
  var sensorValues = parser.parseAndExtract(data, SENSOR_TYPES);
  while (sensorValues) {

    for (var i=0; i<sensorValues.length; i++) {
      console.log("Sensor: %s = %d", sensorValues[i].name, sensorValues[i].value);
    }

    // Call again with null buffer to pick up any serial buffer
    // remainder from the previous call.
    sensorValues = parser.parseAndExtract(null, SENSOR_TYPES);
  }
});

Copyright

Copyright (c) 2016 Naive Roboticist

See LICENSE.txt for details.