TMRAppBle/script/electric.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-10-19 15:00:17 +08:00
"use strict";
2023-10-30 18:06:37 +08:00
/*CRC16Modbus crc算法*/
2023-10-19 15:00:17 +08:00
function calculateCRC16Modbus(dataHexString) {
var dataBytes = [];
for (var i = 0; i < dataHexString.length; i += 2) {
dataBytes.push(parseInt(dataHexString.substr(i, 2), 16));
}
var crc = 0xFFFF;
var polynomial = 0xA001; // This is the polynomial x^16 + x^15 + x^2 + 1
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = dataBytes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var byte = _step.value;
crc ^= byte;
for (var _i = 0; _i < 8; _i++) {
if (crc & 0x0001) {
crc = (crc >> 1 ^ polynomial) & 0xFFFF;
} else {
crc >>= 1;
}
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return crc.toString(16).toUpperCase();
}
2023-10-30 18:06:37 +08:00
/*组装蜂鸣器控制报文*/
2023-10-19 15:00:17 +08:00
function getControlCmd(status, time) {
var cmd = "FE1003F6000204" + status + "0000" + time;
var crc = calculateCRC16Modbus(cmd);
return cmd + crc.substring(2, 4) + crc.substring(0, 2);
}
var lastMsg = '';
2023-10-30 18:06:37 +08:00
/*发送控制消息到蓝牙*/
2023-10-19 15:00:17 +08:00
function control(status, time) {
var msg = "FFFF000D" + getControlCmd(status, time);
msgList=[]
msgList.push(msg);
2023-10-19 15:00:17 +08:00
}