From af3f9aac7a437875da00aaffc628ff38dfc63e8f Mon Sep 17 00:00:00 2001
From: zouyu <2723363702@qq.com>
Date: 星期五, 25 七月 2025 17:14:09 +0800
Subject: [PATCH] Merge branch 'dev_ztns' into dev_y_ztns

---
 src/utils/connect.js |   96 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 96 insertions(+), 0 deletions(-)

diff --git a/src/utils/connect.js b/src/utils/connect.js
new file mode 100644
index 0000000..39edfd6
--- /dev/null
+++ b/src/utils/connect.js
@@ -0,0 +1,96 @@
+// 鏂板锛氬瓨鍌ㄥ叏灞�涓插彛鍜岃鍙栧櫒瀹炰緥
+    let port = null;
+    let reader = null;
+    let accumulatedData = ""; // 绉诲埌鍏ㄥ眬锛屼究浜庡叧闂椂閲嶇疆
+
+    async function connect() {
+      if ("serial" in navigator) {
+        try {
+          port = await navigator.serial.requestPort(); // 淇敼涓哄叏灞�鍙橀噺
+          await port.open({ baudRate: 9600 });
+
+          reader = port.readable.getReader(); // 淇敼涓哄叏灞�鍙橀噺
+          const decoder = new TextDecoder();
+          accumulatedData = ""; // 閲嶇疆绱Н鏁版嵁
+
+          console.log("涓插彛杩炴帴鎴愬姛锛屽紑濮嬫帴鏀舵暟鎹�...");
+
+          while (true) {
+            const { value, done } = await reader.read();
+            if (done) break;
+
+            const decodedChunk = decoder.decode(value, { stream: true });
+            accumulatedData += decodedChunk;
+            console.log("鎺ユ敹鍒版暟鎹潡:", decodedChunk);
+
+            processAccumulatedData(accumulatedData);
+          }
+        } 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);
+          }
+
+          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;
+  }
+  */
+      }
+    }
+
+    // 鏂板锛氬叧闂繛鎺ュ嚱鏁�
+    async function disconnect() {
+      if (!port || !reader) {
+        console.log("鏈缓绔嬭繛鎺ワ紝鏃犻渶鍏抽棴");
+        return;
+      }
+
+      try {
+        // 鍏堝彇娑堣鍙栧櫒
+        await reader.cancel();
+        // 鍐嶅叧闂覆鍙�
+        await port.close();
+        console.log("涓插彛杩炴帴宸插叧闂�");
+
+        // 閲嶇疆鍏ㄥ眬鍙橀噺
+        port = null;
+        reader = null;
+        accumulatedData = "";
+      } catch (error) {
+        console.error("鍏抽棴杩炴帴澶辫触:", error);
+      }
+    }
+
+    export { connect, disconnect };
\ No newline at end of file

--
Gitblit v1.9.3