| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | private SerialPortService serialService; |
| | | |
| | | // 控制变量 |
| | | private boolean autoScroll = true; // 默认自动滚动 |
| | | private boolean autoScroll =false; // 默认自动滚动 |
| | | private static boolean dataUpdateEnabled = true; // 控制数据更新 |
| | | |
| | | // 日期格式化 |
| | |
| | | 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("清空数据"); |
| | |
| | | clearButton.addActionListener(e -> clearData()); |
| | | |
| | | buttonPanel.add(scrollButton); |
| | | buttonPanel.add(pollingButton); |
| | | buttonPanel.add(clearButton); |
| | | |
| | | return buttonPanel; |
| | |
| | | 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(); |
| | |
| | | dataTextArea.setText(sb.toString()); |
| | | } |
| | | } catch (Exception e) { |
| | | System.err.println("修剪行数时发生错误: " + e.getMessage()); |
| | | Errlog.logOperation("修剪行数时发生错误: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | staticDataTextArea.setText(sb.toString()); |
| | | } |
| | | } catch (Exception e) { |
| | | System.err.println("修剪行数时发生错误: " + e.getMessage()); |
| | | Errlog.logOperation("修剪行数时发生错误: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | System.err.println("显示数据时发生错误: " + e.getMessage()); |
| | | Errlog.logOperation("显示数据时发生错误: " + e.getMessage()); |
| | | } |
| | | }); |
| | | } |
| | |
| | | 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()); |
| | | } |
| | | }); |
| | | } |
| | |
| | | |
| | | // 如果内存使用超过80%,建议清理 |
| | | if (usedMemory > maxMemory * 0.8) { |
| | | System.out.println("内存使用警告: " + usedMemory + "MB/" + maxMemory + "MB"); |
| | | //System.out.println("内存使用警告: " + usedMemory + "MB/" + maxMemory + "MB"); |
| | | // 可选:自动清理旧数据 |
| | | if (dataTextArea.getLineCount() > MAX_DISPLAY_LINES) { |
| | | clearData(); |
| | |
| | | memoryMonitorTimer.stop(); |
| | | } |
| | | |
| | | // 关闭时停止轮询 |
| | | if (lunxun.isPolling()) { |
| | | lunxun.stopPolling(); |
| | | } |
| | | // 重要修改:不要停止轮询,只暂停调试输出 |
| | | lunxun.setDEBUG_ENABLED(false); |
| | | |
| | | // 停止串口数据捕获 |
| | | // 重要修改:不要停止串口数据捕获,只移除自己的回调 |
| | | if (serialService != null) { |
| | | serialService.stopCapture(); |
| | | // 使用新的方法移除回调而不停止整个捕获 |
| | | removeDataReceivedCallback(); |
| | | } |
| | | |
| | | // 清理静态引用,防止内存泄漏 |
| | |
| | | |
| | | 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) { |
| | | } |
| | | } |