Electron使用WebAssembly實現CRC-8 ITU校驗
將C/C++語言代碼,經由WebAssembly編譯為庫函數,可以在JS語言環境進行調用。這里介紹在Electron工具環境使用WebAssembly調用CRC-8 ITU格式校驗的方式。
CRC-8 ITU校驗函數WebAssembly源文件
C語言實現CRC-8 ITU格式校驗的介紹見:《C語言CRC-8 ITU格式校驗函數》
選擇上面介紹文章中的uint8_t PY_CRC_8_T_ITU(uint8_t *di, uint32_t len)校驗函數,建立一個新文件PY_CRC_8_T_ITU.cc:
#ifndef EM_PORT_API
# if defined(__EMSCRIPTEN__)
# include <emscripten.h>
# if defined(__cplusplus)
# define EM_PORT_API(rettype) extern "C" rettype EMSCRIPTEN_KEEPALIVE
# else
# define EM_PORT_API(rettype) rettype EMSCRIPTEN_KEEPALIVE
# endif
# else
# if defined(__cplusplus)
# define EM_PORT_API(rettype) extern "C" rettype
# else
# define EM_PORT_API(rettype) rettype
# endif
# endif
#endif#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>EM_PORT_API(void *) mymalloc(uint32_t size) {return malloc(size);
}EM_PORT_API(void) myfree(void * ptr) {free(ptr);
}EM_PORT_API(uint8_t) PY_CRC_8_T_ITU(uint8_t *di, uint32_t len)
{uint8_t crc_poly = 0x07; //X^8+X^2+X^1+1 total 8 effective bits without X^8.uint8_t data_t = 0;while(len--){data_t ^= *di++;for (int8_t i=8; i>0; --i){if (data_t & 0x80)data_t = (data_t<<1) ^ crc_poly;elsedata_t = (data_t<<1);}}return (data_t ^ 0x55);
}
這個文件有三個函數導出,前兩個是獲取和釋放內存的函數,后一個就是CRC-8 ITU校驗函數的導出。
將這個文件進行WebAssembly編譯,就會得到兩個庫文件:
將這幾個文件拷貝到后面建立的Electron工程目錄,再進行調用。
Electron調用WebAssembly CRC-8 ITU函數演示源文件
下載Electron的Hello World!例程,并實現正常運行:
然后將前面的3個WebAssembly相關文件,放到例程根目錄。再引入一個jQuery庫。編寫index.html文件如下:
<!DOCTYPE html>
<html><head><meta charset="UTF-8"><!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP --><link href="./styles.css" rel="stylesheet"><title>Hello World!</title><script>window.$ = window.jQuery = require('./js/jquery-3.3.1.min.js');</script></head><script src="PY_CRC_8_T_ITU.js"></script><script src="./mainprocess.js"></script> <body><h1>Hello World!</h1>We are using Node.js <span id="node-version"></span>,Chromium <span id="chrome-version"></span>,and Electron <span id="electron-version"></span>.<!-- You can also require other files to run in this process --><script src="./renderer.js"></script></body>
</html>
主要修改部分為引入了jQuery,引入了PY_CRC_8_T_ITU.js以及引入了mainprocess.js,mainprocess.js是在例程根目錄下新建的工程文件,內容如下:
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.//增加當前運行狀態和當前運行進程/函數信息,控制不產生誤觸發
window.name="mainwindow"; $(document).ready(function(){Module.onRuntimeInitialized = function() {console.log(Module);}setTimeout(function(){var count = 8;var ptr = Module._mymalloc(count);for (var i = 0; i < count; i++){Module.HEAP8[ptr + i] = 1+i;}console.log(Module._PY_CRC_8_T_ITU(ptr, count));Module._myfree(ptr);},2000); //Delay is a must for Module initialized! });process.on('uncaughtException', function (){});
mainprocess.js實現了WebAssembly庫文件的導入和使用,Module._mymalloc用于申請內存空間,Module._myfree用于釋放內存空間,Module.HEAP8[ptr + i] = 1+i;用于給申請到的內存空間從1開始賦值,這里堆空間為8個字節,因此賦值從1到8。Module._PY_CRC_8_T_ITU(ptr, count)則進行CRC-8 ITU校驗函數的調用,提供了內存指針和要校驗的字節數量。
整個Electron工程環境的文件如下所示:
Electron調用WebAssembly CRC-8 ITU函數演示效果
通過在控制臺輸入 npm start執行Electron工程,打開console顯示:
107是打印出的CRC校驗結果,十六進制值為0x6B, 通過在線工具比較驗證:
Electron使用WebAssembly實現CRC-8 ITU校驗演示工程下載
Electron Demo工程下載,包含已編譯后的WebAssembly庫文件:
–End–