From 48e832bf5b9da40df638c9baf16836b93b918181 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Wed, 13 Aug 2025 20:16:20 +0800 Subject: [PATCH] change respone function to GD32E23x_standard_peripheral lib --- Src/command.c | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/Src/command.c b/Src/command.c index d6fc362..d6d5d89 100644 --- a/Src/command.c +++ b/Src/command.c @@ -16,6 +16,7 @@ #include #include #include "board_config.h" +#include "gd32e23x_usart.h" /* ============================================================================ * 协议格式说明 @@ -136,14 +137,14 @@ static uint8_t command_sum_crc_calc(const uint8_t *data, uint8_t len) } /** - * @brief 发送协议响应帧。 + * @brief 发送协议响应帧(使用GD32E230标准库)。 * @details 构造并发送格式为 B5 TYPE LEN [payload] CRC 的响应帧, * 自动计算CRC校验值并通过串口输出。 * @param type 响应类型码(如 RESP_TYPE_OK, RESP_TYPE_CRC_ERR 等)。 - * @param payload 指向响应数据的缓冲区,可为 NULL。 - * @param len 响应数据长度(字节),payload为NULL时填充0。 + * @param payload 指向响应数据的缓冲区,当len为0时可为NULL。 + * @param len 响应数据长度(字节),为0时不复制payload数据。 * @note 内部使用固定大小缓冲区,超长响应将被丢弃。 - * @warning 通过 printf 发送,确保串口已正确初始化。 + * @warning 使用GD32E230标准库函数发送,确保串口已正确初始化。 * @ingroup Command */ static void send_response(uint8_t type, const uint8_t *payload, uint8_t len) @@ -155,15 +156,33 @@ static void send_response(uint8_t type, const uint8_t *payload, uint8_t len) buf[0] = RESP_HEADER; buf[1] = type; buf[2] = len; - for (uint8_t i = 0; i < len; i++) buf[3 + i] = payload ? payload[i] : 0; - buf[buf_len - 1] = command_sum_crc_calc(buf, buf_len); - for (uint8_t i = 0; i < buf_len; i++) { - printf("%c", buf[i]); + // 简化逻辑:只有当len > 0且payload非空时才复制数据 + if (len > 0 && payload != NULL) { + for (uint8_t i = 0; i < len; i++) { + buf[3 + i] = payload[i]; + } } - // 刷新缓冲区 - fflush(stdout); + buf[buf_len - 1] = command_sum_crc_calc(buf, buf_len); + + // 使用GD32E230标准库函数逐字节发送(标准库实现) + for (uint8_t i = 0; i < buf_len; i++) { + // 等待发送缓冲区空 + while (usart_flag_get(RS485_PHY, USART_FLAG_TBE) == RESET) {} + usart_data_transmit(RS485_PHY, buf[i]); + } + + // 等待发送完成 + while (usart_flag_get(RS485_PHY, USART_FLAG_TC) == RESET) {} + + // // 使用printf发送(通过重定向到串口) + // for (uint8_t i = 0; i < buf_len; i++) { + // printf("%c", buf[i]); + // } + + // // 刷新缓冲区 + // fflush(stdout); } /**