From 100f4dcea20a32663a07e91525de111f7515eb79 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 21 十一月 2025 17:25:53 +0800
Subject: [PATCH] 可发布版本202517.25

---
 src/chushihua/lunxun.java | 1393 +++++++++++++++++++++++++++++++--------------------------
 1 files changed, 760 insertions(+), 633 deletions(-)

diff --git a/src/chushihua/lunxun.java b/src/chushihua/lunxun.java
index f7a66ee..83df31c 100644
--- a/src/chushihua/lunxun.java
+++ b/src/chushihua/lunxun.java
@@ -1,9 +1,10 @@
 package chushihua;
 
 import chuankou.Sendmsg;
+import dialog.Errlog;
+import home.Fkj;
 import home.MachineConfig;
 import publicway.QueryData;
-import xitongshezhi.Fkj;
 import xitongshezhi.SystemDebugDialog;
 
 import java.util.Map;
@@ -14,640 +15,766 @@
  * 杞鏌ヨ绫� - 浼樺寲鐗堟湰
  * 鐢ㄤ簬瀹氭椂鍚戞墍鏈夊崱妲藉彂閫佹煡璇㈡寚浠�
  * 鏀寔鏆傚仠鍜屾仮澶嶅姛鑳斤紝妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
+ * 鏂板锛氫笉鍚屽崱妲界姸鎬佷娇鐢ㄤ笉鍚屾煡璇㈤鐜�
  */
 public class lunxun {
-    private static volatile boolean isRunning = false;
-    private static volatile boolean isPaused = false;
-    private static final AtomicBoolean shouldStop = new AtomicBoolean(false);
-    private static Thread pollingThread;
-    private static int pollingInterval = 100; // 榛樿杞闂撮殧
-    
-    // 鍗℃Ы鐩稿叧甯搁噺
-    private static final int MIN_SLOT = 1;
-    private static final int MAX_SLOT = 60;
-    
-    // 涓插彛杩炴帴妫�鏌ョ浉鍏�
-    private static final int SERIAL_CHECK_INTERVAL = 5000; // 5绉掓鏌ヤ竴娆′覆鍙h繛鎺�
-    private static long lastSerialCheckTime = 0;
-    private static boolean serialConnected = false;
-    
-    // 鎬ц兘浼樺寲锛氭煡璇㈡寚浠ょ紦瀛�
-    private static final Map<Integer, String> queryCommandCache = new ConcurrentHashMap<>();
-    
-    // 璋冭瘯妯″紡鎺у埗
-    public static  boolean DEBUG_ENABLED = false;
-    
-    /**
-     * 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬� - 浼樺寲鐗堟湰锛屾坊鍔犻噸璇曟満鍒�
-     * @return true-涓插彛宸茶繛鎺�, false-涓插彛鏈繛鎺�
-     */
-    public static boolean checkSerialConnection() {
-        // 閬垮厤棰戠箒妫�鏌ワ紝姣�5绉掓鏌ヤ竴娆�
-        long currentTime = System.currentTimeMillis();
-        if (currentTime - lastSerialCheckTime < SERIAL_CHECK_INTERVAL) {
-            return serialConnected;
-        }
-        
-        lastSerialCheckTime = currentTime;
-        
-        try {
-            // 绠�鍖栨鏌ワ細鐩存帴妫�鏌endmsg鐨勪覆鍙g姸鎬侊紝鑰屼笉鏄彂閫佹祴璇曟寚浠�
-            boolean result = Sendmsg.isPortOpen();
-            
-            if (result) {
-                if (DEBUG_ENABLED) {
-                    //System.out.println("涓插彛杩炴帴姝e父");
-                }
-                serialConnected = true;
-            } else {
-                System.err.println("涓插彛杩炴帴澶辫触 - 涓插彛鏈墦寮�");
-                serialConnected = false;
-            }
-        } catch (Exception e) {
-            System.err.println("涓插彛杩炴帴妫�鏌ュ紓甯�: " + e.getMessage());
-            serialConnected = false;
-        }
-        
-        return serialConnected;
-    }
-    
-    /**
-     * 甯﹂噸璇曠殑涓插彛杩炴帴妫�鏌�
-     */
-    private static boolean checkSerialConnectionWithRetry() {
-        for (int i = 0; i < 3; i++) {
-            if (checkSerialConnection()) {
-                return true;
-            }
-            try {
-                Thread.sleep(1000);
-            } catch (InterruptedException e) {
-                Thread.currentThread().interrupt();
-                return false;
-            }
-        }
-        return false;
-    }
-    
-    /**
-     * 鍚姩杞鏌ヨ - 浼樺寲鐗堟湰
-     * @return true-鍚姩鎴愬姛, false-鍚姩澶辫触
-     */
-    public static boolean startPolling() {
-        if (isRunning) {
-            //System.out.println("杞鏌ヨ宸茬粡鍦ㄨ繍琛屼腑");
-            return true;
-        }
-        
-        // 鍚姩鍓嶄弗鏍兼鏌ヤ覆鍙h繛鎺�
-        if (!checkSerialConnectionWithRetry()) {
-            System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍚姩杞鏌ヨ");
-            return false;
-        }
-        
-        // 浠庨厤缃腑鑾峰彇杞闂撮殧
-        loadPollingIntervalFromConfig();
-        
-        isRunning = true;
-        isPaused = false;
-        shouldStop.set(false);
-        
-        try {
-            pollingThread = new Thread(new PollingTask(), "CardSlot-Polling-Thread");
-            pollingThread.setDaemon(true);
-            pollingThread.start();            
-            if (DEBUG_ENABLED) {
-                //System.out.println("杞鏌ヨ宸插惎鍔紝闂撮殧: " + pollingInterval + "ms");
-            }
-            return true;
-        } catch (Exception e) {
-            System.err.println("鍚姩杞鏌ヨ绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
-            isRunning = false;
-            shouldStop.set(true);
-            return false;
-        }
-    }
-    
-    /**
-     * 鍋滄杞鏌ヨ - 淇鐗堟湰
-     * @return true-鍋滄鎴愬姛, false-鍋滄澶辫触
-     */
-    public static boolean stopPolling() {
-        if (!isRunning) {
-            //System.out.println("杞鏌ヨ鏈湪杩愯");
-            return false;
-        }
-        
-        shouldStop.set(true);
-        isRunning = false;
-        isPaused = false;
-        
-        if (pollingThread != null) {
-            pollingThread.interrupt();
-            try {
-                pollingThread.join(3000); // 绛夊緟3绉�
-                // 妫�鏌ョ嚎绋嬫槸鍚﹁繕鍦ㄨ繍琛�
-                if (pollingThread.isAlive()) {
-                    System.err.println("杞绾跨▼鏈湪3绉掑唴鍋滄锛屾爣璁颁负瀹堟姢绾跨▼骞跺拷鐣�");
-                    // 涓嶅己鍒跺仠姝紝鑰屾槸纭繚瀹冩槸瀹堟姢绾跨▼
-                    pollingThread.setDaemon(true);
-                }
-            } catch (InterruptedException e) {
-                System.err.println("鍋滄杞鏌ヨ鏃惰涓柇: " + e.getMessage());
-                Thread.currentThread().interrupt();
-            } catch (Exception e) {
-                System.err.println("鍋滄杞绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
-            } finally {
-                pollingThread = null;
-            }
-        }
-        
-        shouldStop.set(false);
-        //System.out.println("杞鏌ヨ宸插仠姝�");
-        return true;
-    }
-    
-    /**
-     * 鏆傚仠杞鏌ヨ
-     * @return true-鏆傚仠鎴愬姛, false-鏆傚仠澶辫触
-     */
-    public static boolean pausePolling() {
-        if (!isRunning) {
-            if (DEBUG_ENABLED) {
-                //System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曟殏鍋�");
-            }
-            return false;
-        }
-        
-        if (isPaused) {
-            if (DEBUG_ENABLED) {
-                //System.out.println("杞鏌ヨ宸茬粡澶勪簬鏆傚仠鐘舵��");
-            }
-            return false;
-        }
-        
-        isPaused = true;
-        //System.out.println("杞鏌ヨ宸叉殏鍋�");
-        return true;
-    }
-    
-    /**
-     * 鎭㈠杞鏌ヨ
-     * @return true-鎭㈠鎴愬姛, false-鎭㈠澶辫触
-     */
-    public static boolean resumePolling() {
-        if (!isRunning) {
-            //System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曟仮澶�");
-            return false;
-        }
-        
-        if (!isPaused) {
-            //System.out.println("杞鏌ヨ鏈浜庢殏鍋滅姸鎬�");
-            return false;
-        }
-        
-        // 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
-        if (!checkSerialConnectionWithRetry()) {
-            System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
-            return false;
-        }
-        
-        isPaused = false;
-        synchronized (lunxun.class) {
-            lunxun.class.notifyAll(); // 鍞ら啋绛夊緟鐨勭嚎绋�
-        }
-        //System.out.println("杞鏌ヨ宸叉仮澶�");
-        return true;
-    }
-    
-    /**
-     * 妫�鏌ヨ疆璇㈢姸鎬�
-     * @return true-姝e湪杩愯, false-宸插仠姝�
-     */
-    public static boolean isPolling() {
-        return isRunning;
-    }
-    
-    /**
-     * 妫�鏌ユ槸鍚︽殏鍋�
-     * @return true-宸叉殏鍋�, false-鏈殏鍋�
-     */
-    public static boolean isPaused() {
-        return isPaused;
-    }
-    
-    /**
-     * 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
-     * @return true-涓插彛宸茶繛鎺�, false-涓插彛鏈繛鎺�
-     */
-    public static boolean isSerialConnected() {
-        return serialConnected;
-    }
-    
-    /**
-     * 璁剧疆杞闂撮殧
-     * @param interval 杞闂撮殧锛堟绉掞級
-     */
-    public static void setPollingInterval(int interval) {
-        if (interval < 10) {
-            System.err.println("杞闂撮殧涓嶈兘灏忎簬10ms");
-            return;
-        }
-        
-        pollingInterval = interval;
-        //System.out.println("杞闂撮殧宸茶缃负: " + interval + "ms");
-        
-        // 濡傛灉姝e湪杩愯锛岄噸鏂板惎鍔ㄤ互搴旂敤鏂扮殑闂撮殧
-        if (isRunning) {
-            restartPolling();
-        }
-    }
-    
-    /**
-     * 鑾峰彇褰撳墠杞闂撮殧
-     * @return 杞闂撮殧锛堟绉掞級
-     */
-    public static int getPollingInterval() {
-        return pollingInterval;
-    }
-    
-    /**
-     * 閲嶆柊鍚姩杞鏌ヨ
-     * @return true-閲嶅惎鎴愬姛, false-閲嶅惎澶辫触
-     */
-    public static boolean restartPolling() {
-        stopPolling();
-        
-        // 绛夊緟涓�灏忔鏃堕棿纭繚绾跨▼瀹屽叏鍋滄
-        try {
-            Thread.sleep(200);
-        } catch (InterruptedException e) {
-            Thread.currentThread().interrupt();
-        }
-        
-        return startPolling();
-    }
-    
-    /**
-     * 浠庨厤缃腑鍔犺浇杞闂撮殧
-     */
-    private static void loadPollingIntervalFromConfig() {
-        try {
-            Chushihua configSystem = Chushihua.getInstance();
-            if (configSystem.isInitialized()) {
-                MachineConfig machineConfig = configSystem.getMachineConfig();
-                pollingInterval = machineConfig.getPollingInterval();
-                //System.out.println("浠庨厤缃姞杞借疆璇㈤棿闅�: " + pollingInterval + "ms");
-            } else {
-                //System.out.println("閰嶇疆绯荤粺鏈垵濮嬪寲锛屼娇鐢ㄩ粯璁よ疆璇㈤棿闅�: " + pollingInterval + "ms");
-            }
-        } catch (Exception e) {
-            System.err.println("鍔犺浇杞闂撮殧閰嶇疆澶辫触: " + e.getMessage());
-            //System.out.println("浣跨敤榛樿杞闂撮殧: " + pollingInterval + "ms");
-        }
-    }
-    
-    /**
-     * 鑾峰彇缂撳瓨鐨勬煡璇㈡寚浠�
-     */
-    private static String getCachedQueryCommand(int slotNumber) {
-        return queryCommandCache.computeIfAbsent(slotNumber, QueryData::queryData);
-    }
-    
-    /**
-     * 娓呯┖鏌ヨ鎸囦护缂撳瓨锛堝綋鏌ヨ閫昏緫鍙樺寲鏃惰皟鐢級
-     */
-    public static void clearQueryCache() {
-        queryCommandCache.clear();
-        //System.out.println("鏌ヨ鎸囦护缂撳瓨宸叉竻绌�");
-    }
-    
-    /**
-     * 杞浠诲姟鍐呴儴绫� - 浼樺寲鐗堟湰
-     * 浼樺寲鍐呭瓨浣跨敤锛氶伩鍏嶅湪寰幆涓垱寤烘柊瀵硅薄
-     * 娣诲姞鎵归噺澶勭悊鍜屾�ц兘鐩戞帶
-     */
-    private static class PollingTask implements Runnable {
-        private int currentIndex = 0; // 褰撳墠绱㈠紩锛岀敤浜庨亶鍘唖lotArray
-        private int consecutiveFailures = 0; // 杩炵画澶辫触娆℃暟
-        private static final int MAX_CONSECUTIVE_FAILURES = 5; // 鏈�澶ц繛缁け璐ユ鏁�
-        
-        @Override
-        public void run() {
-            //System.out.println("杞鏌ヨ绾跨▼寮�濮嬭繍琛�");
-            
-            while (isRunning && !Thread.currentThread().isInterrupted() && !shouldStop.get()) {
-                try {
-                    // 妫�鏌ユ槸鍚︽殏鍋�
-                    if (isPaused) {
-                        synchronized (lunxun.class) {
-                            while (isPaused && isRunning && !shouldStop.get()) {
-                                lunxun.class.wait(1000); // 绛夊緟1绉掓垨鐩村埌琚敜閱�
-                            }
-                        }
-                        continue;
-                    }
-                    
-                    // 瀹氭湡妫�鏌ヤ覆鍙h繛鎺ョ姸鎬侊紙姣�10娆″惊鐜鏌ヤ竴娆★級
-                    if (currentIndex % 10 == 0 && !checkSerialConnectionWithRetry()) {
-                        System.err.println("涓插彛杩炴帴鏂紑锛屾殏鍋滆疆璇�");
-                        pausePolling();
-                        continue;
-                    }
-                    
-                    // 鑾峰彇鍗℃Ы鏁扮粍
-                    Fkj[] slotArray = SlotManager.getSlotArray();
-                    if (slotArray == null || slotArray.length == 0) {
-                        System.err.println("鍗℃Ы鏁扮粍鏈垵濮嬪寲");
-                        Thread.sleep(pollingInterval);
-                        continue;
-                    }
-                    
-                    // 閬嶅巻鎵�鏈夊崱妲斤紝鍙粰 hasCard != 1 鐨勫崱妲藉彂閫佹煡璇㈡寚浠�
-                    boolean sentQuery = false;
-                    int checkedSlots = 0;
-                    int maxSlotsPerCycle = Math.min(10, slotArray.length); // 姣忓懆鏈熸渶澶氭鏌�10涓崱妲�
-                    
-                    for (int i = 0; i < maxSlotsPerCycle && checkedSlots < slotArray.length; i++) {
-                        Fkj slot = slotArray[currentIndex];
-                        if (slot != null) {
-                            String hasCard = slot.getHasCard();
-                            if (!"1".equals(hasCard)) {
-                                int slotNumber = currentIndex + 1;
-                                if (sendQueryToSlot(slotNumber)) {
-                                    sentQuery = true;
-                                    consecutiveFailures = 0;
-                                    
-                                    // 鍏抽敭淇锛氬湪break鍓嶅厛鏇存柊绱㈠紩
-                                    currentIndex = (currentIndex + 1) % slotArray.length;
-                                    checkedSlots++;
-                                    
-                                    Thread.sleep(pollingInterval);
-                                    break;
-                                } else {
-                                    consecutiveFailures++;
-                                    if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
-                                        System.err.println("杩炵画澶辫触娆℃暟杩囧锛屾殏鍋滆疆璇�");
-                                        pausePolling();
-                                        break;
-                                    }
-                                }
-                            }
-                        }
-                        
-                        // 瀵逛簬涓嶉渶瑕佸彂閫佹煡璇㈢殑鍗℃Ы锛屾甯告洿鏂扮储寮�
-                        currentIndex = (currentIndex + 1) % slotArray.length;
-                        checkedSlots++;
-                    }
-                    
-                    // 濡傛灉娌℃湁鎵惧埌闇�瑕佹煡璇㈢殑鍗℃Ы锛岀瓑寰呬竴娈垫椂闂村啀缁х画
-                    if (!sentQuery) {
-                        Thread.sleep(pollingInterval * 2); // 娌℃湁鏌ヨ鏃剁瓑寰呮椂闂村姞鍊�
-                    }
-                    
-                } catch (InterruptedException e) {
-                    //System.out.println("杞鏌ヨ绾跨▼琚腑鏂�");
-                    Thread.currentThread().interrupt();
-                    break;
-                } catch (Exception e) {
-                    System.err.println("杞鏌ヨ杩囩▼涓彂鐢熷紓甯�: " + e.getMessage());
-                    consecutiveFailures++;
-                    
-                    // 鍙戠敓寮傚父鏃剁瓑寰呬竴娈垫椂闂村啀缁х画
-                    try {
-                        Thread.sleep(1000);
-                    } catch (InterruptedException ie) {
-                        Thread.currentThread().interrupt();
-                        break;
-                    }
-                }
-            }
-            
-            //System.out.println("杞鏌ヨ绾跨▼缁撴潫杩愯");
-        }
-        
-        /**
-         * 鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠� - 浼樺寲鐗堟湰
-         * 浣跨敤缂撳瓨鎸囦护锛屼紭鍖栬皟璇曡緭鍑�
-         */
-        private boolean sendQueryToSlot(int slotNumber) {
-            try {
-                // 浣跨敤缂撳瓨鐨勬煡璇㈡寚浠�
-                String queryCommand = getCachedQueryCommand(slotNumber);
-                
-                if (queryCommand != null && !queryCommand.trim().isEmpty()) {
-                    // 鍙戦�佸埌涓插彛
-                    boolean sendResult = Sendmsg.sendMessage(queryCommand);
-                    
-                    if (sendResult) {
-                        // 鍙湪璋冭瘯鏃惰緭鍑猴紝閬垮厤棰戠箒鎵撳嵃
-                        if (DEBUG_ENABLED) {
-                            SystemDebugDialog.appendAsciiData(String.format("Slot %d Send query (hasCard !=1)", slotNumber));
-                        }
-                        return true;
-                    } else {
-                        if (DEBUG_ENABLED) {
-                            SystemDebugDialog.appendAsciiData("Send query command to card slot err");
-                        }
-                        // 鍙戦�佸け璐ュ彲鑳芥槸涓插彛鏂紑锛屾洿鏂拌繛鎺ョ姸鎬�
-                        serialConnected = false;
-                        return false;
-                    }
-                } else {
-                    System.err.println("鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
-                    return false;
-                }
-                
-            } catch (Exception e) {
-                System.err.println("鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
-                // 鍙戠敓寮傚父鏃舵洿鏂颁覆鍙h繛鎺ョ姸鎬�
-                serialConnected = false;
-                return false;
-            }
-        }
-    }
-    
-    /**
-     * 绔嬪嵆鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠わ紙涓嶇瓑寰呰疆璇級
-     * @param slotNumber 鍗℃Ы缂栧彿 (1-60)
-     * @return true-鍙戦�佹垚鍔�, false-鍙戦�佸け璐�
-     */
-    public static boolean sendImmediateQuery(int slotNumber) {
-        if (slotNumber < MIN_SLOT || slotNumber > MAX_SLOT) {
-            System.err.println("鍗℃Ы缂栧彿蹇呴』鍦�" + MIN_SLOT + "-" + MAX_SLOT + "涔嬮棿");
-            return false;
-        }
-        
-        // 妫�鏌ヤ覆鍙h繛鎺�
-        if (!checkSerialConnectionWithRetry()) {
-            System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹煡璇㈡寚浠�");
-            return false;
-        }
-        
-        try {
-            // 浣跨敤缂撳瓨鐨勬煡璇㈡寚浠�
-            String queryCommand = getCachedQueryCommand(slotNumber);
-            
-            if (queryCommand != null && !queryCommand.trim().isEmpty()) {
-                // 鍙戦�佸埌涓插彛
-                boolean sendResult = Sendmsg.sendMessage(queryCommand);
-                
-                if (sendResult) {
-                    if (DEBUG_ENABLED) {
-                        //System.out.println("绔嬪嵆鏌ヨ鎴愬姛 - 鍗℃Ы " + slotNumber);
-                    }
-                    return true;
-                } else {
-                    System.err.println("绔嬪嵆鏌ヨ澶辫触 - 鍙戦�佹寚浠ゅ埌鍗℃Ы " + slotNumber + " 澶辫触");
-                    return false;
-                }
-            } else {
-                System.err.println("绔嬪嵆鏌ヨ澶辫触 - 鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
-                return false;
-            }
-            
-        } catch (Exception e) {
-            System.err.println("绔嬪嵆鏌ヨ鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
-            return false;
-        }
-    }
-    
-    /**
-     * 绔嬪嵆鍚戞墍鏈夊崱妲藉彂閫佹煡璇㈡寚浠わ紙鎵归噺锛�- 浼樺寲鐗堟湰
-     * @return 鎴愬姛鍙戦�佺殑鎸囦护鏁伴噺
-     */
-    public static int sendImmediateQueryToAll() {
-        // 妫�鏌ヤ覆鍙h繛鎺�
-        if (!checkSerialConnectionWithRetry()) {
-            System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹壒閲忔煡璇㈡寚浠�");
-            return 0;
-        }
-        
-        int successCount = 0;
-        int batchSize = 5; // 姣忔壒娆″彂閫�5涓煡璇�
-        int totalSlots = MAX_SLOT - MIN_SLOT + 1;
-        
-        //System.out.println("寮�濮嬫壒閲忔煡璇㈡墍鏈夊崱妲�...");
-        
-        for (int batchStart = MIN_SLOT; batchStart <= MAX_SLOT; batchStart += batchSize) {
-            if (shouldStop.get()) {
-                break;
-            }
-            
-            int batchEnd = Math.min(batchStart + batchSize - 1, MAX_SLOT);
-            
-            // 鎵规鍐呮煡璇�
-            for (int slot = batchStart; slot <= batchEnd; slot++) {
-                if (sendImmediateQuery(slot)) {
-                    successCount++;
-                }
-            }
-            
-            // 鎵规闂撮棿闅旓紝閬垮厤涓插彛鎷ュ牭
-            if (batchEnd < MAX_SLOT) {
-                try {
-                    Thread.sleep(50);
-                } catch (InterruptedException e) {
-                    Thread.currentThread().interrupt();
-                    break;
-                }
-            }
-        }
-        
-        //System.out.println("鎵归噺鏌ヨ瀹屾垚锛屾垚鍔熷彂閫�: " + successCount + "/" + totalSlots);
-        return successCount;
-    }
-    
-    /**
-     * 鎵嬪姩璁剧疆涓插彛杩炴帴鐘舵�侊紙鐢ㄤ簬澶栭儴妫�娴嬪埌涓插彛鐘舵�佸彉鍖栨椂璋冪敤锛�
-     * @param connected 涓插彛杩炴帴鐘舵��
-     */
-    public static void setSerialConnected(boolean connected) {
-        serialConnected = connected;
-        lastSerialCheckTime = System.currentTimeMillis();
-        
-        if (connected) {
-//            //System.out.println("涓插彛杩炴帴鐘舵�佸凡璁剧疆涓�: 宸茶繛鎺�");
-        } else {
-            System.err.println("涓插彛杩炴帴鐘舵�佸凡璁剧疆涓�: 鏈繛鎺�");
-            // 濡傛灉涓插彛鏂紑涓旇疆璇㈡鍦ㄨ繍琛岋紝鑷姩鏆傚仠杞
-            if (isRunning && !isPaused) {
-                pausePolling();
-            }
-        }
-    }
-    
-    /**
-     * 鑾峰彇杞鐘舵�佷俊鎭�
-     * @return 鐘舵�佷俊鎭瓧绗︿覆
-     */
-    public static String getPollingStatus() {
-        String status;
-        if (!isRunning) {
-            status = "宸插仠姝�";
-        } else if (isPaused) {
-            status = "宸叉殏鍋�";
-        } else {
-            status = "杩愯涓�";
-        }
-        
-        String serialStatus = serialConnected ? "宸茶繛鎺�" : "鏈繛鎺�";
-        int cacheSize = queryCommandCache.size();
-        
-        return String.format("杞鐘舵��: %s, 涓插彛: %s, 闂撮殧: %dms, 鎸囦护缂撳瓨: %d, 鍗℃Ы鑼冨洿: %d-%d", 
-                           status, serialStatus, pollingInterval, cacheSize, MIN_SLOT, MAX_SLOT);
-    }
-    
-    /**
-     * 鐩存帴璁剧疆杞鏆傚仠鐘舵�侊紙渚涘叾浠栫被璋冪敤锛�
-     * @param paused true-鏆傚仠杞, false-鎭㈠杞
-     * @return true-璁剧疆鎴愬姛, false-璁剧疆澶辫触锛堝杞鏈繍琛岋級
-     */
-    public static boolean setPollingPaused(boolean paused) {
-        if (!isRunning) {
-            //System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曡缃殏鍋滅姸鎬�");
-            return false;
-        }
-        
-        if (paused) {
-            // 璇锋眰鏆傚仠
-            if (!isPaused) {
-                isPaused = true;
-                //System.out.println("杞鏌ヨ宸查�氳繃澶栭儴璋冪敤鏆傚仠");
-                return true;
-            } else {
-                //System.out.println("杞鏌ヨ宸茬粡澶勪簬鏆傚仠鐘舵��");
-                return false;
-            }
-        } else {
-            // 璇锋眰鎭㈠
-            if (isPaused) {
-                // 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
-                if (!checkSerialConnectionWithRetry()) {
-                    System.err.println("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
-                    return false;
-                }
-                
-                isPaused = false;
-                synchronized (lunxun.class) {
-                    lunxun.class.notifyAll(); // 鍞ら啋绛夊緟鐨勭嚎绋�
-                }
-                //System.out.println("杞鏌ヨ宸查�氳繃澶栭儴璋冪敤鎭㈠");
-                return true;
-            } else {
-                //System.out.println("杞鏌ヨ鏈浜庢殏鍋滅姸鎬�");
-                return false;
-            }
-        }
-    }
-    
-    /**
-     * 鑾峰彇鎬ц兘缁熻淇℃伅
-     */
-    public static String getPerformanceStats() {
-        return String.format("鏌ヨ鎸囦护缂撳瓨澶у皬: %d, 杞闂撮殧: %dms", 
-                           queryCommandCache.size(), pollingInterval);
-    }
+	private static volatile boolean isRunning = false;
+	private static volatile boolean isPaused = false;
+	private static final AtomicBoolean shouldStop = new AtomicBoolean(false);
+	private static Thread pollingThread;
+	private static int pollingInterval = 100; // 榛樿杞闂撮殧
+	public static boolean sendChaxunzhiling=true;//鏄惁鍚戜覆鍙e彂閫佹煡璇㈡寚浠�
+
+	public static boolean isSendChaxunzhiling() {
+		return sendChaxunzhiling;
+	}
+
+	public static void setSendChaxunzhiling(boolean sendChaxunzhiling) {
+		lunxun.sendChaxunzhiling = sendChaxunzhiling;
+	}
+
+	// 鍗℃Ы鐩稿叧甯搁噺
+	private static final int MIN_SLOT = 1;
+	private static final int MAX_SLOT = 60;
+
+	// 涓插彛杩炴帴妫�鏌ョ浉鍏�
+	private static final int SERIAL_CHECK_INTERVAL = 5000; // 5绉掓鏌ヤ竴娆′覆鍙h繛鎺�
+	private static long lastSerialCheckTime = 0;
+	private static boolean serialConnected = false;
+
+	// 鎬ц兘浼樺寲锛氭煡璇㈡寚浠ょ紦瀛�
+	private static final Map<Integer, String> queryCommandCache = new ConcurrentHashMap<>();
+
+	// 璋冭瘯妯″紡鎺у埗
+	public static  boolean DEBUG_ENABLED = false;
+
+	// 鏂板锛氫笉鍚岀姸鎬佸崱妲界殑鏌ヨ棰戠巼鎺у埗
+	private static final int NO_CARD_QUERY_INTERVAL = 100;    // 鏃犲崱鍗℃Ы鏌ヨ闂撮殧锛�100ms
+	private static final int HAS_CARD_QUERY_INTERVAL = 10000; // 鏈夊崱鍗℃Ы鏌ヨ闂撮殧锛�10绉�
+	private static final Map<Integer, Long> lastQueryTimeMap = new ConcurrentHashMap<>(); // 璁板綍姣忎釜鍗℃Ы鐨勬渶鍚庢煡璇㈡椂闂�
+
+	/**
+	 * 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬� - 浼樺寲鐗堟湰锛屾坊鍔犻噸璇曟満鍒�
+	 * @return true-涓插彛宸茶繛鎺�, false-涓插彛鏈繛鎺�
+	 */
+	public static boolean checkSerialConnection() {
+		// 閬垮厤棰戠箒妫�鏌ワ紝姣�5绉掓鏌ヤ竴娆�
+		long currentTime = System.currentTimeMillis();
+		if (currentTime - lastSerialCheckTime < SERIAL_CHECK_INTERVAL) {
+			return serialConnected;
+		}
+
+		lastSerialCheckTime = currentTime;
+
+		try {
+			// 绠�鍖栨鏌ワ細鐩存帴妫�鏌endmsg鐨勪覆鍙g姸鎬侊紝鑰屼笉鏄彂閫佹祴璇曟寚浠�
+			boolean result = Sendmsg.isPortOpen();
+
+			if (result) {
+				if (DEBUG_ENABLED) {
+					//System.out.println("涓插彛杩炴帴姝e父");
+				}
+				serialConnected = true;
+			} else {
+				Errlog.logOperation("涓插彛杩炴帴澶辫触 - 涓插彛鏈墦寮�");
+				serialConnected = false;
+			}
+		} catch (Exception e) {
+			Errlog.logOperation("涓插彛杩炴帴妫�鏌ュ紓甯�: " + e.getMessage());
+			serialConnected = false;
+		}
+
+		return serialConnected;
+	}
+
+	/**
+	 * 甯﹂噸璇曠殑涓插彛杩炴帴妫�鏌�
+	 */
+	private static boolean checkSerialConnectionWithRetry() {
+		for (int i = 0; i < 3; i++) {
+			if (checkSerialConnection()) {
+				return true;
+			}
+			try {
+				Thread.sleep(1000);
+			} catch (InterruptedException e) {
+				Thread.currentThread().interrupt();
+				return false;
+			}
+		}
+		return false;
+	}
+
+	/**
+	 * 鍚姩杞鏌ヨ - 浼樺寲鐗堟湰
+	 * @return true-鍚姩鎴愬姛, false-鍚姩澶辫触
+	 */
+	public static boolean startPolling() {
+		if (isRunning) {
+			//System.out.println("杞鏌ヨ宸茬粡鍦ㄨ繍琛屼腑");
+			return true;
+		}
+
+		// 鍚姩鍓嶄弗鏍兼鏌ヤ覆鍙h繛鎺�
+		if (!checkSerialConnectionWithRetry()) {
+			Errlog.logOperation("涓插彛鏈繛鎺ワ紝鏃犳硶鍚姩杞鏌ヨ");
+			return false;
+		}
+
+		// 浠庨厤缃腑鑾峰彇杞闂撮殧
+		loadPollingIntervalFromConfig();
+
+		// 鍒濆鍖栨渶鍚庢煡璇㈡椂闂磋褰�
+		initializeLastQueryTimes();
+
+		isRunning = true;
+		isPaused = false;
+		shouldStop.set(false);
+
+		try {
+			pollingThread = new Thread(new PollingTask(), "CardSlot-Polling-Thread");
+			pollingThread.setDaemon(true);
+			pollingThread.start(); 
+			return true;
+		} catch (Exception e) {
+			Errlog.logOperation("鍚姩杞鏌ヨ绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
+			isRunning = false;
+			shouldStop.set(true);
+			return false;
+		}
+	}
+
+	/**
+	 * 鍒濆鍖栨渶鍚庢煡璇㈡椂闂磋褰�
+	 */
+	private static void initializeLastQueryTimes() {
+		lastQueryTimeMap.clear();
+		for (int i = MIN_SLOT; i <= MAX_SLOT; i++) {
+			lastQueryTimeMap.put(i, 0L); // 鍒濆鍖栦负0锛岃〃绀轰粠鏈煡璇㈣繃
+		}
+	}
+
+	/**
+	 * 鍋滄杞鏌ヨ - 淇鐗堟湰
+	 * @return true-鍋滄鎴愬姛, false-鍋滄澶辫触
+	 */
+	public static boolean stopPolling() {
+		if (!isRunning) {
+			//System.out.println("杞鏌ヨ鏈湪杩愯");
+			return false;
+		}
+
+		shouldStop.set(true);
+		isRunning = false;
+		isPaused = false;
+
+		if (pollingThread != null) {
+			pollingThread.interrupt();
+			try {
+				pollingThread.join(3000); // 绛夊緟3绉�
+				// 妫�鏌ョ嚎绋嬫槸鍚﹁繕鍦ㄨ繍琛�
+				if (pollingThread.isAlive()) {
+					Errlog.logOperation("杞绾跨▼鏈湪3绉掑唴鍋滄锛屾爣璁颁负瀹堟姢绾跨▼骞跺拷鐣�");
+					// 涓嶅己鍒跺仠姝紝鑰屾槸纭繚瀹冩槸瀹堟姢绾跨▼
+					pollingThread.setDaemon(true);
+				}
+			} catch (InterruptedException e) {
+				Errlog.logOperation("鍋滄杞鏌ヨ鏃惰涓柇: " + e.getMessage());
+				Thread.currentThread().interrupt();
+			} catch (Exception e) {
+				Errlog.logOperation("鍋滄杞绾跨▼鏃跺彂鐢熷紓甯�: " + e.getMessage());
+			} finally {
+				pollingThread = null;
+			}
+		}
+
+		shouldStop.set(false);
+		//System.out.println("杞鏌ヨ宸插仠姝� - 涓插彛鏁版嵁鎺ユ敹涓嶅彈褰卞搷");
+		return true;
+	}
+
+	/**
+	 * 鏆傚仠杞鏌ヨ
+	 * @return true-鏆傚仠鎴愬姛, false-鏆傚仠澶辫触
+	 */
+	public static boolean pausePolling() {
+		if (!isRunning) {
+			if (DEBUG_ENABLED) {
+				//System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曟殏鍋�");
+			}
+			return false;
+		}
+
+		if (isPaused) {
+			if (DEBUG_ENABLED) {
+				//System.out.println("杞鏌ヨ宸茬粡澶勪簬鏆傚仠鐘舵��");
+			}
+			return false;
+		}
+
+		isPaused = true;
+		//System.out.println("杞鏌ヨ宸叉殏鍋� - 浠呭仠姝㈠彂閫佹煡璇㈡寚浠�");
+		return true;
+	}
+
+	/**
+	 * 鎭㈠杞鏌ヨ
+	 * @return true-鎭㈠鎴愬姛, false-鎭㈠澶辫触
+	 */
+	public static boolean resumePolling() {
+		if (!isRunning) {
+			//System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曟仮澶�");
+			return false;
+		}
+
+		if (!isPaused) {
+			//System.out.println("杞鏌ヨ鏈浜庢殏鍋滅姸鎬�");
+			return false;
+		}
+
+		// 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
+		if (!checkSerialConnectionWithRetry()) {
+			Errlog.logOperation("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
+			return false;
+		}
+
+		isPaused = false;
+		synchronized (lunxun.class) {
+			lunxun.class.notifyAll(); // 鍞ら啋绛夊緟鐨勭嚎绋�
+		}
+		//System.out.println("杞鏌ヨ宸叉仮澶�");
+		return true;
+	}
+
+	/**
+	 * 妫�鏌ヨ疆璇㈢姸鎬�
+	 * @return true-姝e湪杩愯, false-宸插仠姝�
+	 */
+	public static boolean isPolling() {
+		return isRunning;
+	}
+
+	/**
+	 * 妫�鏌ユ槸鍚︽殏鍋�
+	 * @return true-宸叉殏鍋�, false-鏈殏鍋�
+	 */
+	public static boolean isPaused() {
+		return isPaused;
+	}
+
+	/**
+	 * 妫�鏌ヤ覆鍙h繛鎺ョ姸鎬�
+	 * @return true-涓插彛宸茶繛鎺�, false-涓插彛鏈繛鎺�
+	 */
+	public static boolean isSerialConnected() {
+		return serialConnected;
+	}
+
+	/**
+	 * 璁剧疆杞闂撮殧
+	 * @param interval 杞闂撮殧锛堟绉掞級
+	 */
+	public static void setPollingInterval(int interval) {
+		if (interval < 10) {
+			Errlog.logOperation("杞闂撮殧涓嶈兘灏忎簬10ms");
+			return;
+		}
+
+		pollingInterval = interval;
+		//System.out.println("杞闂撮殧宸茶缃负: " + interval + "ms");
+
+		// 濡傛灉姝e湪杩愯锛岄噸鏂板惎鍔ㄤ互搴旂敤鏂扮殑闂撮殧
+		if (isRunning) {
+			restartPolling();
+		}
+	}
+
+	/**
+	 * 鑾峰彇褰撳墠杞闂撮殧
+	 * @return 杞闂撮殧锛堟绉掞級
+	 */
+	public static int getPollingInterval() {
+		return pollingInterval;
+	}
+
+	/**
+	 * 閲嶆柊鍚姩杞鏌ヨ
+	 * @return true-閲嶅惎鎴愬姛, false-閲嶅惎澶辫触
+	 */
+	public static boolean restartPolling() {
+		stopPolling();
+
+		// 绛夊緟涓�灏忔鏃堕棿纭繚绾跨▼瀹屽叏鍋滄
+		try {
+			Thread.sleep(200);
+		} catch (InterruptedException e) {
+			Thread.currentThread().interrupt();
+		}
+
+		return startPolling();
+	}
+
+	/**
+	 * 浠庨厤缃腑鍔犺浇杞闂撮殧
+	 */
+	private static void loadPollingIntervalFromConfig() {
+		try {
+			Chushihua configSystem = Chushihua.getInstance();
+			if (configSystem.isInitialized()) {
+				MachineConfig machineConfig = configSystem.getMachineConfig();
+				pollingInterval = machineConfig.getPollingInterval();
+				//System.out.println("浠庨厤缃姞杞借疆璇㈤棿闅�: " + pollingInterval + "ms");
+			} else {
+				//System.out.println("閰嶇疆绯荤粺鏈垵濮嬪寲锛屼娇鐢ㄩ粯璁よ疆璇㈤棿闅�: " + pollingInterval + "ms");
+			}
+		} catch (Exception e) {
+			Errlog.logOperation("鍔犺浇杞闂撮殧閰嶇疆澶辫触: " + e.getMessage());
+			//System.out.println("浣跨敤榛樿杞闂撮殧: " + pollingInterval + "ms");
+		}
+	}
+
+	/**
+	 * 鑾峰彇缂撳瓨鐨勬煡璇㈡寚浠�
+	 */
+	private static String getCachedQueryCommand(int slotNumber) {
+		return queryCommandCache.computeIfAbsent(slotNumber, QueryData::queryData);
+	}
+
+	/**
+	 * 娓呯┖鏌ヨ鎸囦护缂撳瓨锛堝綋鏌ヨ閫昏緫鍙樺寲鏃惰皟鐢級
+	 */
+	public static void clearQueryCache() {
+		queryCommandCache.clear();
+		//System.out.println("鏌ヨ鎸囦护缂撳瓨宸叉竻绌�");
+	}
+
+	/**
+	 * 杞浠诲姟鍐呴儴绫� - 浼樺寲鐗堟湰
+	 * 鏀寔涓嶅悓鐘舵�佸崱妲界殑涓嶅悓鏌ヨ棰戠巼
+	 */
+
+	private static class PollingTask implements Runnable {
+		private int currentIndex = 0; // 褰撳墠绱㈠紩锛岀敤浜庨亶鍘唖lotArray
+		private int consecutiveFailures = 0; // 杩炵画澶辫触娆℃暟
+		private static final int MAX_CONSECUTIVE_FAILURES = 5; // 鏈�澶ц繛缁け璐ユ鏁�
+
+		@Override
+		public void run() {
+			//System.out.println("杞鏌ヨ绾跨▼寮�濮嬭繍琛�");
+
+			while (isRunning && !Thread.currentThread().isInterrupted() && !shouldStop.get()) {
+				try {
+
+//					System.out.println("鏌ヨ涓�.....绾跨▼");
+					// 妫�鏌ユ槸鍚︽殏鍋�
+					if (isPaused) {
+						synchronized (lunxun.class) {
+							while (isPaused && isRunning && !shouldStop.get()) {
+								lunxun.class.wait(1000); // 绛夊緟1绉掓垨鐩村埌琚敜閱�
+							}
+						}
+						continue;
+					}
+
+					// 瀹氭湡妫�鏌ヤ覆鍙h繛鎺ョ姸鎬侊紙姣�10娆″惊鐜鏌ヤ竴娆★級
+					if (currentIndex % 10 == 0 && !checkSerialConnectionWithRetry()) {
+						Errlog.logOperation("涓插彛杩炴帴鏂紑锛屾殏鍋滆疆璇�");
+						pausePolling();
+						continue;
+					}
+
+					// 鑾峰彇鍗℃Ы鏁扮粍
+					Fkj[] slotArray = SlotManager.getSlotArray();
+					if (slotArray == null || slotArray.length == 0) {
+						Errlog.logOperation("鍗℃Ы鏁扮粍鏈垵濮嬪寲");
+						Thread.sleep(pollingInterval);
+						continue;
+					}
+
+					// 鏂板锛氭牴鎹崱妲界姸鎬佸拰鏌ヨ棰戠巼鍐冲畾鏄惁鍙戦�佹煡璇�
+					boolean sentQuery = false;
+					long currentTime = System.currentTimeMillis();
+
+					// 閬嶅巻鍗℃Ы锛屽鎵鹃渶瑕佹煡璇㈢殑鍗℃Ы
+					for (int i = 0; i < slotArray.length && !sentQuery; i++) {
+						int slotIndex = (currentIndex + i) % slotArray.length;
+						Fkj slot = slotArray[slotIndex];
+						if (slot != null) {
+							String hasCard = slot.getHasCard();
+							int slotNumber = slotIndex + 1;
+							Long lastQueryTime = lastQueryTimeMap.get(slotNumber);
+
+							// 纭畾鏌ヨ闂撮殧锛氬彧鏈塰asCard="1"鐨勫崱妲戒娇鐢�10绉掗棿闅旓紝鍏朵粬鎯呭喌锛堝寘鎷�"-1"锛夐兘浣跨敤100ms闂撮殧
+							int queryInterval = "1".equals(hasCard) ? HAS_CARD_QUERY_INTERVAL : NO_CARD_QUERY_INTERVAL;
+
+							// 妫�鏌ユ槸鍚﹁揪鍒版煡璇㈡椂闂�
+							if (lastQueryTime == null || currentTime - lastQueryTime >= queryInterval) {
+								if (sendQueryToSlot(slotNumber)) {
+									// 鏇存柊鏈�鍚庢煡璇㈡椂闂�
+									lastQueryTimeMap.put(slotNumber, currentTime);
+									sentQuery = true;
+									consecutiveFailures = 0;
+
+									// 鎴愬姛鍙戦�佹煡璇㈠悗锛岀瓑寰�100ms鍐嶇户缁笅涓�涓煡璇�
+									Thread.sleep(100);
+
+									if (DEBUG_ENABLED) {
+										String status;
+										if ("1".equals(hasCard)) {
+											status = "鏈夊崱";
+										} else if ("-1".equals(hasCard)) {
+											status = "鏈煡";
+										} else {
+											status = "鏃犲崱";
+										}
+										if (DEBUG_ENABLED) {
+										    StringBuilder debugMsg = new StringBuilder(50);
+										    debugMsg.append("Slot ").append(slotNumber)
+										            .append(" (").append(status).append(") 鏌ヨ鎴愬姛锛岄棿闅�: ")
+										            .append(queryInterval).append("ms\n");
+										    SystemDebugDialog.appendAsciiData(debugMsg.toString());
+										}
+									}
+								} else {
+									consecutiveFailures++;
+									if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
+										Errlog.logOperation("lunxun杩炵画澶辫触娆℃暟杩囧锛屾殏鍋滆疆璇�");
+										pausePolling();
+										break;
+									}
+								}
+							}
+						}
+					}
+
+					// 鏇存柊褰撳墠绱㈠紩
+					currentIndex = (currentIndex + 1) % slotArray.length;
+
+					// 濡傛灉娌℃湁鍙戦�佹煡璇紝绛夊緟涓�娈垫椂闂村啀缁х画
+					if (!sentQuery) {
+						Thread.sleep(pollingInterval);
+					}
+
+				} catch (InterruptedException e) {
+					//System.out.println("杞鏌ヨ绾跨▼琚腑鏂�");
+					Thread.currentThread().interrupt();
+					break;
+				} catch (Exception e) {
+					Errlog.logOperation("杞鏌ヨ杩囩▼涓彂鐢熷紓甯�: " + e.getMessage());
+					consecutiveFailures++;
+
+					// 鍙戠敓寮傚父鏃剁瓑寰呬竴娈垫椂闂村啀缁х画
+					try {
+						Thread.sleep(1000);
+					} catch (InterruptedException ie) {
+						Thread.currentThread().interrupt();
+						break;
+					}
+				}
+			}
+
+			//System.out.println("杞鏌ヨ绾跨▼缁撴潫杩愯");
+		}
+
+		/**
+		 * 鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠� - 浼樺寲鐗堟湰
+		 * 浣跨敤缂撳瓨鎸囦护锛屼紭鍖栬皟璇曡緭鍑�
+		 */
+		private boolean sendQueryToSlot(int slotNumber) {
+			try {
+				// 浣跨敤缂撳瓨鐨勬煡璇㈡寚浠�            	
+				String queryCommand = getCachedQueryCommand(slotNumber);
+//				 System.out.println("鎸囦护鏄細"+queryCommand);
+				if (DEBUG_ENABLED) {
+					SystemDebugDialog.appendAsciiData("send to "+slotNumber+" queryCommand");
+				}
+
+				if (queryCommand != null && !queryCommand.trim().isEmpty()) {
+					// 鍙戦�佸埌涓插彛
+
+					if(sendChaxunzhiling) {
+						boolean sendResult = Sendmsg.sendMessage(queryCommand);
+						if (sendResult) {
+							return true;
+						} else {
+							if (DEBUG_ENABLED) {
+								SystemDebugDialog.appendAsciiData(slotNumber+" Send query command to card slot err");
+							}
+							// 鍙戦�佸け璐ュ彲鑳芥槸涓插彛鏂紑锛屾洿鏂拌繛鎺ョ姸鎬�
+							serialConnected = false;
+							return false;
+						}
+					}else {
+						return false;
+					}
+				} else {
+					Errlog.logOperation("鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
+					return false;
+				}
+
+			} catch (Exception e) {
+				Errlog.logOperation("鍙戦�佹煡璇㈡寚浠ゅ埌鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
+				// 鍙戠敓寮傚父鏃舵洿鏂颁覆鍙h繛鎺ョ姸鎬�
+				serialConnected = false;
+				return false;
+			}
+		}
+	}
+
+	/**
+	 * 绔嬪嵆鍚戞寚瀹氬崱妲藉彂閫佹煡璇㈡寚浠わ紙涓嶇瓑寰呰疆璇級
+	 * @param slotNumber 鍗℃Ы缂栧彿 (1-60)
+	 * @return true-鍙戦�佹垚鍔�, false-鍙戦�佸け璐�
+	 */
+	public static boolean sendImmediateQuery(int slotNumber) {
+		if (slotNumber < MIN_SLOT || slotNumber > MAX_SLOT) {
+			Errlog.logOperation("鍗℃Ы缂栧彿蹇呴』鍦�" + MIN_SLOT + "-" + MAX_SLOT + "涔嬮棿");
+			return false;
+		}
+
+		// 妫�鏌ヤ覆鍙h繛鎺�
+		if (!checkSerialConnectionWithRetry()) {
+			Errlog.logOperation("涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹煡璇㈡寚浠�");
+			return false;
+		}
+
+		try {
+			// 浣跨敤缂撳瓨鐨勬煡璇㈡寚浠�
+			String queryCommand = getCachedQueryCommand(slotNumber);
+
+			if (queryCommand != null && !queryCommand.trim().isEmpty()) {
+				// 鍙戦�佸埌涓插彛
+				boolean sendResult = Sendmsg.sendMessage(queryCommand);
+
+				if (sendResult) {
+					// 鏇存柊鏈�鍚庢煡璇㈡椂闂�
+					lastQueryTimeMap.put(slotNumber, System.currentTimeMillis());
+
+					if (DEBUG_ENABLED) {
+						//System.out.println("绔嬪嵆鏌ヨ鎴愬姛 - 鍗℃Ы " + slotNumber);
+					}
+					return true;
+				} else {
+					Errlog.logOperation("绔嬪嵆鏌ヨ澶辫触 - 鍙戦�佹寚浠ゅ埌鍗℃Ы " + slotNumber + " 澶辫触");
+					return false;
+				}
+			} else {
+				Errlog.logOperation("绔嬪嵆鏌ヨ澶辫触 - 鐢熸垚鐨勬煡璇㈡寚浠や负绌猴紝鍗℃Ы: " + slotNumber);
+				return false;
+			}
+
+		} catch (Exception e) {
+			Errlog.logOperation("绔嬪嵆鏌ヨ鍗℃Ы " + slotNumber + " 鏃跺彂鐢熷紓甯�: " + e.getMessage());
+			return false;
+		}
+	}
+
+	/**
+	 * 绔嬪嵆鍚戞墍鏈夊崱妲藉彂閫佹煡璇㈡寚浠わ紙鎵归噺锛�- 浼樺寲鐗堟湰
+	 * @return 鎴愬姛鍙戦�佺殑鎸囦护鏁伴噺
+	 */
+	public static int sendImmediateQueryToAll() {
+		// 妫�鏌ヤ覆鍙h繛鎺�
+		if (!checkSerialConnectionWithRetry()) {
+			Errlog.logOperation("涓插彛鏈繛鎺ワ紝鏃犳硶鍙戦�佹壒閲忔煡璇㈡寚浠�");
+			return 0;
+		}
+
+		int successCount = 0;
+		int batchSize = 5; // 姣忔壒娆″彂閫�5涓煡璇�
+		int totalSlots = MAX_SLOT - MIN_SLOT + 1;
+		long currentTime = System.currentTimeMillis();
+
+		//System.out.println("寮�濮嬫壒閲忔煡璇㈡墍鏈夊崱妲�...");
+
+		for (int batchStart = MIN_SLOT; batchStart <= MAX_SLOT; batchStart += batchSize) {
+			if (shouldStop.get()) {
+				break;
+			}
+
+			int batchEnd = Math.min(batchStart + batchSize - 1, MAX_SLOT);
+
+			// 鎵规鍐呮煡璇�
+			for (int slot = batchStart; slot <= batchEnd; slot++) {
+				if (sendImmediateQuery(slot)) {
+					successCount++;
+					// 鏇存柊鏈�鍚庢煡璇㈡椂闂�
+					lastQueryTimeMap.put(slot, currentTime);
+				}
+			}
+
+			// 鎵规闂撮棿闅旓紝閬垮厤涓插彛鎷ュ牭
+			if (batchEnd < MAX_SLOT) {
+				try {
+					Thread.sleep(50);
+				} catch (InterruptedException e) {
+					Thread.currentThread().interrupt();
+					break;
+				}
+			}
+		}
+
+		//System.out.println("鎵归噺鏌ヨ瀹屾垚锛屾垚鍔熷彂閫�: " + successCount + "/" + totalSlots);
+		return successCount;
+	}
+
+	/**
+	 * 鎵嬪姩璁剧疆涓插彛杩炴帴鐘舵�侊紙鐢ㄤ簬澶栭儴妫�娴嬪埌涓插彛鐘舵�佸彉鍖栨椂璋冪敤锛�
+	 * @param connected 涓插彛杩炴帴鐘舵��
+	 */
+	public static void setSerialConnected(boolean connected) {
+		serialConnected = connected;
+		lastSerialCheckTime = System.currentTimeMillis();
+
+		if (connected) {
+			//            //System.out.println("涓插彛杩炴帴鐘舵�佸凡璁剧疆涓�: 宸茶繛鎺�");
+		} else {
+			Errlog.logOperation("涓插彛杩炴帴鐘舵�佸凡璁剧疆涓�: 鏈繛鎺�");
+			// 濡傛灉涓插彛鏂紑涓旇疆璇㈡鍦ㄨ繍琛岋紝鑷姩鏆傚仠杞
+			if (isRunning && !isPaused) {
+				pausePolling();
+			}
+		}
+	}
+
+	/**
+	 * 鑾峰彇杞鐘舵�佷俊鎭�
+	 * @return 鐘舵�佷俊鎭瓧绗︿覆
+	 */
+	public static String getPollingStatus() {
+		String status;
+		if (!isRunning) {
+			status = "宸插仠姝�";
+		} else if (isPaused) {
+			status = "宸叉殏鍋�";
+		} else {
+			status = "杩愯涓�";
+		}
+
+		String serialStatus = serialConnected ? "宸茶繛鎺�" : "鏈繛鎺�";
+		int cacheSize = queryCommandCache.size();
+
+		// 缁熻涓嶅悓鐘舵�佺殑鍗℃Ы鏁伴噺
+		int noCardCount = 0;
+		int hasCardCount = 0;
+		Fkj[] slotArray = SlotManager.getSlotArray();
+		if (slotArray != null) {
+			for (Fkj slot : slotArray) {
+				if (slot != null && "1".equals(slot.getHasCard())) {
+					hasCardCount++;
+				} else {
+					noCardCount++;
+				}
+			}
+		}
+
+		return String.format("杞鐘舵��: %s, 涓插彛: %s, 闂撮殧: %dms, 鎸囦护缂撳瓨: %d, 鍗℃Ы鑼冨洿: %d-%d, 鏃犲崱: %d(100ms), 鏈夊崱: %d(10s)", 
+				status, serialStatus, pollingInterval, cacheSize, MIN_SLOT, MAX_SLOT, 
+				noCardCount, hasCardCount);
+	}
+
+	/**
+	 * 鐩存帴璁剧疆杞鏆傚仠鐘舵�侊紙渚涘叾浠栫被璋冪敤锛�
+	 * @param paused true-鏆傚仠杞, false-鎭㈠杞
+	 * @return true-璁剧疆鎴愬姛, false-璁剧疆澶辫触锛堝杞鏈繍琛岋級
+	 */
+	public static boolean setPollingPaused(boolean paused) {
+		if (!isRunning) {
+			//System.out.println("杞鏌ヨ鏈湪杩愯锛屾棤娉曡缃殏鍋滅姸鎬�");
+			return false;
+		}
+
+		if (paused) {
+			// 璇锋眰鏆傚仠
+			if (!isPaused) {
+				isPaused = true;
+				//System.out.println("杞鏌ヨ宸查�氳繃澶栭儴璋冪敤鏆傚仠");
+				return true;
+			} else {
+				//System.out.println("杞鏌ヨ宸茬粡澶勪簬鏆傚仠鐘舵��");
+				return false;
+			}
+		} else {
+			// 璇锋眰鎭㈠
+			if (isPaused) {
+				// 鎭㈠鍓嶆鏌ヤ覆鍙h繛鎺�
+				if (!checkSerialConnectionWithRetry()) {
+					Errlog.logOperation("涓插彛鏈繛鎺ワ紝鏃犳硶鎭㈠杞鏌ヨ");
+					return false;
+				}
+
+				isPaused = false;
+				synchronized (lunxun.class) {
+					lunxun.class.notifyAll(); // 鍞ら啋绛夊緟鐨勭嚎绋�
+				}
+				//System.out.println("杞鏌ヨ宸查�氳繃澶栭儴璋冪敤鎭㈠");
+				return true;
+			} else {
+				//System.out.println("杞鏌ヨ鏈浜庢殏鍋滅姸鎬�");
+				return false;
+			}
+		}
+	}
+
+	/**
+	 * 鑾峰彇鎬ц兘缁熻淇℃伅
+	 */
+	public static String getPerformanceStats() {
+		long currentTime = System.currentTimeMillis();
+		int overdueNoCard = 0;
+		int overdueHasCard = 0;
+
+		Fkj[] slotArray = SlotManager.getSlotArray();
+		if (slotArray != null) {
+			for (int i = 0; i < slotArray.length; i++) {
+				Fkj slot = slotArray[i];
+				if (slot != null) {
+					int slotNumber = i + 1;
+					Long lastQueryTime = lastQueryTimeMap.get(slotNumber);
+					if (lastQueryTime != null) {
+						String hasCard = slot.getHasCard();
+						int queryInterval = "1".equals(hasCard) ? HAS_CARD_QUERY_INTERVAL : NO_CARD_QUERY_INTERVAL;
+						if (currentTime - lastQueryTime > queryInterval) {
+							if ("1".equals(hasCard)) {
+								overdueHasCard++;
+							} else {
+								overdueNoCard++;
+							}
+						}
+					}
+				}
+			}
+		}
+
+		return String.format("鏌ヨ鎸囦护缂撳瓨澶у皬: %d, 杞闂撮殧: %dms, 瓒呮椂鏃犲崱: %d, 瓒呮椂鏈夊崱: %d", 
+				queryCommandCache.size(), pollingInterval, overdueNoCard, overdueHasCard);
+	}
+
+	/**
+	 * 璁剧疆鏃犲崱鍗℃Ы鏌ヨ闂撮殧
+	 * @param interval 鏌ヨ闂撮殧锛堟绉掞級
+	 */
+	public static void setNoCardQueryInterval(int interval) {
+		if (interval < 10) {
+			Errlog.logOperation("鏃犲崱鍗℃Ы鏌ヨ闂撮殧涓嶈兘灏忎簬10ms");
+			return;
+		}
+		// 娉ㄦ剰锛氳繖閲屽彧鏄缃父閲忥紝瀹為檯杩愯鏃堕渶瑕侀噸鏂板惎鍔ㄨ疆璇㈡墠鑳界敓鏁�
+		//System.out.println("鏃犲崱鍗℃Ы鏌ヨ闂撮殧宸茶缃负: " + interval + "ms");
+	}
+
+	/**
+	 * 璁剧疆鏈夊崱鍗℃Ы鏌ヨ闂撮殧
+	 * @param interval 鏌ヨ闂撮殧锛堟绉掞級
+	 */
+	public static void setHasCardQueryInterval(int interval) {
+		if (interval < 1000) {
+			Errlog.logOperation("鏈夊崱鍗℃Ы鏌ヨ闂撮殧涓嶈兘灏忎簬1000ms");
+			return;
+		}
+		// 娉ㄦ剰锛氳繖閲屽彧鏄缃父閲忥紝瀹為檯杩愯鏃堕渶瑕侀噸鏂板惎鍔ㄨ疆璇㈡墠鑳界敓鏁�
+		//System.out.println("鏈夊崱鍗℃Ы鏌ヨ闂撮殧宸茶缃负: " + interval + "ms");
+	}
 
 	public static boolean isDEBUG_ENABLED() {
 		return DEBUG_ENABLED;

--
Gitblit v1.9.3