Git Product home page Git Product logo

crc16's Introduction

Build Status Coverage Status

Node CRC16 - (中文版README)

MODBUS is an application-layer messaging protocol, positioned at level 7 of the OSI model. It provides client/server communication between devices connected on different types of buses or networks.The CRC(Cyclic Redundancy Check) part in protocol, such as MODBUS over serial line(Page 42), and Modbus-RTU(Page 75), adopt the same one algorithm.

node-crc16 implement the c++ version of this algorithm by table look-up, and also provide a node native addon and a nodejs version wrapper.

This module has been well unit tested and documented.

Usage

Tips: the intuitive decription about this module is the comment in src and the code in unit test :).

Install

npm install node-crc16

generate a sum crc16.checkSum

checkSumaccept three params, the first two params (input, [encoding]) construct a buffer

crc16.checkSum('一个utf8字符串', 'utf8')

default encoding is hex

var sum = crc16.checkSum('a031ffb7');
sum.should.equal('726d');

the third param is option,an object

  • option.retType set the format of the returned sum
    • default is hex,two bytes BigEndian hex string, 726d
    • array, two unsigned char number of the returned sum,[114, 109]
    • int,one unsigned short number of the returned sum,29293
    • buffer,Buffer type of the returned sum,<Buffer 72 6d>
var sum = crc16.checkSum('a031ffb7', {retType: 'array'});
sum.should.eql([114, 109]);

verify a sum crc16.verifySum

Params of verifySum is same as checkSum, the first two params is to constructed a buffer, which contains the sum to be verified.

var stream = 'a031ffb7',
    sum = '726d';
var isValid = crc16.verifySum(stream + sum);
isValid.should.equal(true);

Contribution

get source code

# fork and clone the code to your local env
git clone [email protected]:imnemo/crc16.git
cd crc16

code structure

├── lib         //CRC16 algorithm implemention in c++
├── util        //Util functions
├── src         //Node Native Addon
├── test        //JS unit testing
├── test_cpp    //C++ unit testing
├── index.js    //Main entry of NodeJS module

install dependencies

npm install

C++ unit testing

CRC16check and verify algorithm's implemention in c++ is standalone and in ./lib/crc16.cc.If you will modify that, please write suitable unit testing case. You can reference Catch, and then run:

make test

Make sure all the unit testing case is passed after you modify.

JS unit testing

npm test or ./node_modules/.bin/mocha --reporter spec

Benchmark

use benchmark.js

 >>> npm run benchmark

> [email protected] benchmark /Users/nemo/code/imnemo/crc16
> node benchmark/benchmark.js

CEC16#checkSum x 905,071 ops/sec ±2.00% (83 runs sampled)
CRC16#verifySum x 1,540,940 ops/sec ±19.92% (65 runs sampled)
Fastest is CRC16#verifySum

use nanobench

 >>> npm run nanobench

> [email protected] nanobench /Users/nemo/code/imnemo/crc16
> node benchmark/nanobench.js

NANOBENCH version 2
> /Users/nemo/.nvm/versions/node/v8.1.2/bin/node benchmark/nanobench.js

# CRC16#checkSum 2,000,000 times
ok ~3.17 s (3 s + 166422442 ns)

# CRC16#verifySum 2,000,000 times
ok ~2.85 s (2 s + 848059820 ns)

all benchmarks completed
ok ~6.01 s (6 s + 14482262 ns)

pull request

You can pull a request when you complete all steps above.

TODO

  • Add JS code lint
  • Add changelog
  • Add JS unit test
  • Add JS unit test code covarage
  • Add C++ unit test
  • Add CI
  • Add performance test
  • Add global module supported to provide a cli tool
  • Refactor node native addon part with NAN
  • Add donate entry

crc16's People

Contributors

imnemo avatar

Watchers

James Cloos avatar misty avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.