This commit is contained in:
fengerli
2025-11-05 08:48:15 +08:00
2 changed files with 100 additions and 43 deletions

View File

@ -262,7 +262,8 @@
alarmStatus: null,
detailPageUrl: "/pages/6155/deviceDetail",
showConfirm: false
}
},
sendSeq: 0
}
},
@ -1083,6 +1084,8 @@
this.Status.Pop.showPop = true;
},
sendUsr() {
this.sendSeq++;
const currentSeq = this.sendSeq;
let f = this.getDevice();
if (!f) {
these.showBleUnConnect()
@ -1111,7 +1114,7 @@
});
this.setBleFormData();
let task = async () => {
var sendTxtPackge = (rgbdata, type, str) => {
var sendTxtPackge = (rgbdata, type, str, lineIndex) => {
var promise = new Promise((resolve, reject) => {
try {
@ -1136,6 +1139,16 @@
dataView.setUint8(bufferSize - 1, 0xFF);
// 2. 将完整数据包切片成20字节的小块进行发送
// 打印完整数据包,便于首次与再次发送对比
try {
const fullBytes = new Uint8Array(fullBuffer);
const hexString = Array.from(fullBytes).map(b => b.toString(16).padStart(2, '0')).join(' ');
console.log('[7305][SEQ ' + currentSeq + '][Line ' + lineIndex + '] 完整BLE数据包 len=261 type=' + type + ' text="' + (str || '') + '"');
console.log('[7305][SEQ ' + currentSeq + '][Line ' + lineIndex + '] HEX:\n' + hexString);
} catch (e) {
console.warn('[7305][SEQ ' + currentSeq + '] 打印完整BLE数据包失败:', e);
}
const chunkSize = 20;
const numChunks = Math.ceil(fullBuffer.byteLength / chunkSize);
let chunkIndex = 0;
@ -1150,25 +1163,26 @@
const end = Math.min(start + chunkSize, fullBuffer.byteLength);
const chunk = fullBuffer.slice(start, end);
const hexArray = Array.from(new Uint8Array(chunk)).map(b => b
.toString(16).padStart(2, '0'));
console.log(`发送数据块 ${chunkIndex + 1}/${numChunks}:`, hexArray
.join(' '));
const hexArray = Array.from(new Uint8Array(chunk)).map(b => b.toString(16).padStart(2, '0'));
console.log(`[7305][SEQ ${currentSeq}][Line ${lineIndex}] 发送块 ${chunkIndex + 1}/${numChunks}: ${hexArray.join(' ')}`);
ble.sendData(f.deviceId, chunk, f.writeServiceId, f
.wirteCharactId, 100).then(() => {
chunkIndex++;
setTimeout(sendNextChunk, 30); // 个小包之间延时30ms
// 前3个小包放慢节奏,给设备解析时间
const gap = chunkIndex <= 3 ? 60 : 30;
setTimeout(sendNextChunk, gap);
}).catch(err => {
if (err.code == '10007') {
setTimeout(sendNextChunk, 30);
setTimeout(sendNextChunk, 50);
} else {
reject(err);
}
});
}
sendNextChunk();
// 初次发送前增加更长延迟,避免第一次发送异常
setTimeout(sendNextChunk, 100);
} catch (ex) {
console.log("ex=", ex);
@ -1211,7 +1225,9 @@
try {
// console.log("1111");
await sendTxtPackge(rgb, h3dic[i], str);
await sendTxtPackge(rgb, h3dic[i], str, i);
// 每行之间插入小延迟,避免行首解析异常
await new Promise(r => setTimeout(r, 120));
// console.log("222222");
} catch (ex) {
flag = false;