张世豪
21 小时以前 0f1509097818fbf48d7741d0fe0d9b973e88730d
src/home/CardMachineUI.java
@@ -87,7 +87,14 @@
           initializeUI();
           initializeSlots();
           startUIUpdates(); // UI刷新定时器
           startSerialStatusMonitoring(); // 串口状态监控
           startSerialStatusMonitoring(); // 串口状态监控
           setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
           addWindowListener(new java.awt.event.WindowAdapter() {
               @Override
               public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                   dispose(); // 调用我们修改过的dispose方法
               }
           });
           
           //System.out.println("主界面初始化完成");
       } catch (Exception e) {
@@ -174,7 +181,7 @@
    }
   private void initializeUI() {
      setTitle("智能人脸发卡机管理系统");
      setTitle("发卡机控制软件V1.0");
      setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
@@ -596,34 +603,67 @@
   }
   
   public void dispose() {
      // 停止UI刷新定时器
      if (uiUpdateTimer != null) {
         uiUpdateTimer.stop();
      }
      // 停止串口状态监控
      if (serialStatusTimer != null) {
         serialStatusTimer.stop();
      }
      // 停止轮询查询
      if (lunxun.isPolling()) {
         lunxun.stopPolling();
         //System.out.println("应用程序关闭,轮询查询已停止");
      }
      // 停止串口协议解析器(新增)
      if (serialProtocolParser != null) {
         serialProtocolParser.stop();
      }
      // 停止串口服务
      if (serialPortService != null) {
         serialPortService.stopCapture();
         serialPortService.close();
      }
      super.dispose();
      int result = JOptionPane.showConfirmDialog(this,
             "确定要退出程序吗?",
             "确认退出",
             JOptionPane.YES_NO_OPTION);
         if (result != JOptionPane.YES_OPTION) {
             return; // 用户取消退出
         }
       // 停止UI刷新定时器
       if (uiUpdateTimer != null) {
           uiUpdateTimer.stop();
       }
       // 停止串口状态监控
       if (serialStatusTimer != null) {
           serialStatusTimer.stop();
       }
       // 停止轮询查询
       if (lunxun.isPolling()) {
           lunxun.stopPolling();
           //System.out.println("应用程序关闭,轮询查询已停止");
       }
       // 停止串口协议解析器(新增)
       if (serialProtocolParser != null) {
           serialProtocolParser.stop();
       }
       // 停止串口服务
       if (serialPortService != null) {
           serialPortService.stopCapture();
           serialPortService.close();
       }
       super.dispose();
       // 新增:确保程序完全退出前释放单实例锁
       try {
           // 通过反射调用 Homein 的清理方法
           Class<?> homeinClass = Class.forName("home.Homein");
           java.lang.reflect.Method cleanupMethod = homeinClass.getDeclaredMethod("cleanupSingleInstanceLock");
           cleanupMethod.setAccessible(true);
           cleanupMethod.invoke(null);
           //System.out.println("单实例锁已释放");
       } catch (Exception e) {
           System.err.println("释放单实例锁时发生异常: " + e.getMessage());
           // 如果反射失败,尝试直接关闭端口
           try {
               java.net.ServerSocket socket = new java.net.ServerSocket();
               socket.setReuseAddress(true);
               socket.bind(new java.net.InetSocketAddress(9999));
               socket.close();
               //System.out.println("通过直接绑定方式释放端口");
           } catch (Exception ex) {
               System.err.println("直接释放端口也失败: " + ex.getMessage());
           }
       }
       // 新增:确保程序完全退出
       System.exit(0);
   }
   
   /**
@@ -661,10 +701,10 @@
   public boolean performCardPickup(int slotId) {
      try {
         // 调用OpenDoor生成开门指令
         String command = OpenDoor.openOneDoor(slotId, OpenDoor.TYPE_ISSUE_CARD);
//         String command = OpenDoor.openOneDoor(slotId, OpenDoor.TYPE_ISSUE_CARD);
         // 使用Sendmsg发送指令到串口
         boolean sendResult = Sendmsg.sendMessage(command);
         boolean sendResult = Sendmsg.opendoorzhiling(slotId, 2);
         if (sendResult) {            
            return true;