From 3b00ed30d7a86c1ea577f00c6b14e045a98733f6 Mon Sep 17 00:00:00 2001 From: hulk Date: Sat, 10 Oct 2026 00:06:42 +0800 Subject: [PATCH] M2 protocol: replace toggle with idempotent explicit control --- Src/command.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/Src/command.c b/Src/command.c index cac955c..22490fd 100644 --- a/Src/command.c +++ b/Src/command.c @@ -335,10 +335,12 @@ static bool __attribute__((unused)) find_parameter_value(const uint8_t *cmd, uin * - 带参数命令:M<数字>S<参数>(如 M100S123,参数为十进制) * * 支持的命令(FilmForge): - * - M1: 单次查询温度值(payload 为 ASCII 摄氏度,如 "23.8";OPEN/SHRT=故障) - * - M888: 软件复位 - * - M999: 输出固件版本号(v<板卡码>.<主>.<次>.<修订>) - * - M9999: 进入 OTA 模式(复位进 Bootloader) + * - M1: 单次查询温度值(payload 为 ASCII 摄氏度,如 "23.8";OPEN/SHRT=故障) + * - M2: 查询连续上报状态(payload: "ON " 或 "OFF") + * - M2S: 设置连续上报间隔(100~60000)并开启;M2S0 停止 + * - M888: 软件复位 + * - M999: 输出固件版本号(v<板卡码>.<主>.<次>.<修订>) + * - M9999: 进入 OTA 模式(复位进 Bootloader) * * 具体业务命令(加热/测温/控温等)待定,在 switch 中按需扩展。 * @@ -399,18 +401,26 @@ void handle_command(const uint8_t *frame, uint8_t len) { return; /* ========================================== - * M2 连续温度上报开关(画曲线用) - * M2 : 切换连续上报 使能/停止(默认间隔 500ms) - * M2S : 设置上报间隔(100~60000,10ms 步进)并开启上报 + * M2 连续温度上报控制(画曲线用,全部幂等可重发) + * M2 : 查询上报状态(payload: "ON " 或 "OFF") + * M2S : 100~60000 设置上报间隔(10ms 步进)并开启 + * M2S0 : 停止上报 * 上报帧格式与 M1 响应相同:B5 F0 LEN [ASCII 摄氏度] CRC + * 注意:勿用"翻转"语义——链路 ack 丢失重发时翻转类命令会状态错乱, + * 本命令族全部为幂等的显式设置。 * ========================================== */ case 2u: { uint32_t period = 0; if (find_parameter_value(&cmd[cmd_index], (uint8_t)(cmd_len - cmd_index), 'S', &period)) { - /* 带参数:设置间隔并开启 */ - if (period >= 100U && period <= 60000U) { + if (period == 0U) { + /* M2S0: 停止上报 */ + s_temp_report_enabled = false; + s_temp_report_counter = 0; + send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok)); + } else if (period >= 100U && period <= 60000U) { + /* M2S: 设置间隔并开启 */ s_temp_report_period_ms = (uint16_t)period; s_temp_report_enabled = true; s_temp_report_counter = 0; @@ -419,10 +429,15 @@ void handle_command(const uint8_t *frame, uint8_t len) { send_response(RESP_TYPE_PARAM_ERR, s_report_status_err, sizeof(s_report_status_err)); } } else { - /* 无参数:切换使能 */ - s_temp_report_enabled = !s_temp_report_enabled; - s_temp_report_counter = 0; - send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok)); + /* M2: 查询上报状态 */ + char state_str[12]; + uint8_t n; + if (s_temp_report_enabled) { + n = (uint8_t)sprintf(state_str, "ON %u", s_temp_report_period_ms); + } else { + n = (uint8_t)sprintf(state_str, "OFF"); + } + send_response(RESP_TYPE_OK, (const uint8_t *)state_str, n); } COMMAND_DEBUG("M2 report %s, period=%ums", s_temp_report_enabled ? "ON" : "OFF", s_temp_report_period_ms);