How to use the crc.crc16ccitt function in crc

To help you get started, we’ve selected a few crc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pulsar-heavy-industries / phi / www / src / midi.js View on Github external
rd++

                        // see how many bytes we will be processing in this chunk
                        let chunk = Math.min(7, data.length - rd)

                        for (let i = 0; i < chunk; ++i)
                        {
                            data[w] = data[rd] | ( (bits & (1 << i)) ? 0x80 : 0 )
                            rd++
                            w++
                        }
                    }

                    let crc1 = data[0] | (data[1] << 8)
                    data = data.slice(2, w)
                    let crc2 = crc16ccitt(data)

                    if (crc1 === crc2)
                    {
                        this.emitter.emit('in-sysex', data)
                    }
                    else
                    {
                        console.log('SYSEX BAD CRC', crc1, crc2, data)
                    }
                }
                break

            default:
                console.log('Unknown MIDI data', data)
                break
        }
github vshymanskyy / blynk-library-js / experiments / ble-to-tcp.js View on Github external
BleBeanSerial.prototype.sendCmd = function(cmdBuffer,payloadBuffer,done) {
  var sizeBuffer = new Buffer(2);
  sizeBuffer.writeUInt8(cmdBuffer.length + payloadBuffer.length,0);
  sizeBuffer.writeUInt8(0,1);

  //GST contains sizeBuffer, cmdBuffer, and payloadBuffer
  var gst = Buffer.concat([sizeBuffer,cmdBuffer,payloadBuffer]);

  var crcString = crc.crc16ccitt(gst);
  var crc16Buffer = new Buffer(2);
  crc16Buffer.writeUInt16LE(crcString, 0);
  
  gst = Buffer.concat([sizeBuffer,cmdBuffer,payloadBuffer,crc16Buffer]);
  
  var gt_qty = Math.floor(gst.length/19);
  if (gst.length % 19 != 0) {
    gt_qty += 1;
  }
  var optimal_packet_size = 19;
  
  for (var ch=0; ch
github vshymanskyy / blynk-library-js / bin / blynk-gateway-ble.js View on Github external
//first packet, reset data buffer
  if (start) {
    this.gst = new Buffer(0);
  }

  //TODO probably only if messageCount is in order
  this.gst = Buffer.concat( [this.gst, gt.slice(1)] );

  //last packet, process and emit
  if(packetCount === 0){

    var length = this.gst[0]; //size of thse cmd and payload

    //crc only the size, cmd and payload
    var crcString = crc.crc16ccitt(this.gst.slice(0,this.gst.length-2));
    //messy buffer equality because we have to swap bytes and can't use string equality because tostring drops leading zeros
    
    //console.log('CRC: ' , typeof crcString);
    var crc16 = new Buffer(2);
    crc16.writeUInt16BE(crcString, 0);
    
    var valid = (crc16[0]===this.gst[this.gst.length-1] && crc16[1]===this.gst[this.gst.length-2]);

    var command = ( (this.gst[2] << 8) + this.gst[3] ) & ~(0x80) ;

    //this.emit('raw', this.gst.slice(2,this.gst.length-2), length, valid, command);

    if(valid){

      //ideally some better way to do lookup
      if(command === (commands.MSG_ID_CC_ACCEL_READ[0] << 8 ) + commands.MSG_ID_CC_ACCEL_READ[1])
github vshymanskyy / blynk-library-js / experiments / ble-to-tcp.js View on Github external
//first packet, reset data buffer
  if (start) {
    this.gst = new Buffer(0);
  }

  //TODO probably only if messageCount is in order
  this.gst = Buffer.concat( [this.gst, gt.slice(1)] );

  //last packet, process and emit
  if(packetCount === 0){

    var length = this.gst[0]; //size of thse cmd and payload

    //crc only the size, cmd and payload
    var crcString = crc.crc16ccitt(this.gst.slice(0,this.gst.length-2));
    //messy buffer equality because we have to swap bytes and can't use string equality because tostring drops leading zeros
    
    //console.log('CRC: ' , typeof crcString);
    var crc16 = new Buffer(2);
    crc16.writeUInt16BE(crcString, 0);
    
    var valid = (crc16[0]===this.gst[this.gst.length-1] && crc16[1]===this.gst[this.gst.length-2]);

    var command = ( (this.gst[2] << 8) + this.gst[3] ) & ~(0x80) ;

    //this.emit('raw', this.gst.slice(2,this.gst.length-2), length, valid, command);

    if(valid){

      //ideally some better way to do lookup
      if(command === (commands.MSG_ID_CC_ACCEL_READ[0] << 8 ) + commands.MSG_ID_CC_ACCEL_READ[1])
github noelportugal / bean-writer / bean-writer.js View on Github external
function getBuffer(cmdBuffer,payloadBuffer){

    //size buffer contains size of(cmdBuffer, and payloadBuffer) and a reserved byte set to 0
    var sizeBuffer = new Buffer(2);
    sizeBuffer.writeUInt8(cmdBuffer.length + payloadBuffer.length,0);
    sizeBuffer.writeUInt8(0,1);

    //GST (Gatt Serial Transport) contains sizeBuffer, cmdBuffer, and payloadBuffer
    var gstBuffer = Buffer.concat([sizeBuffer,cmdBuffer,payloadBuffer]);

    var crcString = crc.crc16ccitt(gstBuffer);
    var crc16Buffer = new Buffer(crcString, 'hex');

    //GATT contains sequence header, gstBuffer and crc166
    var gattBuffer = new Buffer(1 + gstBuffer.length + crc16Buffer.length);

    var header = (((this.count++ * 0x20) | 0x80) & 0xff);
    gattBuffer[0]=header;

    gstBuffer.copy(gattBuffer,1,0); //copy gstBuffer into gatt shifted right 1

    //swap 2 crc bytes and add to end of gatt
    gattBuffer[gattBuffer.length-2]=crc16Buffer[1];
    gattBuffer[gattBuffer.length-1]=crc16Buffer[0];
    
    console.log(gattBuffer);
github jacobrosenthal / ble-bean / lib / Bean.js View on Github external
//first packet, reset data buffer
  if (start) {
    this.gst = new Buffer(0);
  }

  //TODO probably only if messageCount is in order
  this.gst = Buffer.concat( [this.gst, gt.slice(1)] );

  //last packet, process and emit
  if(packetCount === 0){

    var length = this.gst[0]; //size of thse cmd and payload

    //crc only the size, cmd and payload
    var crcString = crc.crc16ccitt(this.gst.slice(0,this.gst.length-2));
    //messy buffer equality because we have to swap bytes and can't use string equality because tostring drops leading zeros
    var crc16 = new Buffer(crcString, 'hex');
    var valid = (crc16[0]===this.gst[this.gst.length-1] && crc16[1]===this.gst[this.gst.length-2]);

    var command = ( (this.gst[2] << 8) + this.gst[3] ) & ~(0x80) ;

    this.emit('raw', this.gst.slice(2,this.gst.length-2), length, valid, command);

    if(valid){

      //ideally some better way to do lookup
      if(command === (commands.MSG_ID_CC_ACCEL_READ[0] << 8 ) + commands.MSG_ID_CC_ACCEL_READ[1])
      {
        var x = (((this.gst[5] << 24) >> 16) | this.gst[4]) * 0.00391;
        var y = (((this.gst[7] << 24) >> 16) | this.gst[6]) * 0.00391;
        var z = (((this.gst[9] << 24) >> 16) | this.gst[8]) * 0.00391;
github jacobrosenthal / ble-bean / lib / Bean.js View on Github external
Bean.prototype.send = function(cmdBuffer,payloadBuffer,done){

  //size buffer contains size of(cmdBuffer, and payloadBuffer) and a reserved byte set to 0
  var sizeBuffer = new Buffer(2);
  sizeBuffer.writeUInt8(cmdBuffer.length + payloadBuffer.length,0);
  sizeBuffer.writeUInt8(0,1);

  //GST contains sizeBuffer, cmdBuffer, and payloadBuffer
  var gstBuffer = Buffer.concat([sizeBuffer,cmdBuffer,payloadBuffer]);

  var crcString = crc.crc16ccitt(gstBuffer);
  var crc16Buffer = new Buffer(crcString, 'hex');

  //GATT contains sequence header, gstBuffer and crc166
  var gattBuffer = new Buffer(1 + gstBuffer.length + crc16Buffer.length);

  var header = (((this.count++ * 0x20) | 0x80) & 0xff);
  gattBuffer[0]=header;

  gstBuffer.copy(gattBuffer,1,0); //copy gstBuffer into gatt shifted right 1

  //swap 2 crc bytes and add to end of gatt
  gattBuffer[gattBuffer.length-2]=crc16Buffer[1];
  gattBuffer[gattBuffer.length-1]=crc16Buffer[0];

  this.writeDataCharacteristic(SERIAL_UUID, BEAN_SERIAL_CHAR_UUID, gattBuffer, done);
github PunchThrough / bean-sdk-node / src / util / util.js View on Github external
function crc16(buf) {
  return crc.crc16ccitt(buf)
}
github pulsar-heavy-industries / phi / www / src / midi.js View on Github external
return new Promise((resolve, reject) => {
            let crc = crc16ccitt(buf)
            buf = [crc & 0xff, (crc >> 8) & 0xff].concat(buf)

            let out_buf = []
            for (let r = 0; r < buf.length; )
            {
                let chunk = buf.length - r
                if (chunk > 7)
                {
                    chunk = 7
                }

                let bits = 0
                for (let i = 0; i < chunk; ++i)
                {
                    if (buf[r + i] & 0x80)
                    {

crc

Module for calculating Cyclic Redundancy Check (CRC) for Node.js and the browser.

MIT
Latest version published 2 years ago

Package Health Score

70 / 100
Full package analysis