From 0f1509097818fbf48d7741d0fe0d9b973e88730d Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期六, 22 十一月 2025 17:46:43 +0800
Subject: [PATCH] 新增人脸检测暂停有卡卡槽查询

---
 src/chuankou/SerialPortConnectionDialog.java |   63 ++++++++++++++++++++++++++++---
 1 files changed, 57 insertions(+), 6 deletions(-)

diff --git a/src/chuankou/SerialPortConnectionDialog.java b/src/chuankou/SerialPortConnectionDialog.java
index d84ef12..764c1b5 100644
--- a/src/chuankou/SerialPortConnectionDialog.java
+++ b/src/chuankou/SerialPortConnectionDialog.java
@@ -183,6 +183,8 @@
         exitButton.setFocusPainted(false);
         exitButton.setBorder(BorderFactory.createEmptyBorder(8, 20, 8, 20));
         exitButton.addActionListener(e -> {
+            // 鏂板锛氶��鍑哄墠纭繚鍏抽棴涓插彛杩炴帴
+            cleanupSerialPort();
             System.exit(0);
         });
         
@@ -279,13 +281,13 @@
                         // 鍚姩鍗忚瑙f瀽鍣�
                         if (serialService.getProtocolParser() != null) {
                             serialService.getProtocolParser().start();
-                            System.out.println("涓插彛鍗忚瑙f瀽鍣ㄥ凡鍚姩");
+                            //System.out.println("涓插彛鍗忚瑙f瀽鍣ㄥ凡鍚姩");
                         }
                         
                         // 鍚姩鏁版嵁鎹曡幏骞跺惎鐢ㄨ皟璇曡緭鍑�
                         serialService.enableDebugOutput();
                         serialService.startCapture(data -> {
-                            // 杩欓噷浼氳Е鍙慡erialPortService涓殑System.out.println鎵撳嵃
+                            // 杩欓噷浼氳Е鍙慡erialPortService涓殑//System.out.println鎵撳嵃
                         });
                         
                         // 閲嶈淇敼锛氱Щ闄よ嚜鍔ㄥ垱寤轰富鐣岄潰鐨勪唬鐮�
@@ -303,14 +305,63 @@
                         });
                         
                     } else {
-                        // 杩炴帴澶辫触澶勭悊淇濇寔涓嶅彉...
+                        // 杩炴帴澶辫触
+                        connectButton.setEnabled(true);
+                        connectButton.setText("杩炴帴涓插彛");
+                        connectButton.setBackground(SECONDARY_COLOR);
+                        statusLabel.setText("涓插彛杩炴帴澶辫触: " + portName);
+                        showMessage("杩炴帴澶辫触", "鏃犳硶杩炴帴鍒颁覆鍙� " + portName, "error");
+                        
+                        // 鍏抽棴澶辫触鐨勪覆鍙h繛鎺�
+                        if (serialService != null) {
+                            serialService.close();
+                            serialService = null;
+                        }
                     }
                 });
                 
             } catch (Exception e) {
-                // 寮傚父澶勭悊淇濇寔涓嶅彉...
+                SwingUtilities.invokeLater(() -> {
+                    connectButton.setEnabled(true);
+                    connectButton.setText("杩炴帴涓插彛");
+                    connectButton.setBackground(SECONDARY_COLOR);
+                    statusLabel.setText("杩炴帴寮傚父: " + e.getMessage());
+                    showMessage("杩炴帴寮傚父", "涓插彛杩炴帴杩囩▼涓彂鐢熼敊璇�: " + e.getMessage(), "error");
+                    
+                    // 鍏抽棴寮傚父鐨勪覆鍙h繛鎺�
+                    if (serialService != null) {
+                        serialService.close();
+                        serialService = null;
+                    }
+                });
             }
         }).start();
+    }
+    
+    /**
+     * 鏂板锛氭竻鐞嗕覆鍙h祫婧愮殑鏂规硶
+     */
+    private void cleanupSerialPort() {
+        if (serialService != null) {
+            try {
+                // 鍋滄鏁版嵁鎹曡幏
+                serialService.stopCapture();
+                
+                // 鍋滄鍗忚瑙f瀽鍣�
+                if (serialService.getProtocolParser() != null) {
+                    serialService.getProtocolParser().stop();
+                }
+                
+                // 鍏抽棴涓插彛
+                serialService.close();
+                
+                //System.out.println("涓插彛杩炴帴宸叉竻鐞�");
+            } catch (Exception e) {
+                System.err.println("娓呯悊涓插彛璧勬簮鏃跺彂鐢熷紓甯�: " + e.getMessage());
+            } finally {
+                serialService = null;
+            }
+        }
     }
     
     private void showMessage(String title, String message, String type) {
@@ -351,7 +402,7 @@
     public void dispose() {
         // 濡傛灉杩炴帴澶辫触锛屽叧闂覆鍙�
         if (!isConnected && serialService != null) {
-            serialService.close();
+            cleanupSerialPort();
         }
         super.dispose();
     }
@@ -380,7 +431,7 @@
                         
                         // 閲嶈淇敼锛氱Щ闄よ嚜鍔ㄦ墦寮�涓荤晫闈㈢殑浠g爜
                         // 鐢盚omein缁熶竴绠$悊涓荤晫闈㈢殑鍒涘缓鍜屾樉绀�
-                        System.out.println("涓插彛杩炴帴鎴愬姛锛屽噯澶囪繑鍥炴帶鍒舵潈缁欎富绋嬪簭");
+                        //System.out.println("涓插彛杩炴帴鎴愬姛锛屽噯澶囪繑鍥炴帶鍒舵潈缁欎富绋嬪簭");
                         
                         // 鍙渶瑕佸叧闂璇濇锛屼笉鍒涘缓涓荤晫闈�
                         // 涓荤晫闈㈠皢鍦℉omein.showMainInterface()涓垱寤�

--
Gitblit v1.9.3