From cb2a01ee7dea28a2661720060b03c41dc372acb5 Mon Sep 17 00:00:00 2001
From: yaowanxin <3588231647@qq.com>
Date: 星期五, 01 八月 2025 11:53:16 +0800
Subject: [PATCH] 完善串口处理数据,数据选择

---
 src/utils/connect.js |   99 +++++++++++++++++++++++++++++++++++++------------
 1 files changed, 74 insertions(+), 25 deletions(-)

diff --git a/src/utils/connect.js b/src/utils/connect.js
index 39edfd6..251a15e 100644
--- a/src/utils/connect.js
+++ b/src/utils/connect.js
@@ -1,10 +1,30 @@
+import request from '@/utils/request';
 // 鏂板锛氬瓨鍌ㄥ叏灞�涓插彛鍜岃鍙栧櫒瀹炰緥
     let port = null;
     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;
+      }
+    }
 
     async function connect() {
-      if ("serial" in navigator) {
         try {
           port = await navigator.serial.requestPort(); // 淇敼涓哄叏灞�鍙橀噺
           await port.open({ baudRate: 9600 });
@@ -28,16 +48,36 @@
         } catch (error) {
           console.error("涓插彛閿欒:", error);
         }
-      }
-
+    }
       function processAccumulatedData(data) {
+
         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);
-            handleMessage(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('鍙戦�佸け璐ワ紝淇濈暀鏁版嵁寰呴噸璇�');
+                });
+              }
+            }
           }
 
           accumulatedData = messages[messages.length - 1];
@@ -45,29 +85,38 @@
 
         // 鍏朵粬娑堟伅鎷嗗垎绀轰緥淇濇寔涓嶅彉
         /*
-  while (accumulatedData.length >= 32) {
-    const message = accumulatedData.substring(0, 32);
-    accumulatedData = accumulatedData.substring(32);
-    handleMessage(message);
-  }
-  */
+        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;
-  }
-  */
+        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) {
+      // 鍖归厤鏁板�奸儴鍒嗭紝鍙鐞嗘璐熷彿
+      const match = message.match(/-?\d+\.?\d*/); 
+      if (match) {
+        return parseFloat(match[0]);
+      }
+      return NaN;
     }
 
     // 鏂板锛氬叧闂繛鎺ュ嚱鏁�
@@ -93,4 +142,4 @@
       }
     }
 
-    export { connect, disconnect };
\ No newline at end of file
+    export { connect, disconnect,weightList };
\ No newline at end of file

--
Gitblit v1.9.3