diff --git a/app/src/main/java/com/ouxuan/oxface/debug/Ox485DebugHelper.java b/app/src/main/java/com/ouxuan/oxface/debug/Ox485DebugHelper.java index 3a84173..92a9a15 100644 --- a/app/src/main/java/com/ouxuan/oxface/debug/Ox485DebugHelper.java +++ b/app/src/main/java/com/ouxuan/oxface/debug/Ox485DebugHelper.java @@ -28,7 +28,7 @@ public class Ox485DebugHelper { String status = ox485.get485Status(); LogManager.logInfo(TAG, "当前状态: " + status); - // 2. 连接诊断 + // 2. 连接诊断 - 使用Ox485的内部诊断方法 LogManager.logInfo(TAG, "步骤2: 连接诊断"); ox485.diagnose485Connection(); diff --git a/app/src/main/java/com/ouxuan/oxface/device/Ox485.java b/app/src/main/java/com/ouxuan/oxface/device/Ox485.java index 6c26ab5..4900f2e 100644 --- a/app/src/main/java/com/ouxuan/oxface/device/Ox485.java +++ b/app/src/main/java/com/ouxuan/oxface/device/Ox485.java @@ -182,7 +182,7 @@ public class Ox485 { */ private void sendCommandAndWaitResponse(PeopleNumCallback callback) { // 发送前检查串口状态 - if (!serialPortManager.isOpened()) { + if (!isSerialPortReallyOpen()) { String errorMsg = "串口未打开,无法发送命令"; LogManager.logError(TAG, errorMsg); isSerialPortOpen = false; @@ -289,7 +289,7 @@ public class Ox485 { long elapsedTime = System.currentTimeMillis() - startTime; String errorMsg = "485人数查询超时(等待时间: " + elapsedTime + "ms)"; LogManager.logError(TAG, errorMsg); - LogManager.logError(TAG, "485超时详情 - 串口状态: " + (serialPortManager != null ? serialPortManager.isOpened() : "null") + + LogManager.logError(TAG, "485超时详情 - 串口状态: " + (isSerialPortReallyOpen() ? "已打开" : "未打开") + ", 内部连接状态: " + isSerialPortOpen + ", 最后成功时间: " + (lastSuccessTime > 0 ? new java.text.SimpleDateFormat("HH:mm:ss.SSS").format(new java.util.Date(lastSuccessTime)) : "无")); @@ -420,7 +420,7 @@ public class Ox485 { // 2. 检查串口管理器状态 LogManager.logInfo(TAG, "诊断2 - 串口管理器状态:"); - LogManager.logInfo(TAG, " - 串口是否打开: " + (serialPortManager != null ? serialPortManager.isOpened() : "null")); + LogManager.logInfo(TAG, " - 串口是否打开: " + (isSerialPortReallyOpen() ? "已打开" : "未打开")); LogManager.logInfo(TAG, " - 内部连接状态: " + isSerialPortOpen); LogManager.logInfo(TAG, " - 是否连接中: " + isConnecting); @@ -646,7 +646,7 @@ public class Ox485 { } // 验证串口是否真正打开 - if (!serialPortManager.isOpened()) { + if (!isSerialPortReallyOpen()) { isConnecting = false; String errorMsg = "485串口打开后状态检查失败,串口可能未正确初始化"; LogManager.logError(TAG, errorMsg); @@ -747,4 +747,14 @@ public class Ox485 { // 15: 0x0F, 3: 0x03, 4: 0x04, 0: 0x00 return (bytes[0] == 15 && bytes[1] == 3 && bytes[2] == 4 && bytes[3] == 0); } + + /** + * 检查串口是否真正打开 + * 由于 SerialPortManager 没有 isOpened() 方法, + * 我们通过检查内部状态变量来判断 + * @return true表示串口已打开 + */ + private boolean isSerialPortReallyOpen() { + return isSerialPortOpen && serialPortManager != null; + } } \ No newline at end of file