From 7be65a0428a4527889b6955c56aafdb81dda28a8 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期六, 22 十一月 2025 11:20:39 +0800
Subject: [PATCH] 开启状态线程,优化结束程序逻辑
---
src/chuankou/SerialPortConnectionDialog.java | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 54 insertions(+), 3 deletions(-)
diff --git a/src/chuankou/SerialPortConnectionDialog.java b/src/chuankou/SerialPortConnectionDialog.java
index 1f8d200..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);
});
@@ -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();
}
--
Gitblit v1.9.3