张世豪
5 小时以前 a6077217e25f5804027194a5c2848e773eda1abd
src/xitongshezhi/SystemDebugDialog.java
@@ -10,6 +10,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import chushihua.lunxun;
import dialog.Errlog;
public class SystemDebugDialog extends JDialog {
    private static final long serialVersionUID = -9131186661220052051L;
@@ -25,11 +26,9 @@
    private static final Color WARNING_COLOR = new Color(243, 156, 18);
    private static final Color DARK_COLOR = new Color(15, 28, 48);
    private static final Color TEXT_COLOR = new Color(224, 224, 224);
    // UI组件
 // UI组件
    private JTextArea dataTextArea;
    private JButton clearButton;
    private JButton pollingButton;
    private JButton scrollButton;
    private JTextField sendTextField;
    private JButton sendButton;
@@ -43,7 +42,7 @@
    private SerialPortService serialService;
    
    // 控制变量
    private boolean autoScroll = true; // 默认自动滚动
    private boolean autoScroll =false; // 默认自动滚动
    private static boolean dataUpdateEnabled = true; // 控制数据更新
    
    // 日期格式化
@@ -214,14 +213,6 @@
        scrollButton.setBorder(BorderFactory.createEmptyBorder(8, 20, 8, 20));
        scrollButton.addActionListener(e -> toggleAutoScroll());
        
        // 启动/关闭查询按钮
        pollingButton = new JButton("启动查询");
        pollingButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        pollingButton.setBackground(SECONDARY_COLOR);
        pollingButton.setForeground(Color.WHITE);
        pollingButton.setFocusPainted(false);
        pollingButton.setBorder(BorderFactory.createEmptyBorder(8, 20, 8, 20));
        pollingButton.addActionListener(e -> togglePolling());
        
        // 清空按钮
        clearButton = new JButton("清空数据");
@@ -233,7 +224,6 @@
        clearButton.addActionListener(e -> clearData());
        
        buttonPanel.add(scrollButton);
        buttonPanel.add(pollingButton);
        buttonPanel.add(clearButton);
        
        return buttonPanel;
@@ -331,40 +321,22 @@
        staticDataUpdateEnabled = dataUpdateEnabled; // 同步静态变量
        if (autoScroll) {
            scrollButton.setText("暂停");
            scrollButton.setText("开始");
            scrollButton.setBackground(PRIMARY_COLOR);
            if (staticDataTextArea != null) {
                staticDataTextArea.setCaretPosition(staticDataTextArea.getDocument().getLength());
            }
            appendAsciiData("[" + getCurrentTime() + "] 开始显示数据\n");
            lunxun.DEBUG_ENABLED=true;
        } else {
            scrollButton.setText("开始");
            scrollButton.setText("暂停");
            scrollButton.setBackground(WARNING_COLOR);
            appendAsciiData("[" + getCurrentTime() + "] 暂停显示数据\n");
        }
    }
    
    private void togglePolling() {
        if (lunxun.isPolling()) {
            // 如果正在轮询,则停止
            if (lunxun.stopPolling()) {
                pollingButton.setText("启动查询");
                pollingButton.setBackground(SECONDARY_COLOR);
                onDataReceivedascii("[" + getCurrentTime() + "] stop\n");
                lunxun.setDEBUG_ENABLED(false);
            }
        } else {
            // 如果未轮询,则启动
            if (lunxun.startPolling()) {
               lunxun.setDEBUG_ENABLED(true);
                pollingButton.setText("停止查询");
                pollingButton.setBackground(DANGER_COLOR);
                onDataReceivedascii("[" + getCurrentTime() + "] strat\n");
            } else {
                showMessage("错误", "无法启动轮询查询,请检查串口连接", "error");
            }
        }
    }
    
    private void sendData() {
        String text = sendTextField.getText().trim();
@@ -418,7 +390,7 @@
                dataTextArea.setText(sb.toString());
            }
        } catch (Exception e) {
            System.err.println("修剪行数时发生错误: " + e.getMessage());
            Errlog.logOperation("修剪行数时发生错误: " + e.getMessage());
        }
    }
    
@@ -479,7 +451,7 @@
                staticDataTextArea.setText(sb.toString());
            }
        } catch (Exception e) {
            System.err.println("修剪行数时发生错误: " + e.getMessage());
            Errlog.logOperation("修剪行数时发生错误: " + e.getMessage());
        }
    }
@@ -511,7 +483,7 @@
                }
            } catch (Exception e) {
                System.err.println("显示数据时发生错误: " + e.getMessage());
                Errlog.logOperation("显示数据时发生错误: " + e.getMessage());
            }
        });
    }
@@ -531,14 +503,14 @@
                    displayText = displayText.substring(0, MAX_LINE_LENGTH) + "...";
                }
                staticDataTextArea.append(displayText);
                staticDataTextArea.append(displayText+"\n");
                if (staticDataUpdateEnabled) {
                    staticDataTextArea.setCaretPosition(staticDataTextArea.getDocument().getLength());
                }
            } catch (Exception e) {
                System.err.println("显示ASCII数据时发生错误: " + e.getMessage());
                Errlog.logOperation("显示ASCII数据时发生错误: " + e.getMessage());
            }
        });
    }
@@ -616,14 +588,13 @@
            memoryMonitorTimer.stop();
        }
        
        // 关闭时停止轮询
        if (lunxun.isPolling()) {
            lunxun.stopPolling();
        }
        // 重要修改:不要停止轮询,只暂停调试输出
        lunxun.setDEBUG_ENABLED(false);
        
        // 停止串口数据捕获
        // 重要修改:不要停止串口数据捕获,只移除自己的回调
        if (serialService != null) {
            serialService.stopCapture();
            // 使用新的方法移除回调而不停止整个捕获
            removeDataReceivedCallback();
        }
        
        // 清理静态引用,防止内存泄漏
@@ -634,4 +605,21 @@
        
        super.dispose();
    }
    // 新增:移除数据接收回调的方法
    private void removeDataReceivedCallback() {
        if (serialService != null) {
            try {
                // 通过反射或其他方式移除回调,或者简单地设置为null
                // 这里假设SerialPortService有移除回调的方法
                serialService.setResponseConsumer(null);
            } catch (Exception e) {
                Errlog.logOperation("移除数据接收回调时发生错误: " + e.getMessage());
            }
        }
    }
    // 新增:启用/禁用轮询控制的方法
    public static void setPollingControlEnabled(boolean enabled) {
    }
}