A native node addon to calcalate and verify CRC16 values, adopted by MODBUS agreement.
npm install node-crc16

CRC(Cyclic Redundancy Check) part in protocol, such as [MODBUS over serial line]2, and [Modbus-RTU]3, 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.
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.
Tips: the most intuitive decription about this module is the comment in src and the code in unit test :).
npm install node-crc16checkSumaccept three params, the first two params (input, [encoding]) construct a Bufferjavascript
crc16.checkSum('utf8 string', 'utf8')
`
default encoding is hex
`javascript
var sum = crc16.checkSum('a031ffb7');
sum.should.equal('726d');
`
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,
`javascript
var sum = crc16.checkSum('a031ffb7', {retType: 'array'});
sum.should.eql([114, 109]);
`
$3
Params of verifySum is same as checkSum, the first two params are used to constructe a Buffer which contains the sum to be verified.
`javascript
var stream = 'a031ffb7',
sum = '726d';
var isValid = crc16.verifySum(stream + sum);
isValid.should.equal(true);
`Contribution
$3
`sh
fork and clone the code to your local env
git clone git@github.com:imnemo/crc16.git
cd crc16
`$3
`
├── 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
`$3
npm install$3
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:
`bash
make test
`
> Make sure all the unit testing case is passed after you modify.$3
npm test or ./node_modules/.bin/mocha --reporter spec$3
#### use benchmark.js
`bash
>>> 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% (83 runs sampled)
CRC16#verifySum x 1,540,940 ops/sec ±19.92% (65 runs sampled)
Fastest is CRC16#verifySum
`#### use nanobench
`bash
>>> 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 (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)
`$3
You can pull a request when you complete all steps above.
TODO
- [x] Add JS code lint
- [x] Add changelog
- [x] Add JS unit test
- [x] Add JS unit test code covarage
- [x] Add C++ unit test
- [x] Add CI
- [x] Add performance test
- [x] Refactor node native addon part with NAPI
- [x] Add index.js.d`---
twitter: @imoncoding

Welcome to subscribe my wechat!
[1]: http://modbus.org/specs.php
[2]: https://www.honeywellprocess.com/library/support/Public/Documents/51-52-25-66.pdf
[3]: http://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf