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 | 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