中文版README)
Node CRC16 - (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.
Versions
If your version of node.js is lower than v8.x.x
, please use the latest v1.x.x
of this module, or you should select v2.x.x
, which uses NAPI
to implement native addon gracefully and compatibly.
Usage
Tips: the most intuitive decription about this module is the comment in src and the code in unit test :).
Install
npm install node-crc16
crc16.checkSum
generate a sum by checkSum
accept three params, the first two params (input, [encoding])
construct a Buffer
crc16
default encoding
is hex
var sum = crc16;sumshould;
the third param is option
,which type is 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>
- default is
var sum = crc16;sumshould;
crc16.verifySum
verify a sum by Params of verifySum
is same as checkSum
, the first two params are used to constructe a Buffer
which contains the sum
to be verified.
var stream = 'a031ffb7' sum = '726d';var isValid = crc16;isValidshould;
Contribution
get source code
# fork and clone the code to your local env git clone git@github.com:imnemo/crc16.gitcd 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
The implemention of CRC16
checking and verifing algorithm in c++ is standalone in ./lib/crc16.cc
. If you want to modify it, please write suitable unittest cases. 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 > node-crc16@1.0.0 benchmark /Users/nemo/code/imnemo/crc16> node benchmark/benchmark.js CEC16#checkSum x 905,071 ops/sec ±2.00% CRC16#verifySum x 1,540,940 ops/sec ±19.92% Fastest is CRC16#verifySum
use nanobench
>>> npm run nanobench > node-crc16@1.0.0 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 # CRC16#verifySum 2,000,000 times ok ~2.85 s all benchmarks completedok ~6.01 s
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
- Refactor node native addon part with NAPI
- Add
index.js.d
- Add global module supported to provide a cli tool
- Add donate entry
twitter: @imoncoding
Welcome to subscribe my wechat!