From 7acfc864d11de1fc41cabc2a5d4fad3894c2e5b0 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 28 十一月 2025 11:27:08 +0800
Subject: [PATCH] 增加发卡不成功再次发卡的逻辑

---
 src/chuankou/Sendmsg.java |  670 ++++++++++++++++++++++++++++---------------------------
 1 files changed, 341 insertions(+), 329 deletions(-)

diff --git a/src/chuankou/Sendmsg.java b/src/chuankou/Sendmsg.java
index 79ace5c..3a74126 100644
--- a/src/chuankou/Sendmsg.java
+++ b/src/chuankou/Sendmsg.java
@@ -6,11 +6,13 @@
 
 import javax.swing.SwingWorker;
 
+import chushihua.SlotManager;
 import chushihua.lunxun;
 import dialog.Charulog;
 import dialog.Dingshidialog;
 import dialog.Errlog;
 import publicway.OpenDoor;
+import publicway.TimestampUtil;
 import xitongshezhi.SystemDebugDialog;
 
 /**
@@ -19,333 +21,343 @@
  * 浼樺寲鍐呭瓨浣跨敤锛岄伩鍏嶉暱鏃堕棿杩愯鍐呭瓨娉勬紡
  */
 public class Sendmsg {
-    // 闈欐�佷覆鍙f湇鍔″疄渚�
-    private static volatile SerialPortService serialService = null;
-    private static final AtomicBoolean isPortOpen = new AtomicBoolean(false);
-    
-    // 鏀逛负闈瀎inal锛屾敮鎸佸姩鎬佹帶鍒�
-    private static volatile boolean DEBUG_MODE = false;
-    
-    // 浣跨敤ThreadLocal淇濊瘉SimpleDateFormat绾跨▼瀹夊叏
-    private static final ThreadLocal<SimpleDateFormat> TIME_FORMATTER = 
-        ThreadLocal.withInitial(() -> new SimpleDateFormat("HH:mm:ss.SSS"));
-    
-    // 缂撳瓨瀛楃涓叉瀯寤哄櫒锛屽噺灏戝璞″垱寤�
-    private static final ThreadLocal<StringBuilder> STRING_BUILDER_CACHE = 
-        ThreadLocal.withInitial(() -> new StringBuilder(128));
-    
-    // 璁板綍娲昏穬鐨凷wingWorker锛屼究浜庣鐞�
-    private static final ConcurrentHashMap<String, SwingWorker<?, ?>> ACTIVE_WORKERS = 
-        new ConcurrentHashMap<>();
-   
-    
-    /**鍙戝崱鏈嶅姟鍣ㄦ帶鍒舵墦寮�鏌愪釜鏌滈棬璋冪敤鎸囦护
-     * @param int slotId鏌滈棬缂栧彿1-60
-     * @param int type 1鏄湇鍔″櫒鍙戝崱锛�2鏄鐞嗗憳鍙戝崱*/
-    public static boolean opendoorzhiling(int slotId,int type) {
-        lunxun.setSendChaxunzhiling(false);//鏆傚仠鏌ヨ鎸囦护
-        
-        try {
-            // 璋冪敤OpenDoor鐢熸垚寮�闂ㄦ寚浠�
-            String command = OpenDoor.openOneDoor(slotId, type);
-            boolean sendResult = Sendmsg.sendMessage(command);
-            
-            StringBuilder mesBuilder = STRING_BUILDER_CACHE.get();
-            mesBuilder.setLength(0); // 娓呯┖閲嶇敤
-            mesBuilder.append(command).append(";type").append(type).append("鎺у埗鎵撳紑").append(slotId).append("鏌滈棬");
-            String mes = mesBuilder.toString();
-            
-            Charulog.logOperation(mes);
-            if (lunxun.DEBUG_ENABLED) {
-                SystemDebugDialog.appendAsciiData(mes);
-            }
-            
-            return sendResult;
-        } finally {
-            // 纭繚鎭㈠鏌ヨ鎸囦护
-            lunxun.setSendChaxunzhiling(true);
-        }
-    }
-    
-    /**
-     * 鎵撳紑鍏ㄩ儴鍗℃Ы鐨勫叕鐢ㄩ潤鎬佹柟娉�
-     * @param type 鎿嶄綔绫诲瀷锛�1-鏈嶅姟鍣ㄥ彂鍗★紝2-绠$悊鍛樺彂鍗�
-     */
-    public static void openAllSlots(int type) {
-        lunxun.setSendChaxunzhiling(false);//鏆傚仠鏌ヨ鎸囦护
-        
-        String workerKey = "openAllSlots_" + System.currentTimeMillis();
-        
-        // 浣跨敤SwingWorker鍦ㄥ悗鍙版墽琛岋紝閬垮厤闃诲UI
-        SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
-            @Override
-            protected Void doInBackground() throws Exception {
-                // 閬嶅巻鎵�鏈夊崱妲斤紙1-60锛�
-                for (int slotId = 1; slotId <= 60 && !isCancelled(); slotId++) {
-                    try {
-                        // 鐢熸垚寮�闂ㄦ寚浠�
-                        String command = OpenDoor.openOneDoor(slotId, type);
-                        
-                        // 鍙戦�佷覆鍙f寚浠�
-                        boolean sent = Sendmsg.sendMessage(command);
-                        
-                        if (!sent) {
-                            Errlog.logOperation("鍙戦�佹寚浠ゅ埌鍗℃Ы " + slotId + " 澶辫触");
-                        }
-                        
-                        // 闂撮殧100ms锛屼絾妫�鏌ユ槸鍚﹁鍙栨秷
-                        Thread.sleep(100);
-                        
-                    } catch (InterruptedException e) {
-                        Thread.currentThread().interrupt();
-                        break; // 琚腑鏂椂閫�鍑哄惊鐜�
-                    } catch (Exception e) {
-                        Errlog.logOperation("澶勭悊鍗℃Ы " + slotId + " 鏃跺彂鐢熼敊璇�: " + e.getMessage());
-                        // 缁х画澶勭悊涓嬩竴涓崱妲斤紝涓嶄腑鏂惊鐜�
-                    }
-                }
-                return null;
-            }
-            
-            @Override
-            protected void done() {
-                try {
-                    // 娓呯悊worker寮曠敤
-                    ACTIVE_WORKERS.remove(workerKey);
-                    
-                    // 鍙�夛細瀹屾垚鍚庡彲浠ユ坊鍔犲洖璋冨鐞�
-                    if (!isCancelled()) {
-                        StringBuilder messageBuilder = STRING_BUILDER_CACHE.get();
-                        messageBuilder.setLength(0);
-                        
-                        String types = "绠$悊鍛�";
-                        if(type == 1) {
-                            types = "鏈嶅姟鍣ㄦ寚浠�";
-                        }
-                        messageBuilder.append(types).append("宸插皢鍏ㄩ儴鍗℃Ы宸茬粡鎵撳紑璇峰彇鍗�");
-                        String message = messageBuilder.toString();
-                        
-                        Dingshidialog.showTimedDialog(null, 5, message);
-                        Charulog.logOperation(message);
-                    }
-                } finally {
-                    // 纭繚鎭㈠鏌ヨ鎸囦护
-                    lunxun.setSendChaxunzhiling(true);
-                }
-            }
-        };
-        
-        // 璁板綍worker渚夸簬绠$悊
-        ACTIVE_WORKERS.put(workerKey, worker);
-        worker.execute();
-    }
-    
-    /**
-     * 鍙栨秷鎵�鏈夋鍦ㄦ墽琛岀殑浠诲姟
-     */
-    public static void cancelAllTasks() {
-        ACTIVE_WORKERS.forEach((key, worker) -> {
-            if (!worker.isDone()) {
-                worker.cancel(true);
-            }
-        });
-        ACTIVE_WORKERS.clear();
-    }
-    
-    /**
-     * 璁剧疆涓插彛鏈嶅姟瀹炰緥
-     * @param service 涓插彛鏈嶅姟瀹炰緥
-     * @param open 涓插彛鏄惁鎵撳紑
-     */
-    public static void setSerialService(SerialPortService service, boolean open) {
-        serialService = service;
-        isPortOpen.set(open);
-    }
-    
-    /**
-     * 鑾峰彇涓插彛鏈嶅姟瀹炰緥
-     */
-    public static SerialPortService getSerialService() {
-        return serialService;
-    }
-    
-    /**
-     * 鍙戦�佹秷鎭埌涓插彛锛堝甫閲嶈瘯鏈哄埗锛�
-     */
-    public static boolean sendMessage(String message) {
-        if (!isPortOpen.get() || serialService == null) {
-            Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鏈墦寮�锛屾棤娉曞彂閫佹暟鎹�");
-            return false;
-        }
-        
-        if (message == null || message.trim().isEmpty()) {
-            Errlog.logOperation("[" + getCurrentTime() + "] 鍙戦�佹暟鎹负绌�");
-            return false;
-        }
-        
-        String text = message.trim();
-        int retryCount = 0;
-        final int MAX_RETRY = 2;
-        
-        while (retryCount <= MAX_RETRY) {
-            try {
-                byte[] data = hexStringToByteArray(text);
-                if (data == null) {
-                    Errlog.logOperation("[" + getCurrentTime() + "] HEX杞崲澶辫触锛屾暟鎹�: " + text);
-                    return false;
-                }
-                
-                boolean sendResult = serialService.send(data);
-                
-                if (sendResult) {
-                    if (DEBUG_MODE) {
-                        System.out.println("[" + getCurrentTime() + "] 鍙戦�佹垚鍔�: " + text.toUpperCase());
-                    }
-                    return true;
-                } else {
-                    retryCount++;
-                    if (retryCount <= MAX_RETRY) {
-                        Errlog.logOperation("[" + getCurrentTime() + "] 鍙戦�佸け璐ワ紝姝e湪閲嶈瘯 (" + retryCount + "/" + MAX_RETRY + ")");
-                        try {
-                            Thread.sleep(50); // 閲嶈瘯鍓嶇瓑寰�
-                        } catch (InterruptedException e) {
-                            Thread.currentThread().interrupt();
-                            break;
-                        }
-                    } else {
-                        if (DEBUG_MODE) {
-                            Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鍙戦�佸け璐ワ紝鎸囦护: " + text.toUpperCase());
-                            Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鐘舵�� - 鎵撳紑: " + isPortOpen.get() + ", 鏈嶅姟: " + (serialService != null));
-                            if (serialService != null) {
-                                Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鏈嶅姟鐘舵�� - 鏄惁鎵撳紑: " + serialService.isOpen());
-                            }
-                        }
-                        return false;
-                    }
-                }
-            } catch (Exception e) {
-                Errlog.logOperation("[" + getCurrentTime() + "] 鍙戦�佸紓甯革紝鎸囦护: " + text.toUpperCase());
-                if (DEBUG_MODE) {
-                    e.printStackTrace();
-                }
-                return false;
-            }
-        }
-        
-        return false;
-    }
-    
-    /**
-     * 妫�鏌ヤ覆鍙f槸鍚﹀凡鎵撳紑
-     * @return 涓插彛鎵撳紑鐘舵��
-     */
-    public static boolean isPortOpen() {
-        boolean open = isPortOpen.get() && serialService != null;
-        if (!open && DEBUG_MODE) {
-            Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鐘舵�佹鏌�: 鏈墦寮�");
-        }
-        return open;
-    }
-    
-    /**
-     * HEX瀛楃涓茶浆瀛楄妭鏁扮粍
-     * @param s HEX瀛楃涓�
-     * @return 瀛楄妭鏁扮粍
-     */
-    private static byte[] hexStringToByteArray(String s) {
-        if (s == null || s.isEmpty()) {
-            return new byte[0];
-        }
-        
-        s = s.replaceAll("\\s+", ""); // 绉婚櫎绌烘牸
-        int len = s.length();
-        if (len % 2 != 0) {
-            throw new IllegalArgumentException("HEX瀛楃涓查暱搴﹀繀椤讳负鍋舵暟锛屽綋鍓嶉暱搴�: " + len + ", 鏁版嵁: " + s);
-        }
-        
-        byte[] data = new byte[len / 2];
-        for (int i = 0; i < len; i += 2) {
-            int high = Character.digit(s.charAt(i), 16);
-            int low = Character.digit(s.charAt(i + 1), 16);
-            
-            if (high == -1 || low == -1) {
-                throw new IllegalArgumentException("鏃犳晥鐨凥EX瀛楃: '" + s.charAt(i) + s.charAt(i + 1) + "' 鍦ㄤ綅缃� " + i + "-" + (i+1) + ", 瀹屾暣鏁版嵁: " + s);
-            }
-            
-            data[i / 2] = (byte) ((high << 4) + low);
-        }
-        return data;
-    }
-    
-    /**
-     * 瀛楄妭鏁扮粍杞琀EX瀛楃涓�
-     * @param bytes 瀛楄妭鏁扮粍
-     * @return HEX瀛楃涓�
-     */
-    public static String bytesToHex(byte[] bytes) {
-        if (bytes == null || bytes.length == 0) {
-            return "";
-        }
-        
-        StringBuilder sb = STRING_BUILDER_CACHE.get();
-        sb.setLength(0);
-        for (byte b : bytes) {
-            sb.append(String.format("%02x", b));
-        }
-        return sb.toString();
-    }
-    
-    /**
-     * 鑾峰彇褰撳墠鏃堕棿瀛楃涓�
-     * @return 鏃堕棿瀛楃涓�
-     */
-    private static String getCurrentTime() {
-        return TIME_FORMATTER.get().format(new Date());
-    }
-    
-    /**
-     * 鍚敤璋冭瘯妯″紡
-     */
-    public static void enableDebugMode() {
-        DEBUG_MODE = true;
-        System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡宸插惎鐢�");
-    }
-    
-    /**
-     * 绂佺敤璋冭瘯妯″紡
-     */
-    public static void disableDebugMode() {
-        DEBUG_MODE = false;
-        System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡宸茬鐢�");
-    }
-    
-    /**
-     * 璁剧疆璋冭瘯妯″紡
-     */
-    public static void setDebugMode(boolean debug) {
-        DEBUG_MODE = debug;
-        System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡: " + (debug ? "鍚敤" : "绂佺敤"));
-    }
-    
-    /**
-     * 娓呯悊璧勬簮锛岄槻姝㈠唴瀛樻硠婕�
-     */
-    public static void cleanup() {
-        cancelAllTasks();
-        
-        // 娓呯悊ThreadLocal璧勬簮
-        TIME_FORMATTER.remove();
-        STRING_BUILDER_CACHE.remove();
-        
-        if (DEBUG_MODE) {
-            System.out.println("[" + getCurrentTime() + "] Sendmsg璧勬簮娓呯悊瀹屾垚");
-        }
-    }
-    
-    /**
-     * 鑾峰彇娲昏穬浠诲姟鏁伴噺
-     */
-    public static int getActiveTaskCount() {
-        return ACTIVE_WORKERS.size();
-    }
+	// 闈欐�佷覆鍙f湇鍔″疄渚�
+	private static volatile SerialPortService serialService = null;
+	private static final AtomicBoolean isPortOpen = new AtomicBoolean(false);
+
+	// 鏀逛负闈瀎inal锛屾敮鎸佸姩鎬佹帶鍒�
+	private static volatile boolean DEBUG_MODE = false;
+
+	// 浣跨敤ThreadLocal淇濊瘉SimpleDateFormat绾跨▼瀹夊叏
+	private static final ThreadLocal<SimpleDateFormat> TIME_FORMATTER = 
+			ThreadLocal.withInitial(() -> new SimpleDateFormat("HH:mm:ss.SSS"));
+
+	// 缂撳瓨瀛楃涓叉瀯寤哄櫒锛屽噺灏戝璞″垱寤�
+	private static final ThreadLocal<StringBuilder> STRING_BUILDER_CACHE = 
+			ThreadLocal.withInitial(() -> new StringBuilder(128));
+
+	// 璁板綍娲昏穬鐨凷wingWorker锛屼究浜庣鐞�
+	private static final ConcurrentHashMap<String, SwingWorker<?, ?>> ACTIVE_WORKERS = 
+			new ConcurrentHashMap<>();
+
+
+	/**鍙戝崱鏈嶅姟鍣ㄦ帶鍒舵墦寮�鏌愪釜鏌滈棬璋冪敤鎸囦护
+	 * @param int slotId鏌滈棬缂栧彿1-60
+	 * @param int type 1鏄湇鍔″櫒鍙戝崱锛�2鏄鐞嗗憳鍙戝崱*/
+	public static boolean opendoorzhiling(int slotId,int type) {
+		lunxun.setSendChaxunzhiling(false);//鏆傚仠鏌ヨ鎸囦护
+
+		try {
+			// 璋冪敤OpenDoor鐢熸垚寮�闂ㄦ寚浠�
+			String command = OpenDoor.openOneDoor(slotId, type);
+			boolean sendResult = Sendmsg.sendMessage(command);
+
+			StringBuilder mesBuilder = STRING_BUILDER_CACHE.get();
+			mesBuilder.setLength(0); // 娓呯┖閲嶇敤
+			mesBuilder.append(command).append(";type").append(type).append("鎺у埗鎵撳紑").append(slotId).append("鏌滈棬");
+			String mes = mesBuilder.toString();
+
+			// 鑾峰彇褰撳墠鏃堕棿
+			String currentTime = TimestampUtil.getTimestamp();
+			// 鏇存柊鍗℃Ы鐨勬敹鍒板彂鍗℃寚浠ゆ椂闂�
+			SlotManager.slotArray[slotId-1].setReceiveCardCommandTime(currentTime);
+
+			Charulog.logOperation(mes);
+			if (lunxun.DEBUG_ENABLED) {
+				SystemDebugDialog.appendAsciiData(mes);
+			}
+
+			return sendResult;
+		} finally {
+			// 纭繚鎭㈠鏌ヨ鎸囦护
+			lunxun.setSendChaxunzhiling(true);
+		}
+	}
+
+	/**
+	 * 鎵撳紑鍏ㄩ儴鍗℃Ы鐨勫叕鐢ㄩ潤鎬佹柟娉�
+	 * @param type 鎿嶄綔绫诲瀷锛�1-鏈嶅姟鍣ㄥ彂鍗★紝2-绠$悊鍛樺彂鍗�
+	 */
+	public static void openAllSlots(int type) {
+		lunxun.setSendChaxunzhiling(false);//鏆傚仠鏌ヨ鎸囦护
+
+		String workerKey = "openAllSlots_" + System.currentTimeMillis();
+
+		// 浣跨敤SwingWorker鍦ㄥ悗鍙版墽琛岋紝閬垮厤闃诲UI
+		SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
+			@Override
+			protected Void doInBackground() throws Exception {
+				// 閬嶅巻鎵�鏈夊崱妲斤紙1-60锛�
+				for (int slotId = 1; slotId <= 60 && !isCancelled(); slotId++) {
+					try {
+						// 鐢熸垚寮�闂ㄦ寚浠�
+						String command = OpenDoor.openOneDoor(slotId, type);
+
+						// 鍙戦�佷覆鍙f寚浠�
+						boolean sent = Sendmsg.sendMessage(command);
+
+						if (!sent) {
+							Errlog.logOperation("鍙戦�佹寚浠ゅ埌鍗℃Ы " + slotId + " 澶辫触");
+						}else {
+							// 鑾峰彇褰撳墠鏃堕棿
+							String currentTime = TimestampUtil.getTimestamp();
+							// 鏇存柊鍗℃Ы鐨勬敹鍒板彂鍗℃寚浠ゆ椂闂�
+							SlotManager.slotArray[slotId-1].setReceiveCardCommandTime(currentTime);
+						}
+
+						// 闂撮殧100ms锛屼絾妫�鏌ユ槸鍚﹁鍙栨秷
+						Thread.sleep(100);
+
+					} catch (InterruptedException e) {
+						Thread.currentThread().interrupt();
+						break; // 琚腑鏂椂閫�鍑哄惊鐜�
+					} catch (Exception e) {
+						Errlog.logOperation("澶勭悊鍗℃Ы " + slotId + " 鏃跺彂鐢熼敊璇�: " + e.getMessage());
+						// 缁х画澶勭悊涓嬩竴涓崱妲斤紝涓嶄腑鏂惊鐜�
+					}
+				}
+				return null;
+			}
+
+			@Override
+			protected void done() {
+				try {
+					// 娓呯悊worker寮曠敤
+					ACTIVE_WORKERS.remove(workerKey);
+
+					// 鍙�夛細瀹屾垚鍚庡彲浠ユ坊鍔犲洖璋冨鐞�
+					if (!isCancelled()) {
+						StringBuilder messageBuilder = STRING_BUILDER_CACHE.get();
+						messageBuilder.setLength(0);
+
+						String types = "绠$悊鍛�";
+						if(type == 1) {
+							types = "鏈嶅姟鍣ㄦ寚浠�";
+						}
+						messageBuilder.append(types).append("宸插皢鍏ㄩ儴鍗℃Ы宸茬粡鎵撳紑璇峰彇鍗�");
+						String message = messageBuilder.toString();
+
+						Dingshidialog.showTimedDialog(null, 5, message);
+						Charulog.logOperation(message);
+					}
+				} finally {
+					// 纭繚鎭㈠鏌ヨ鎸囦护
+					lunxun.setSendChaxunzhiling(true);
+				}
+			}
+		};
+
+		// 璁板綍worker渚夸簬绠$悊
+		ACTIVE_WORKERS.put(workerKey, worker);
+		worker.execute();
+	}
+
+	/**
+	 * 鍙栨秷鎵�鏈夋鍦ㄦ墽琛岀殑浠诲姟
+	 */
+	public static void cancelAllTasks() {
+		ACTIVE_WORKERS.forEach((key, worker) -> {
+			if (!worker.isDone()) {
+				worker.cancel(true);
+			}
+		});
+		ACTIVE_WORKERS.clear();
+	}
+
+	/**
+	 * 璁剧疆涓插彛鏈嶅姟瀹炰緥
+	 * @param service 涓插彛鏈嶅姟瀹炰緥
+	 * @param open 涓插彛鏄惁鎵撳紑
+	 */
+	public static void setSerialService(SerialPortService service, boolean open) {
+		serialService = service;
+		isPortOpen.set(open);
+	}
+
+	/**
+	 * 鑾峰彇涓插彛鏈嶅姟瀹炰緥
+	 */
+	public static SerialPortService getSerialService() {
+		return serialService;
+	}
+
+	/**
+	 * 鍙戦�佹秷鎭埌涓插彛锛堝甫閲嶈瘯鏈哄埗锛�
+	 */
+	public static boolean sendMessage(String message) {
+		if (!isPortOpen.get() || serialService == null) {
+			Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鏈墦寮�锛屾棤娉曞彂閫佹暟鎹�");
+			return false;
+		}
+
+		if (message == null || message.trim().isEmpty()) {
+			Errlog.logOperation("[" + getCurrentTime() + "] 鍙戦�佹暟鎹负绌�");
+			return false;
+		}
+
+		String text = message.trim();
+		int retryCount = 0;
+		final int MAX_RETRY = 2;
+
+		while (retryCount <= MAX_RETRY) {
+			try {
+				byte[] data = hexStringToByteArray(text);
+				if (data == null) {
+					Errlog.logOperation("[" + getCurrentTime() + "] HEX杞崲澶辫触锛屾暟鎹�: " + text);
+					return false;
+				}
+
+				boolean sendResult = serialService.send(data);
+
+				if (sendResult) {
+					if (DEBUG_MODE) {
+						System.out.println("[" + getCurrentTime() + "] 鍙戦�佹垚鍔�: " + text.toUpperCase());
+					}
+					return true;
+				} else {
+					retryCount++;
+					if (retryCount <= MAX_RETRY) {
+						Errlog.logOperation("[" + getCurrentTime() + "] 鍙戦�佸け璐ワ紝姝e湪閲嶈瘯 (" + retryCount + "/" + MAX_RETRY + ")");
+						try {
+							Thread.sleep(50); // 閲嶈瘯鍓嶇瓑寰�
+						} catch (InterruptedException e) {
+							Thread.currentThread().interrupt();
+							break;
+						}
+					} else {
+						if (DEBUG_MODE) {
+							Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鍙戦�佸け璐ワ紝鎸囦护: " + text.toUpperCase());
+							Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鐘舵�� - 鎵撳紑: " + isPortOpen.get() + ", 鏈嶅姟: " + (serialService != null));
+							if (serialService != null) {
+								Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鏈嶅姟鐘舵�� - 鏄惁鎵撳紑: " + serialService.isOpen());
+							}
+						}
+						return false;
+					}
+				}
+			} catch (Exception e) {
+				Errlog.logOperation("[" + getCurrentTime() + "] 鍙戦�佸紓甯革紝鎸囦护: " + text.toUpperCase());
+				if (DEBUG_MODE) {
+					e.printStackTrace();
+				}
+				return false;
+			}
+		}
+
+		return false;
+	}
+
+	/**
+	 * 妫�鏌ヤ覆鍙f槸鍚﹀凡鎵撳紑
+	 * @return 涓插彛鎵撳紑鐘舵��
+	 */
+	public static boolean isPortOpen() {
+		boolean open = isPortOpen.get() && serialService != null;
+		if (!open && DEBUG_MODE) {
+			Errlog.logOperation("[" + getCurrentTime() + "] 涓插彛鐘舵�佹鏌�: 鏈墦寮�");
+		}
+		return open;
+	}
+
+	/**
+	 * HEX瀛楃涓茶浆瀛楄妭鏁扮粍
+	 * @param s HEX瀛楃涓�
+	 * @return 瀛楄妭鏁扮粍
+	 */
+	private static byte[] hexStringToByteArray(String s) {
+		if (s == null || s.isEmpty()) {
+			return new byte[0];
+		}
+
+		s = s.replaceAll("\\s+", ""); // 绉婚櫎绌烘牸
+		int len = s.length();
+		if (len % 2 != 0) {
+			throw new IllegalArgumentException("HEX瀛楃涓查暱搴﹀繀椤讳负鍋舵暟锛屽綋鍓嶉暱搴�: " + len + ", 鏁版嵁: " + s);
+		}
+
+		byte[] data = new byte[len / 2];
+		for (int i = 0; i < len; i += 2) {
+			int high = Character.digit(s.charAt(i), 16);
+			int low = Character.digit(s.charAt(i + 1), 16);
+
+			if (high == -1 || low == -1) {
+				throw new IllegalArgumentException("鏃犳晥鐨凥EX瀛楃: '" + s.charAt(i) + s.charAt(i + 1) + "' 鍦ㄤ綅缃� " + i + "-" + (i+1) + ", 瀹屾暣鏁版嵁: " + s);
+			}
+
+			data[i / 2] = (byte) ((high << 4) + low);
+		}
+		return data;
+	}
+
+	/**
+	 * 瀛楄妭鏁扮粍杞琀EX瀛楃涓�
+	 * @param bytes 瀛楄妭鏁扮粍
+	 * @return HEX瀛楃涓�
+	 */
+	public static String bytesToHex(byte[] bytes) {
+		if (bytes == null || bytes.length == 0) {
+			return "";
+		}
+
+		StringBuilder sb = STRING_BUILDER_CACHE.get();
+		sb.setLength(0);
+		for (byte b : bytes) {
+			sb.append(String.format("%02x", b));
+		}
+		return sb.toString();
+	}
+
+	/**
+	 * 鑾峰彇褰撳墠鏃堕棿瀛楃涓�
+	 * @return 鏃堕棿瀛楃涓�
+	 */
+	private static String getCurrentTime() {
+		return TIME_FORMATTER.get().format(new Date());
+	}
+
+	/**
+	 * 鍚敤璋冭瘯妯″紡
+	 */
+	public static void enableDebugMode() {
+		DEBUG_MODE = true;
+		System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡宸插惎鐢�");
+	}
+
+	/**
+	 * 绂佺敤璋冭瘯妯″紡
+	 */
+	public static void disableDebugMode() {
+		DEBUG_MODE = false;
+		System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡宸茬鐢�");
+	}
+
+	/**
+	 * 璁剧疆璋冭瘯妯″紡
+	 */
+	public static void setDebugMode(boolean debug) {
+		DEBUG_MODE = debug;
+		System.out.println("[" + getCurrentTime() + "] Sendmsg璋冭瘯妯″紡: " + (debug ? "鍚敤" : "绂佺敤"));
+	}
+
+	/**
+	 * 娓呯悊璧勬簮锛岄槻姝㈠唴瀛樻硠婕�
+	 */
+	public static void cleanup() {
+		cancelAllTasks();
+
+		// 娓呯悊ThreadLocal璧勬簮
+		TIME_FORMATTER.remove();
+		STRING_BUILDER_CACHE.remove();
+
+		if (DEBUG_MODE) {
+			System.out.println("[" + getCurrentTime() + "] Sendmsg璧勬簮娓呯悊瀹屾垚");
+		}
+	}
+
+	/**
+	 * 鑾峰彇娲昏穬浠诲姟鏁伴噺
+	 */
+	public static int getActiveTaskCount() {
+		return ACTIVE_WORKERS.size();
+	}
 }
\ No newline at end of file

--
Gitblit v1.10.0