lefengyang
3 days ago b020606dadaea909d91a8e34287ef4cddb9d4623
发送前清除
1 files modified
77 ■■■■ changed files
ledApp/pages/index/index.vue 77 ●●●● patch | view | raw | blame | history
ledApp/pages/index/index.vue
@@ -191,7 +191,8 @@
                dId: '' ,//已连接的设备id,
                devKwh: '',
                platform: '',
                screenOn: true
                screenOn: true,
                waitDallThenSend: false // 标记:收到DALL清除所有信息指令回应后,再开始发送INST帧
            }
        },
        onLoad() {
@@ -377,7 +378,7 @@
                console.log('--------------', item);
                uni.showLoading({
                    title: '发送中...',
                    title: '发送中0%',
                    mask: true
                });
@@ -402,7 +403,9 @@
                }
                if (this.ledData.length > 0) {
                    this.commandINST(this.ledData[this.ledCurrent]);
                    // 向智能壳发送INST帧前,先发送一次清除所有信息指令(DALL),待智能壳回应后再开始发送
                    this.waitDallThenSend = true;
                    this.commandDALL();
                } else {
                    uni.hideLoading();
                    uni.showToast({
@@ -587,7 +590,8 @@
            },
            //向低功耗蓝牙设备特征值中写入二进制数据
            writeBLECharacteristicValue(buffer) {
            //keepLoading: 发送INST帧(含DALL前置指令、结束帧)过程中保持进度loading,发送成功后不关闭
            writeBLECharacteristicValue(buffer, keepLoading = false) {
                const that = this
                uni.writeBLECharacteristicValue({
                    deviceId: this.deviceId,
@@ -597,7 +601,9 @@
                    writeType: 'write',
                    success: (res) => {
                        //console.log('写入指令发送成功----', res);
                        if (!keepLoading) {
                        uni.hideLoading();
                        }
                    },
                    fail: (err) => {
                        //console.log('写入指令发送失败----', err);
@@ -706,6 +712,42 @@
                    this.writeBLECharacteristicValue(buffer)
                }, 500);
            },
            // 清除所有信息指令(DALL)
            commandDALL() {
                // 1.手机发指令:启动字节+帧头+包头段(0x43+简易绑定号+屏大小)+DALL+CRC校验码+帧尾。
                // 2.智能壳回指令:启动字节+帧头+包头段+DALL+返回标志+加密地址+CRC校验码+帧尾。
                // 构建指令数据
                const startBytes = new Array(10).fill(0x55); // 10个启动字节
                const frameHeader = 0x7E; // 帧头
                const packageType = 0x43; // 包类型
                // 使用新的简易绑定号生成函数
                const simpleBindId = this.generateSimpleBindId();
                // 屏大小 (2 bytes)
                const screenSize = [0x0C, 0x18];
                const packageHeader = [packageType, ...simpleBindId, ...screenSize];
                const command = [0x44, 0x41, 0x4C, 0x4C]; // "DALL"的ASCII码
                // 组合数据
                const data = [
                    ...startBytes,
                    frameHeader,
                    ...packageHeader,
                    ...command
                ];
                // 计算CRC校验码(从包头段开始计算)
                const crc = this.calcCRC16(data.slice(11));
                // 添加CRC和帧尾
                data.push((crc >> 8) & 0xFF); // CRC高8位
                data.push(crc & 0xFF); // CRC低8位
                data.push(frameHeader); // 帧尾
                console.log('DALL data----', this.hexArrayToHexString(data))
                const buffer = new Uint8Array(data).buffer;
                setTimeout(() => {
                    this.writeBLECharacteristicValue(buffer, true)
                }, 50);
            },
            //添加信息指令(INST)
            commandINST(hexString) {
                // 1.手机发指令:启动+帧头+包头段(0x43+简易绑定号+屏大小)+INST+信息属性+文件名+总帧数+当前帧*+块描述+块数据+CRC校验码+帧尾
@@ -764,6 +806,12 @@
                //console.log(this.ledCurrent, '----INST data----', this.hexArrayToHexString(data))
                //const buffer = new Uint8Array(data).buffer;
                console.log('第几帧-------',this.ledCurrent)
                // 根据总帧数和已发送数量更新发送进度(当前帧发送前,已发送ledCurrent帧),范围10%~99%
                const sendPercent = Math.max(10, Math.min(99, Math.round(this.ledCurrent / this.ledTotal * 100)));
                uni.showLoading({
                    title: '发送中' + sendPercent + '%',
                    mask: true
                });
                setTimeout(() => {
                    //this.writeBLECharacteristicValue(buffer)
                    this.sendBLEDataInChunks(
@@ -821,7 +869,7 @@
                const endbuffer = new Uint8Array(endData).buffer;
                setTimeout(() => {
                    this.writeBLECharacteristicValue(endbuffer)
                    this.writeBLECharacteristicValue(endbuffer, true)
                }, 50);
            },
            async sendBLEDataInChunks(hexStr) {
@@ -986,15 +1034,24 @@
                                    // });
                                    that.commandINST(that.ledData[that.ledCurrent]);
                                } else if (that.ledCurrent == that.ledTotal) {
                                    // uni.showLoading({
                                    //  title: '发送结束帧',
                                    //  mask: true
                                    // });
                                    // 所有数据帧已发送完成,发送结束帧,进度显示100%
                                    uni.showLoading({
                                        title: '发送中100%',
                                        mask: true
                                    });
                                    that.commandEnd()
                                } else {
                                    // 结束帧应答(ledCurrent == ledTotal + 1),全部发送完成,隐藏loading
                                    uni.hideLoading();
                                }
                            } else if (resName == 'DALL') {
                                // 清除所有信息指令回应:返回标志0x30工作无异常,0x31工作有异常
                                if (that.waitDallThenSend) {
                                    that.waitDallThenSend = false;
                                    // 收到DALL回应后,开始发送第一帧INST
                                    that.commandINST(that.ledData[that.ledCurrent]);
                                }
                            } else if (resName == 'POWR') {
                                // 查询电量
                                let capacityHex = result.slice(-8, -6); // "0a"