From cac9dd41aa47bee7534ea4cb51b6b04b20f4e23c Mon Sep 17 00:00:00 2001 From: Zhanghu Date: Thu, 5 Mar 2026 14:47:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E4=B8=B2=E5=8F=A3=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E4=B8=8D=E4=B8=BA=E7=A9=BA=E6=97=B6=E5=88=99=E5=BC=80?= =?UTF-8?q?=E5=85=B3=E4=B8=80=E6=AC=A1=E8=AE=BE=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_dashboard/changelog.md | 2 +- .../activity/BuildingDashboardActivity.java | 23 ++++++-- .../perferences/PreferenceConfiguration.java | 6 ++ .../dashboard/serial/SerialPortDetector.java | 59 +++++++++++++++++++ .../src/main/res/xml/root_preferences.xml | 5 ++ 5 files changed, 90 insertions(+), 5 deletions(-) diff --git a/app_dashboard/changelog.md b/app_dashboard/changelog.md index 8b931e4..dfa1e49 100644 --- a/app_dashboard/changelog.md +++ b/app_dashboard/changelog.md @@ -16,7 +16,7 @@ author: * [dashboardclient_1.0.5.apk](dashboardclient_1.0.5.apk) #### 更新记录 -* 仅串口路径为空时才自动检测设备 +* App 启动时如果串口路径为空时自动检测设备, 否则开关一次设备 * 设置界面可清除串口路径 ### [1.0.4.23] - 2026.1.5 diff --git a/app_dashboard/src/main/java/cn/ykbox/dashboard/activity/BuildingDashboardActivity.java b/app_dashboard/src/main/java/cn/ykbox/dashboard/activity/BuildingDashboardActivity.java index 98d0a38..531107b 100644 --- a/app_dashboard/src/main/java/cn/ykbox/dashboard/activity/BuildingDashboardActivity.java +++ b/app_dashboard/src/main/java/cn/ykbox/dashboard/activity/BuildingDashboardActivity.java @@ -119,19 +119,34 @@ public class BuildingDashboardActivity extends FullscreenActivity { private void initSerialPort() { String portPath = PreferenceConfiguration.getSerialPortPath(this); + int baudRate = PreferenceConfiguration.getSerialPortBaudRate(this); + boolean sendPowerOnCmd = PreferenceConfiguration.getSendPowerOnCmd(this); + + // 初始化串口检测器 + detector = new SerialPortDetector(portPath, baudRate); // 只有 portPath 为空时才进行串口检测 if (TextUtils.isEmpty(portPath)) { - int baudRate = PreferenceConfiguration.getSerialPortBaudRate(this); - // 初始化串口检测器 - detector = new SerialPortDetector(portPath, baudRate); - Log.i(TAG, "Starting serial port detection/verification..."); // 参数 true 表示确保设备开机(发送两个命令) // 参数 false 表示仅检测串口(任一命令有响应即可) startSerialPortDetection(true); } else { Log.i(TAG, "Using saved serial port path: " + portPath); + + // 如果勾选了"发送打开电源指令",则先发送关闭再发送打开 + if (sendPowerOnCmd) { + Log.i(TAG, "Sending power on sequence..."); + new Thread(() -> { + detector.sendPowerOffCommand(); + try { + Thread.sleep(3000); // 等待 3 秒 + } catch (InterruptedException e) { + Log.e(TAG, "Sleep interrupted", e); + } + detector.sendPowerOnCommand(); + }).start(); + } } } diff --git a/app_dashboard/src/main/java/cn/ykbox/dashboard/perferences/PreferenceConfiguration.java b/app_dashboard/src/main/java/cn/ykbox/dashboard/perferences/PreferenceConfiguration.java index af93598..5039df1 100644 --- a/app_dashboard/src/main/java/cn/ykbox/dashboard/perferences/PreferenceConfiguration.java +++ b/app_dashboard/src/main/java/cn/ykbox/dashboard/perferences/PreferenceConfiguration.java @@ -13,6 +13,7 @@ public class PreferenceConfiguration { private static final String KEY_SERIAL_PORT_PATH = "k_serial_port_path"; private static final String KEY_SERIAL_PORT_BAUD_RATE = "k_serial_baud"; + private static final String KEY_SEND_POWER_ON_CMD = "k_send_power_on_cmd"; public static String getSerialPortPath(Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); @@ -29,4 +30,9 @@ public class PreferenceConfiguration { String baud = prefs.getString(KEY_SERIAL_PORT_BAUD_RATE, "9600"); return Integer.parseInt(baud); } + + public static boolean getSendPowerOnCmd(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getBoolean(KEY_SEND_POWER_ON_CMD, true); + } } diff --git a/app_dashboard/src/main/java/cn/ykbox/dashboard/serial/SerialPortDetector.java b/app_dashboard/src/main/java/cn/ykbox/dashboard/serial/SerialPortDetector.java index a5c1c6f..a6d7c96 100644 --- a/app_dashboard/src/main/java/cn/ykbox/dashboard/serial/SerialPortDetector.java +++ b/app_dashboard/src/main/java/cn/ykbox/dashboard/serial/SerialPortDetector.java @@ -45,6 +45,51 @@ public class SerialPortDetector { this.callback = callback; } + /** + * 发送关闭设备指令到已保存的串口(不等待响应) + */ + public void sendPowerOffCommand() { + sendCommand(TEST_CMD_OFF); + } + + /** + * 发送打开设备指令到已保存的串口(不等待响应) + */ + public void sendPowerOnCommand() { + sendCommand(TEST_CMD_ON); + } + + /** + * 发送指令到已保存的串口(不等待响应) + * @param commandHex 十六进制命令字符串 + */ + private void sendCommand(String commandHex) { + if (TextUtils.isEmpty(savedPath)) { + Log.w(TAG, "No serial port path configured"); + return; + } + + new Thread(() -> { + SerialHelper serialHelper = null; + try { + serialHelper = new SimpleSerialHelper(savedPath, baudRate); + serialHelper.open(); + serialHelper.sendHex(commandHex); + Log.d(TAG, "Sent command: " + commandHex); + } catch (Exception e) { + Log.e(TAG, "Error sending command: " + e.getMessage()); + } finally { + if (serialHelper != null) { + try { + serialHelper.close(); + } catch (Exception e) { + Log.e(TAG, "Error closing port: " + e.getMessage()); + } + } + } + }).start(); + } + /** * 开始串口检测(智能检测) * 先验证已保存的串口,如果不可用再进行全局检测 @@ -226,6 +271,20 @@ public class SerialPortDetector { } } + /** + * 简单的串口 Helper,用于只发送命令不等待响应 + */ + private static class SimpleSerialHelper extends SerialHelper { + public SimpleSerialHelper(String sPort, int iBaudRate) { + super(sPort, iBaudRate); + } + + @Override + protected void onDataReceived(ComBean comBean) { + // 不需要处理响应 + } + } + /** * 内部测试用的 SerialHelper */ diff --git a/app_dashboard/src/main/res/xml/root_preferences.xml b/app_dashboard/src/main/res/xml/root_preferences.xml index 8aa2dd4..c9c7c2d 100644 --- a/app_dashboard/src/main/res/xml/root_preferences.xml +++ b/app_dashboard/src/main/res/xml/root_preferences.xml @@ -25,6 +25,11 @@ app:title="串口波特率" app:defaultValue="9600" app:useSimpleSummaryProvider="true" /> +