From e247acdd5e2a952302142b852620fab05f001114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=BE=AE=E4=B8=80=E7=AC=91?= <709648985@qq.com> Date: Wed, 26 Nov 2025 11:12:05 +0800 Subject: [PATCH] =?UTF-8?q?6155=E4=BA=BA=E5=91=98=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E5=90=88=E6=88=90=E4=B8=80=E4=B8=AA06=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=A4=B4=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/6155/deviceDetail.vue | 227 +++++++++++------------------------- 1 file changed, 71 insertions(+), 156 deletions(-) diff --git a/pages/6155/deviceDetail.vue b/pages/6155/deviceDetail.vue index d31c03c..d6cf78d 100644 --- a/pages/6155/deviceDetail.vue +++ b/pages/6155/deviceDetail.vue @@ -1524,166 +1524,80 @@ }); this.setBleFormData(); let task = async () => { - var sendTxtPackge = (rgbdata, type, str) => { - - var promise = new Promise((resolve, reject) => { - try { - // 设备协议:FA 06/07/08 01 00 + 256字节数据 + FF - // 每个文本行只需要一个261字节的包 - const bufferSize = 261; - const dataSize = 256; // 数据部分固定256字节 - - let buffer = new ArrayBuffer(bufferSize); - let dataView = new DataView(buffer); - - // 写入头部:FA 06/07/08 01 00 - dataView.setUint8(0, 0xFA); - dataView.setUint8(1, type); - dataView.setUint8(2, 0x01); - dataView.setUint8(3, 0x00); - - // 写入数据(最多256字节),确保数据是数字格式 - let actualDataSize = Math.min(rgbdata.length, dataSize); - for (let i = 0; i < actualDataSize; i++) { - // 确保数据是数字格式,如果是字符串则转换 - let byteValue = typeof rgbdata[i] === 'string' ? these.toByteValue(rgbdata[i]) : rgbdata[i]; - dataView.setUint8(4 + i, byteValue); - } - - // 用0填充剩余数据部分(如果数据不足256字节) - for (let i = 4 + actualDataSize; i < bufferSize - 1; i++) { - dataView.setUint8(i, 0x00); - } - - // 写入尾部:FF - dataView.setUint8(bufferSize - 1, 0xFF); - - console.log(`发送文本数据包: type=0x${type.toString(16)}, 数据长度=${actualDataSize}, 总长度=${bufferSize}`); - - // 发送数据包 - ble.sendData(f.deviceId, buffer, f.writeServiceId, f - .wirteCharactId, 100).then(() => { - resolve(); - }).catch(err => { - if (err.code == '10007') { - // 重试 - setTimeout(() => { - ble.sendData(f.deviceId, buffer, f.writeServiceId, f - .wirteCharactId, 100).then(() => { - resolve(); - }).catch(reject); - }, 100); - } else { - reject(err); - } - }); - - } catch (ex) { - console.log("构建数据包异常:", ex); - reject(ex); - } - }); - - return promise; - - } - - console.log("11111"); - var result = null; try { - console.log("this.$refs.textToHex=", this.$refs.textToHex); - // 获取像素数据(组件内部会自动预热画布) - result = await this.$refs.textToHex.drawAndGetPixels(); - } catch (ex) { - console.log("获取画布数据异常:", ex); - } - if (!result) { - hideLoading(this); - return; - } - console.log("result=", result); - result = result.map(level1 => { - return level1.flat(Infinity).map(item => { - // 确保数据转换为数字格式,避免字符串格式导致的问题 - return typeof item === 'string' ? these.toByteValue(item) : item; - }); - }); - console.log("result=", result); + // 预热画布,确保画布和字体完全准备好(解决APP重新打开后第一次获取数据不完整的问题) + console.log("预热画布..."); + await this.$refs.textToHex.drawAndGetPixels(); + await new Promise(resolve => setTimeout(resolve, 200)); // 等待预热完成 - // var str1="FA 06 01 00 FF FF F7 9F EF 6F EC F7 EA 09 CF FF AF FB EF EB EF EB EC 6B EF EB EC 6B EF EB EF FB EE 63 FF FF FF FF F7 9F EF 6F EC F7 EA 09 CF FF AF FB EF EB EF EB EC 6B EF EB EC 6B EF EB EF FB EE 63 FF FF FF FF F7 FF 81 03 ED BB DD B7 CB CF F3 C7 CD 39 BE FF FE FF C0 03 FE FB FD FB F3 F7 8F 87 FF FF FF FF FE FF FE FF FE FF C0 03 FF FB FD FB FD FB FD FB FD FB FB FB FB FF F7 F7 EF F7 9F 8F FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF" - // var str2="FA 07 01 00 FF FF EE DD EE DF EF 5B AB DF AA 03 AE FF AE FF EE 03 EE FF EE FF EE 03 EE FF EE FF EE E3 FF FF FF FF EE DD EE DF EF 5B AB DF AA 03 AE FF AE FF EE 03 EE FF EE FF EE 03 EE FF EE FF EE E3 FF FF FF FF EF 77 EF 73 EF 7F 80 01 EF 7F EF 7F EF 03 E7 3B 8E BB EE D7 EE EF ED E7 ED 9B 8B 7D FF FF FF FF FF FF F7 EF F7 F7 EF F7 DF FB FF FF FF FF FE FF 80 01 FE 7F FD BF FB DF F7 E7 9F F9 FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF" - // var str3="FA 08 01 00 FF FF EF DF EC 01 EF FF AB FF AA 03 AA FB AE FB EE 03 EF DF EF DF EE DB ED DF ED DD EF 1F FF FF FF FF EF BF EF 87 81 77 EE F7 EC 03 81 7F EF 7F EF 7F EF 03 81 7F EF 7F EF 7D EF 7D EF 03 FF FF FF FF F9 F1 CF BF DF FF DF FF C1 FF DD 81 DD F7 DD F7 C1 F7 DF 77 FF 77 BF 77 BF 77 FF F7 FF FF FF FF FD FF FD FF FB FF FB FF F0 07 E7 F7 EF F7 D8 07 BF F7 FF F7 F8 07 FF F7 FF F7 FF C7 FF FF FF FF FF FF FF FF FF FF FE FF FE 7F FE 7F FE FF FD BF FD FF FB DF F7 EF EF F7 DF FB BF FD FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF" - - // let arr1=('0x'+(str1.split(' ').join(",0x"))).split(','); - // let arr2=('0x'+(str2.split(' ').join(",0x"))).split(','); - // let arr3=('0x'+(str3.split(' ').join(",0x"))).split(','); - - // result=[arr1,arr2,arr3]; - - - // console.log("result=",result); - - - let h3dic = [0x06, 0x07, 0x08]; - let pros = []; - let flag = true; - // 在开始发送前添加延迟,确保数据完全准备好(解决第一次发送第一个字缺失的问题) - await new Promise(resolve => setTimeout(resolve, 500)); - - for (var i = 0; i < result.length; i++) { - - let str = this.formData.textLines[i]; - - if (str.length > 0) { - - let width = str.length * 16; - var rgb = result[i]; - - // 确保数据是数组且已转换 - if (!Array.isArray(rgb)) { - console.error(`第${i+1}行数据格式错误:`, rgb); - flag = false; - break; - } - - // 第一次发送前额外延迟,确保设备完全准备好 - if (i === 0) { - console.log("准备发送第一个文本行,等待设备准备好..."); - await new Promise(resolve => setTimeout(resolve, 500)); - } - - try { - console.log(`开始发送第${i+1}个文本行 (type=0x${h3dic[i].toString(16)})`); - await sendTxtPackge(rgb, h3dic[i], str); - console.log(`第${i+1}个文本行发送完成`); - - // 每个文本行发送完成后,添加延迟确保设备处理完成 - if (i < result.length - 1) { - await new Promise(resolve => setTimeout(resolve, 300)); - } - } catch (ex) { - flag = false; - console.error(`发送第${i+1}个文本行出现异常:`, ex); - break; - } + // 1. 获取所有文本行的像素数据 + const allPixels = await this.$refs.textToHex.drawAndGetPixels(); + if (!allPixels) { + throw new Error("无法生成像素数据"); } - - - } - - hideLoading(these); - if (flag) { - console.log("发送成功"); + + // 2. 将所有像素数据合并到一个大数组中 + let combinedData = []; + for (let i = 0; i < 3; i++) { + const linePixels = (allPixels[i] || []).flat(Infinity).map(item => + typeof item === 'string' ? these.toByteValue(item) : item + ); + // 补齐到256字节 + while (linePixels.length < 256) { + linePixels.push(0x00); + } + combinedData = combinedData.concat(linePixels.slice(0, 256)); + } + + // 3. 构建完整的逻辑大包 + const logicalPacketSize = 4 + combinedData.length + 1; // Header + Data + Footer + const logicalBuffer = new ArrayBuffer(logicalPacketSize); + const logicalDataView = new DataView(logicalBuffer); + + // 写入头部 + logicalDataView.setUint8(0, 0xFA); + logicalDataView.setUint8(1, 0x06); + logicalDataView.setUint8(2, 0x03); + logicalDataView.setUint8(3, 0x00); + + // 写入数据 + for (let i = 0; i < combinedData.length; i++) { + logicalDataView.setUint8(4 + i, combinedData[i]); + } + + // 写入尾部 + logicalDataView.setUint8(logicalPacketSize - 1, 0xFF); + + // 4. 将逻辑大包分包发送 + const CHUNK_SIZE = 20; // 每个物理包的大小 + const totalChunks = Math.ceil(logicalPacketSize / CHUNK_SIZE); + + updateLoading(these, { + text: `准备发送,共 ${totalChunks} 个数据包...` + }); + + for (let i = 0; i < totalChunks; i++) { + const start = i * CHUNK_SIZE; + const end = Math.min(start + CHUNK_SIZE, logicalPacketSize); + const chunk = logicalBuffer.slice(start, end); + + updateLoading(these, { + text: `正在发送 ${i + 1} / ${totalChunks}` + }); + + await ble.sendData(f.deviceId, chunk, f.writeServiceId, f.wirteCharactId, 50); + await new Promise(resolve => setTimeout(resolve, 50)); // 包之间延迟 + } + + // 5. 发送成功处理 + hideLoading(these); this.showPop({ message: "发送成功", iconUrl: "/static/images/6155/DeviceDetail/uploadSuccess.png", borderColor: '#BBE600', buttonBgColor: '#BBE600' }); - - - - + let json = { deviceId: these.device.id, name: these.formData.textLines[1], @@ -1691,17 +1605,18 @@ unitName: these.formData.textLines[2], code: "" }; - usrApi.sendUsr(json) - } else { + usrApi.sendUsr(json); + + } catch (ex) { + hideLoading(these); this.showPop({ - message: "出现异常发送失败", + message: "发送失败: " + (ex.msg || ex.message), iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png", borderColor: "#e034344d", buttonBgColor: "#E03434", }); } - } - + }; setTimeout(task, 0); }, getDetail() {