|  |  | 
 |  |  |     let reader = null; | 
 |  |  |     let accumulatedData = ""; // 移到全局,便于关闭时重置 | 
 |  |  |     let weightList = []; // 用于存储重量值的列表 | 
 |  |  |  | 
 |  |  |     async function sendWeightsToBackend(weights) { | 
 |  |  |       try { | 
 |  |  |         // 使用 await 等待 request 函数返回结果 | 
 |  |  |         const response = await request({ | 
 |  |  |           url: "/weight/handleWeights", | 
 |  |  |           method: "POST", | 
 |  |  |           data: weights, // 通常 request 函数使用 data 字段传递数据 | 
 |  |  |         }); | 
 |  |  |         // 根据 request 函数的实现判断响应是否成功 | 
 |  |  |         if (response.code !== 200) { // 假设 response 有 code 字段,200 表示成功 | 
 |  |  |           throw new Error(`Backend response error: ${response.message}`); | 
 |  |  |         } | 
 |  |  |         return response.data; // 假设响应数据在 data 字段中 | 
 |  |  |       } catch (error) { | 
 |  |  |         console.error('发送重量数据到后端失败:', error); | 
 |  |  |         throw error; | 
 |  |  |       } | 
 |  |  |     export function sendWeightsToBackend(weights) { | 
 |  |  |       return request({ | 
 |  |  |         url: "/weight/handleWeights", | 
 |  |  |         method: "POST", | 
 |  |  |         data: weights, | 
 |  |  |       }); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     async function connect() { | 
 |  |  | 
 |  |  |           console.error("串口错误:", error); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |       function processAccumulatedData(data) { | 
 |  |  |     function processAccumulatedData(data) { | 
 |  |  |  | 
 |  |  |         if (data.includes("\n")) { | 
 |  |  |           const messages = data.split("\n"); | 
 |  |  |       if (data.includes("\n")) { | 
 |  |  |         const messages = data.split("\n"); | 
 |  |  |  | 
 |  |  |           for (let i = 0; i < messages.length - 1; i++) { | 
 |  |  |             const completeMessage = messages[i]; | 
 |  |  |             console.log("完整消息:", completeMessage); | 
 |  |  |             // 提取数值 | 
 |  |  |             const weightValue = extractWeightValue(completeMessage); | 
 |  |  |             if (!isNaN(weightValue)) { | 
 |  |  |               console.log("提取的重量值:", weightValue);  | 
 |  |  |               // 将有效重量值添加到列表 | 
 |  |  |               weightList.push(weightValue); | 
 |  |  |         for (let i = 0; i < messages.length - 1; i++) { | 
 |  |  |           const completeMessage = messages[i]; | 
 |  |  |           console.log("完整消息:", completeMessage); | 
 |  |  |           // 提取数值 | 
 |  |  |           const weightValue = extractWeightValue(completeMessage); | 
 |  |  |           if (!isNaN(weightValue)) { | 
 |  |  |             console.log("提取的重量值:", weightValue);  | 
 |  |  |             // 将有效重量值添加到列表 | 
 |  |  |             weightList.push(weightValue); | 
 |  |  |  | 
 |  |  |               // 当列表长度达到 6 时,发送数据到后端并清空列表 | 
 |  |  |               if (weightList.length === 6) {  | 
 |  |  |                 sendWeightsToBackend({ | 
 |  |  |                   weights: weightList | 
 |  |  |                 }) | 
 |  |  |                 .then(() => { | 
 |  |  |                   console.log('数据发送成功'); | 
 |  |  |                   weightList = []; | 
 |  |  |                 }) | 
 |  |  |                 .catch(() => { | 
 |  |  |                   console.log('发送失败,保留数据待重试'); | 
 |  |  |                 }); | 
 |  |  |               } | 
 |  |  |             // 当列表长度达到 6 时,发送数据到后端并清空列表 | 
 |  |  |             if (weightList.length === 6) {  | 
 |  |  |               sendWeightsToBackend({ | 
 |  |  |                 weights: weightList | 
 |  |  |               }) | 
 |  |  |               .then(() => { | 
 |  |  |                 console.log('数据发送成功'); | 
 |  |  |                 weightList = []; | 
 |  |  |               }) | 
 |  |  |               .catch(() => { | 
 |  |  |                 console.log('发送失败,保留数据待重试'); | 
 |  |  |               }); | 
 |  |  |             } | 
 |  |  |           } | 
 |  |  |  | 
 |  |  |           accumulatedData = messages[messages.length - 1]; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 其他消息拆分示例保持不变 | 
 |  |  |         /* | 
 |  |  |         while (accumulatedData.length >= 32) { | 
 |  |  |           const message = accumulatedData.substring(0, 32); | 
 |  |  |           accumulatedData = accumulatedData.substring(32); | 
 |  |  |           handleMessage(message); | 
 |  |  |         } | 
 |  |  |         */ | 
 |  |  |  | 
 |  |  |         /* | 
 |  |  |         let startIndex = 0; | 
 |  |  |         while (true) { | 
 |  |  |           const start = accumulatedData.indexOf(0xAA, startIndex); | 
 |  |  |           if (start === -1) break; | 
 |  |  |            | 
 |  |  |           const end = accumulatedData.indexOf(0x55, start + 1); | 
 |  |  |           if (end === -1) break; | 
 |  |  |            | 
 |  |  |           const message = accumulatedData.substring(start, end + 1); | 
 |  |  |           accumulatedData = accumulatedData.substring(end + 1); | 
 |  |  |           handleMessage(message); | 
 |  |  |           startIndex = start; | 
 |  |  |         } | 
 |  |  |         */ | 
 |  |  |         accumulatedData = messages[messages.length - 1]; | 
 |  |  |       } | 
 |  |  |  | 
 |  |  |       // 其他消息拆分示例保持不变 | 
 |  |  |       /* | 
 |  |  |       while (accumulatedData.length >= 32) { | 
 |  |  |         const message = accumulatedData.substring(0, 32); | 
 |  |  |         accumulatedData = accumulatedData.substring(32); | 
 |  |  |         handleMessage(message); | 
 |  |  |       } | 
 |  |  |       */ | 
 |  |  |  | 
 |  |  |       /* | 
 |  |  |       let startIndex = 0; | 
 |  |  |       while (true) { | 
 |  |  |         const start = accumulatedData.indexOf(0xAA, startIndex); | 
 |  |  |         if (start === -1) break; | 
 |  |  |          | 
 |  |  |         const end = accumulatedData.indexOf(0x55, start + 1); | 
 |  |  |         if (end === -1) break; | 
 |  |  |          | 
 |  |  |         const message = accumulatedData.substring(start, end + 1); | 
 |  |  |         accumulatedData = accumulatedData.substring(end + 1); | 
 |  |  |         handleMessage(message); | 
 |  |  |         startIndex = start; | 
 |  |  |       } | 
 |  |  |       */ | 
 |  |  |     } | 
 |  |  |      | 
 |  |  |     // 新增提取重量数值的方法 | 
 |  |  |     function extractWeightValue(message) { | 
 |  |  |       // 匹配数值部分,可处理正负号 |