CRC computation - Answer


#1

Hi everyone,

Here is my contribution to help people who will need to calculate a CRC (cyclic redundancy check). In my case i had need to calculate CRCXmodem checksum for Bluetooth serial communication.
I spent time to search something in Javascript for my Ionic app and i did not have the time to learn deeper on the subject. We can found a lot of lib or sample in C on the web but it is not always easy to understand and refactoring it to javascript.

But, i finally found a online CRC calculator, wrote in JS, here : http://www.tahapaksu.com/crc/.

After retrieving the source code, i created a ts class you can find here : CRC.ts

You simply need to add it to your project and use it like below :

First import the module (crc in my case) in the providers section in your app.module.ts

import { crc } from './../models/crc';
@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    crc
  ]
})
export class AppModule {}

Next you can use it

import { crc } from './../../models/crc';

@Component({
  selector: 'page-mode-expert',
  templateUrl: 'mode-expert.html',
})
export class ModeExpertPage {

 constructor(public navCtrl: NavController, 
              public navParams: NavParams, 
              private utils: crc) {
        
        this.utils.CRCMaster.init();  //init CRC lib
	    let result = this.utils.CRCMaster.Calculate("01040706","hex");  // Calculate CRC for the hexa string '01040706' 
      	console.log("All results : " + JSON.stringify(result));  // Display the result for all type of CRC
        console.log("CRCXmodem : " + result.crcxmodem);  // Display result for CRCXmodem type only
  }
  
}

That’s all, i hope that it could help you like it helped me !