Home Reference Source Repository
public class | source

Parser

Direct Subclass:

IcarusParser

Parser creation helper class. Any subclasses should reimplement the parse method, calling super.parse in the beginning. You have useful methods for reading regular C datatypes from the buffer: readInt, readFloat, readChar... To use it in node-serialport or the serial wrapper just pass in ::Parser.parse Keep in mind not initialize this class as is. RE-IMPLEMENT Parser.parse!

Constructor Summary

Public Constructor
public

constructor(endianess: String)

Member Summary

Public Members
public

Raw packet byte endianess (Little Endian (LE) for the Arduino).

public

Parsed output container.

Protected Members
protected

Current position in the packet being parsed.

protected

Current packet being parsed.

Method Summary

Public Methods
public

parse(rawPacket: Buffer): Object

Parses a packet.

public

readBoolean(key: String, converter: Function): Boolean

Reads a boolean.

public

readChar(key: String, converter: Function): String

Reads a char.

public

readFloat(key: String, converter: Function): Number

Reads a 32-bit float.

public

readInt(key: String, size: Number, converter: Function): Number

Reads a signed integer of 1-6 bytes.

public

readUInt(key: String, size: Number, converter: Function): Number

Reads an unsigned integer of 1-6 bytes.

public

setValue(key: String, val: mixed, converter: Function): mixed

The value setter called by read* helper functions.

Public Constructors

public constructor(endianess: String) source

Params:

NameTypeAttributeDescription
endianess String
  • optional
  • default: 'LE'

Set byte-order to big endian (BE) or little endian (LE).

Public Members

public endianess: String source

Raw packet byte endianess (Little Endian (LE) for the Arduino). Acceptable values: LE, BE.

public packet: Object source

Parsed output container.

Properties:

NameTypeAttributeDescription
crc.sent String

The CRC32 checkum sent along with the packet.

crc.local String

The CRC32 checksum calculated from the sent data.

Protected Members

protected _i: Number source

Current position in the packet being parsed.

protected _raw: Object source

Current packet being parsed.

Public Methods

public parse(rawPacket: Buffer): Object source

Parses a packet. Default implementation resets state and calculates and extracts the CRC32 checksum before parsing the new packet. That's why, if you want any parsing at all to be performed, YOU MUST RE-IMPLEMENT THIS METHOD. Preferrably calling this implementation before any parsing(but after any decoding wink wink quasi-binary decoder).

Params:

NameTypeAttributeDescription
rawPacket Buffer

The packet to be parsed.

Return:

Object

The parsed object.

public readBoolean(key: String, converter: Function): Boolean source

Reads a boolean.

Params:

NameTypeAttributeDescription
key String

The dot notation path where the boolean goes to.

converter Function
  • optional
  • default: identity

A function that converts the value.

Return:

Boolean

The read boolean.

public readChar(key: String, converter: Function): String source

Reads a char.

Params:

NameTypeAttributeDescription
key String

The dot notation path where the char goes to.

converter Function
  • optional
  • default: identity

A function that converts the value.

Return:

String

The read char.

public readFloat(key: String, converter: Function): Number source

Reads a 32-bit float.

Params:

NameTypeAttributeDescription
key String

The dot notation path where the float goes to.

converter Function
  • optional
  • default: identity

A function that converts the value.

Return:

Number

The read float.

public readInt(key: String, size: Number, converter: Function): Number source

Reads a signed integer of 1-6 bytes.

Params:

NameTypeAttributeDescription
key String

The dot notation path where the int goes to.

size Number
  • optional
  • default: 1

The size in bytes of the integer (1 to 6 bytes only).

converter Function
  • optional
  • default: identity

A function that converts the value.

Return:

Number

The read integer.

public readUInt(key: String, size: Number, converter: Function): Number source

Reads an unsigned integer of 1-6 bytes.

Params:

NameTypeAttributeDescription
key String

The dot notation path where the int goes to.

size Number
  • optional
  • default: 1

The size in bytes of the integer (1 to 6 bytes only).

converter Function
  • optional
  • default: identity

A function that converts the value.

Return:

Number

The read integer.

public setValue(key: String, val: mixed, converter: Function): mixed source

The value setter called by read* helper functions. Uses object path dot notation in the key and can convert values in one line.

Params:

NameTypeAttributeDescription
key String

The object path where the value will be inserted.

val mixed

Anything you want to put.

converter Function
  • optional
  • default: identity

A function that converts the value.

Return:

mixed

The provided value (val).