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

Electron使用WebAssembly实现CRC-16 CCITT校验

Electron使用WebAssembly实现CRC-16 CCITT校验

将C/C++语言代码,经由WebAssembly编译为库函数,可以在JS语言环境进行调用。这里介绍在Electron工具环境使用WebAssembly调用CRC-16 CCITT格式校验的方式。

CRC-16 CCITT校验函数WebAssembly源文件

C语言实现CRC-16 CCITT格式校验的介绍见:《C语言CRC-16 CCITT格式校验函数》

选择上面介绍文章中的uint16_t PY_CRC_16_T8_CCITT_i(uint8_t *di, uint32_t len)校验函数,建立一个新文件PY_CRC_16_T8_CCITT_i.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(uint16_t) PY_CRC_16_T8_CCITT_i(uint8_t *di, uint32_t len)
{uint16_t crc_poly = 0x8408; //Bit sequence inversion of 0x1021uint16_t data_t = 0; //CRC registerfor(uint32_t i = 0; i < len; i++){data_t ^= di[i]; //8-bit datafor (uint8_t j = 0; j < 8; j++){if (data_t & 0x0001)data_t = (data_t >> 1) ^ crc_poly;elsedata_t >>= 1;}}return data_t;
}

这个文件有三个函数导出,前两个是获取和释放内存的函数,后一个就是CRC-16 CCITT校验函数的导出。

将这个文件进行WebAssembly编译,就会得到两个库文件:
在这里插入图片描述

将这几个文件拷贝到后面建立的Electron工程目录,再进行调用。

Electron调用WebAssembly CRC-16 CCITT函数演示源文件

下载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_16_T8_CCITT_i.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_16_T8_CCITT_i.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_16_T8_CCITT_i(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_16_T8_CCITT_i(ptr, count)则进行CRC-16 CCITT校验函数的调用,提供了内存指针和要校验的字节数量。

整个Electron工程环境的文件如下所示:
在这里插入图片描述

Electron调用WebAssembly CRC-16 CCITT函数演示效果

通过在控制台输入 npm start执行Electron工程,打开console显示:
在这里插入图片描述

61095是打印出的CRC校验结果,十六进制值为0xEEA7, 通过在线工具比较验证:
在这里插入图片描述

Electron使用WebAssembly实现CRC-16 CCITT校验演示工程下载

Electron Demo工程下载,包含已编译后的WebAssembly库文件:
在这里插入图片描述

–End–

http://www.dtcms.com/a/360825.html

相关文章:

  • 9.1C++——类中特殊的成员函数
  • 安卓悬浮球-3566-测试报告
  • vue社区网格化管理系统(代码+数据库+LW)
  • Adobe Acrobat打开pdf文件时闪退如何解决?
  • OpenCV-CUDA 图像处理
  • 论文阅读_TradingAgents多智能体金融交易框架
  • .net 微服务jeager链路跟踪
  • C++11 ——— lambda表达式
  • LeetCode 19: 删除链表的倒数第 N 个结点
  • GIT(了解)
  • 计算机网络---https(超文本传输安全协议)
  • Unity项目基本风格/规范
  • 三、SVN实践练习指南
  • 【项目思维】贪吃蛇(嵌入式进阶方向)
  • 函数、数组与 grep + 正则表达式的 Linux Shell 编程进阶指南
  • GPU 通用手册:裸机、Docker、K8s 环境实战宝典
  • 嵌入式碎片知识总结(二)
  • Shell编程(二):正则表达式
  • 至真科技西安分公司正式成立,赋能点金石业务增长新篇章!
  • 基于Spring Authorization Server的OAuth2与OpenID Connect统一认证授权框架深度解析
  • Linux -- 进程间通信【System V共享内存】
  • 基于llama.cpp在CPU环境部署Qwen3
  • JimuReport 积木报表 v2.1.3 版本发布,免费开源的可视化报表和大屏
  • 【Linux手册】Unix/Linux 信号:原理、触发与响应机制实战
  • 开源 C# .net mvc 开发(九)websocket--服务器与客户端的实时通信
  • Unity:XML笔记
  • 【基础】Three.js中如何添加阴影(附案例代码)
  • 基于SpringBoot的运动服装销售系统【2026最新】
  • 大型语言模型微调 内容预告(69)
  • 剧本杀小程序系统开发:重塑社交娱乐新生态