【物联网开发】、【小程序蓝牙通讯数据校验】JS CRC-16-MODBUS 验证 高位在前地位在后;JS异或校验;16进制字符串和float互转

1.CRC校验

javascript 复制代码
/*计算CRC-16/MODBUS校验位高低位*/
function calculateCRC16Modbus(dataHexString) {
	const dataBytes = [];
	for (let i = 0; i < dataHexString.length; i += 2) {
		dataBytes.push(parseInt(dataHexString.substr(i, 2), 16));
	}

	let crc = 0xFFFF;
	const polynomial = 0xA001; // This is the polynomial x^16 + x^15 + x^2 + 1

	for (const byte of dataBytes) {
		crc ^= byte;
		for (let i = 0; i < 8; i++) {
			if (crc & 0x0001) {
				crc = ((crc >> 1) ^ polynomial) & 0xFFFF;
			} else {
				crc >>= 1;
			}
		}
	}

	crc = pad(crc.toString(16).toUpperCase(),4);//补0(不然会出八阿哥)
	let crcArr = new Array(2)
	console.log('crc='+crc)
	crcArr[0] = crc.substring(2, 4)
	crcArr[1] = crc.substring(0, 2)
	let code = crcArr[0] + crcArr[1]
	return code
}

2.异或校验

javascript 复制代码
// 生成校验码
function genCheckCode(msgBodyCode) {
	let firstByte = null;
	for (let i = 0; i < msgBodyCode.length; i = i + 2) {
		const curCode = '0x' + msgBodyCode.substring(i, i + 2);
		if (i == 0) {
			firstByte = curCode
		} else {
			firstByte = firstByte ^ curCode
		}
	}
	firstByte = parseInt(firstByte)
	const codePad = pad(firstByte.toString(16), 2)
	console.log('校验码==' + codePad)
	return codePad;
}

3.pad 左侧补0方法

javascript 复制代码
// 左侧补齐0
function pad(str, max) {
	str = str.toString();
	return str.length < max ? pad("0" + str, max) : str;
}

4.16进制和float(4字节)互转

javascript 复制代码
/*4字节byte十六进制转浮点数      解析   */
function hex2float(hexString, precision) {
	//hex2float('EC519A44', 2))
	precision = parseInt(precision)
	if (isNaN(precision) || precision < 0) {
		precision = 2
	}

	// 高低位转换
	const byteArray = [];
	for (let i = 0; i < hexString.length; i += 2) {
		byteArray.unshift(hexString.substr(i, 2));
	}

	// 创建一个包含十六进制数的 ArrayBuffer
	const hexValue = '0x' + byteArray.join('');
	const buffer = new ArrayBuffer(4);
	const dataView = new DataView(buffer);
	dataView.setUint32(0, hexValue, false);

	// 从 DataView 中读取浮点数
	const floatValue = dataView.getFloat32(0, false);
	return floatValue.toFixed(precision);
}


/*浮点数转4字节十六进制      发送*/
function float2hex(floatNumber) {
	// 创建一个 4 字节的 ArrayBuffer
	const buffer = new ArrayBuffer(4);

	// 使用 DataView 来操作 ArrayBuffer
	const dataView = new DataView(buffer);

	// 将浮点数写入到 DataView 中
	dataView.setFloat32(0, floatNumber, false); // 第二个参数是偏移量,false 表示使用大端字节序

	// 从 DataView 中读取十六进制表示
	const hexString = dataView.getUint32(0, false).toString(16).toUpperCase();

	// 高低位转换
	const byteArray = [];
	for (let i = 0; i < hexString.length; i += 2) {
		byteArray.unshift(hexString.substr(i, 2));
	}
	return byteArray.join('');
}
相关推荐
喵了meme13 小时前
C语言实战4
c语言·开发语言
码界奇点14 小时前
Python从0到100一站式学习路线图与实战指南
开发语言·python·学习·青少年编程·贴图
9ilk14 小时前
【C++】--- 特殊类设计
开发语言·c++·后端
sali-tec14 小时前
C# 基于halcon的视觉工作流-章68 深度学习-对象检测
开发语言·算法·计算机视觉·重构·c#
老前端的功夫14 小时前
Vue 3 性能深度解析:从架构革新到运行时的全面优化
javascript·vue.js·架构
天天扭码15 小时前
如何实现流式输出?一篇文章手把手教你!
前端·aigc·ai编程
前端 贾公子15 小时前
vue移动端适配方案 === postcss-px-to-viewport
前端·javascript·html
生骨大头菜16 小时前
使用python实现相似图片搜索功能,并接入springcloud
开发语言·python·spring cloud·微服务
绝不收费—免费看不了了联系我16 小时前
Fastapi的单进程响应问题 和 解决方法
开发语言·后端·python·fastapi
GISer_Jing16 小时前
AI营销增长:4大核心能力+前端落地指南
前端·javascript·人工智能