当前位置: 首页 > news >正文

Electron使用WebAssembly实现CRC-8 ITU校验

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–

相关文章:

  • Electron + Vue 实现系统消息通知与点击跳转页面
  • 如何实现从网页一键启动你的 Electron 桌面应用(zxjapp://)
  • 汽车零部件的EMI抗扰性测试
  • GStreamer (三)常⽤插件
  • word文档中如何在方框中打✔
  • 计算机视觉与深度学习 | Python实现ARIMA-WOA-CNN-LSTM时间序列预测(完整源码和数据
  • eMMC深度解析:嵌入式多媒体卡的硬件电路设计要点
  • 软考 系统架构设计师系列知识点之杂项集萃(63)
  • 大语言模型 13 - 从0开始训练GPT 0.25B参数量 MiniMind2 补充 训练开销 训练步骤 知识蒸馏 LoRA等
  • 核保核赔的集中管理方案
  • 三键标准、多键usb鼠标数据格式
  • 【工具】Windows|外接的显示器怎么用软件调亮度(Brightness Slider)
  • 应用服务器发生内存溢出怎么办
  • 大模型在股骨干骨折诊疗全流程中的应用研究报告
  • 澳大利亚TikTok网络专线+本地化策略:澳洲电商品牌的破局之道
  • Qt—模态与非模态对话框
  • [长城杯 2024]anote
  • Conda 环境下安装 GCC 和 glibc (crypt.h) 教程
  • 数据仓库面试题合集②】ETL 设计与调度策略详解
  • iOS解码实现
  • 英伟达回应在上海设立新办公空间:正租用一个新办公空间,这是在中国持续深耕的努力
  • 一日双赛“莎头组合”赢得强势,但国乒已开始品尝输球滋味
  • 国新办10时将举行新闻发布会,介绍4月份国民经济运行情况
  • 美国前总统拜登确诊前列腺癌
  • 河南发布高温橙警:郑州、洛阳等地最高气温将达40℃以上
  • 通用汽车回应进口车业务调整传闻:因经济形势变化重组,致力于在中国持续发展