0.0.2 • Published 11 years ago

node-slip v0.0.2

Weekly downloads
38
License
-
Repository
-
Last release
11 years ago

node-slip

This package parses (and generates) RFC 1055 Serial Line Internet Protocol (SLIP) packets. Used in conjunction with packages like node-serialport, it can be useful to communicate between node programs and old-skool embedded systems that are still using SLIP.

Note: this package is does not support CSLIP (Compressed SLIP.)

Installation

The easiest way to install this package is to use npm:

If you want to check out the source, use the git command:

Usage

Theory of Operation

This package lets you parse and generate SLIP formatted packets. The parser is implemented in a simple stateful object. The user feeds a SLIP octet stream to the parser object. This parser object strips off the leading and trailing END characters (0xC0) and unescapes escape sequences (0xDB 0xDC and 0xDB 0xDD).

When you initialize a parser, you pass it a "receiver" object. When the parser receives a complete packet, it calls the "data" function in the receiver object. If the receiver defines "framing" or "escape" functions, it will call these functions when it encounters framing or escape errors.

The packet generator is a simple static method on the slip package. Pass it a raw node Buffer and the generator will add END characters to the front and back and escape octets that require escaping.

Using the Parser

Start your node program like any other by requiring the package:

Now define a receiver object. This is an instance that optionally defines the functions: data, framing and escape. Here's a very simple example:

Now instantiate a slip parser like so:

And start sending the parser some data:

In theory, this line should emit something like this:

But if you did something like this, you'll see why it's a useful package:

This code fragment will emit this:

Note the parser doesn't emit anything until the whole packet is available. This is useful if (like me) you're using node-serialport to read input from an embedded system over the serial port and that embedded system doesn't always give you complete packets.

Another fun thing that can happen is we get two (or more) SLIP packets in one call to read the serial port. This sometimes happens if your packets are small and you only process serial port input every NNN microseconds. So if you do this:

You'll get this:

And if you get a framing error (like you add stuff between the two middle END bytes, we collect the data and pass it as a parameter to the framing() function. Ergo, doing this:

Will get you this:

Mucking up an escape sequence will cause the escape() function in the receiver to be called. So if you do this:

You get this:

Note: data() and framing() are passed Buffer objects. escape() is only passed a number.

Using the Generator

The generator is much simpler. You simply call the slip.generator() function, passing it a buffer as input. What you get back will be a data buffer with starting and ending END bytes applied and C0 and DB bytes escaped.

This routine:

Will output this: