张世豪
21 小时以前 0f1509097818fbf48d7741d0fe0d9b973e88730d
src/chuankou/SerialPortConnectionDialog.java
@@ -4,17 +4,16 @@
import java.awt.*;
import com.fazecast.jSerialComm.SerialPort;
import chushihua.Chushihua;
import home.CardMachineUI;
/**
 * 串口连接对话框
 * 软件启动时显示,用于选择串口和波特率并连接
 */
@SuppressWarnings("serial")
public class SerialPortConnectionDialog extends JDialog {
    
    // 颜色常量
    private static final Color PRIMARY_COLOR = new Color(52, 152, 219);
    private static final Color PRIMARY_DARK_COLOR = new Color(41, 128, 185);
    private static final Color SECONDARY_COLOR = new Color(46, 204, 113);
    private static final Color DANGER_COLOR = new Color(231, 76, 60);
    private static final Color DARK_COLOR = new Color(255, 69, 0); // 改为橘黄色
@@ -184,6 +183,8 @@
        exitButton.setFocusPainted(false);
        exitButton.setBorder(BorderFactory.createEmptyBorder(8, 20, 8, 20));
        exitButton.addActionListener(e -> {
            // 新增:退出前确保关闭串口连接
            cleanupSerialPort();
            System.exit(0);
        });
        
@@ -280,13 +281,13 @@
                        // 启动协议解析器
                        if (serialService.getProtocolParser() != null) {
                            serialService.getProtocolParser().start();
                            System.out.println("串口协议解析器已启动");
                            //System.out.println("串口协议解析器已启动");
                        }
                        
                        // 启动数据捕获并启用调试输出
                        serialService.enableDebugOutput();
                        serialService.startCapture(data -> {
                            // 这里会触发SerialPortService中的System.out.println打印
                            // 这里会触发SerialPortService中的//System.out.println打印
                        });
                        
                        // 重要修改:移除自动创建主界面的代码
@@ -304,14 +305,63 @@
                        });
                        
                    } else {
                        // 连接失败处理保持不变...
                        // 连接失败
                        connectButton.setEnabled(true);
                        connectButton.setText("连接串口");
                        connectButton.setBackground(SECONDARY_COLOR);
                        statusLabel.setText("串口连接失败: " + portName);
                        showMessage("连接失败", "无法连接到串口 " + portName, "error");
                        // 关闭失败的串口连接
                        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");
                    // 关闭异常的串口连接
                    if (serialService != null) {
                        serialService.close();
                        serialService = null;
                    }
                });
            }
        }).start();
    }
    /**
     * 新增:清理串口资源的方法
     */
    private void cleanupSerialPort() {
        if (serialService != null) {
            try {
                // 停止数据捕获
                serialService.stopCapture();
                // 停止协议解析器
                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) {
@@ -352,7 +402,7 @@
    public void dispose() {
        // 如果连接失败,关闭串口
        if (!isConnected && serialService != null) {
            serialService.close();
            cleanupSerialPort();
        }
        super.dispose();
    }
@@ -381,7 +431,7 @@
                        
                        // 重要修改:移除自动打开主界面的代码
                        // 由Homein统一管理主界面的创建和显示
                        System.out.println("串口连接成功,准备返回控制权给主程序");
                        //System.out.println("串口连接成功,准备返回控制权给主程序");
                        
                        // 只需要关闭对话框,不创建主界面
                        // 主界面将在Homein.showMainInterface()中创建