serialport-parser-msp-v2

1.0.0 • Public • Published

SerialPort MSP V2 Parser

Introduction

Parser for SerialPort that implements Multiwii Serial Protocol Version 2 according to the MSP-V2 specifications.

Installation

npm install serialport-parser-msp-v2

Usage

The parser can encode MSP commands and decode MSP responses.

MSP commands and responses are of type MspMsg.

interface MspMsg {
  cmd: number,
  flag: number,
  buffer: number[]
}

Flag has a default value of 0.

Encoding commands

The MSPEncoder encodes MSP commands.

Initialisation

new MspEncoder()

Example

This example writes an MSP_IDENT command to the serialport.

import SerialPort from 'serialport'
import { MspCmd, MspMsg, MspEncoder } from 'serialport-parser-msp-v2'

const serialPort = new SerialPort('/dev/ttyUSB0')
const encoder = new MspEncoder().pipe(serialPort)
const mspCmd = {
    cmd: MspCmd.MSP_IDENT,
    buffer: []
  }
encoder.write(mspCmd)

For more examples see here.

If the MspEncoder gives an error, you can encode the MspCmd as follows:

import SerialPort from 'serialport'
import { encode, MspCmd, MspMsg } from 'serialport-parser-msp-v2'

const serialPort = new SerialPort('/dev/ttyUSB0')
const mspCmd = {
    cmd: MspCmd.MSP_IDENT,
    buffer: []
  }
serialPort.write(encode(mspCmd))

Decoding responses

The MSPEncoder decodes MSP responses.

Initialisation

new MspDecoder()

Example

This example registers a function that will receive

  • an Error object when an MSP error message is received
  • an MspMsg object when an MSP response message is received
import SerialPort from 'serialport'
import { MspCmd, MspMsg, MspDecoder } from 'serialport-parser-msp-v2'

const serialPort = new SerialPort('/dev/ttyUSB0')
const decoder = serialPort.pipe(new MspDecoder())
parser.on('data', function (object: any) {
    if (object instanceof Error)
      console.log("MSP Error received")
    else
      console.log("MspResponse: " + object)
  })

For more examples see here.

Readme

Keywords

Package Sidebar

Install

npm i serialport-parser-msp-v2

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

66.1 kB

Total Files

31

Last publish

Collaborators

  • eluinstra