generated from hulk/gd32e23x_template_cmake_vscode
change respone function to GD32E23x_standard_peripheral lib
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "board_config.h"
|
#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 的响应帧,
|
* @details 构造并发送格式为 B5 TYPE LEN [payload] CRC 的响应帧,
|
||||||
* 自动计算CRC校验值并通过串口输出。
|
* 自动计算CRC校验值并通过串口输出。
|
||||||
* @param type 响应类型码(如 RESP_TYPE_OK, RESP_TYPE_CRC_ERR 等)。
|
* @param type 响应类型码(如 RESP_TYPE_OK, RESP_TYPE_CRC_ERR 等)。
|
||||||
* @param payload 指向响应数据的缓冲区,可为 NULL。
|
* @param payload 指向响应数据的缓冲区,当len为0时可为NULL。
|
||||||
* @param len 响应数据长度(字节),payload为NULL时填充0。
|
* @param len 响应数据长度(字节),为0时不复制payload数据。
|
||||||
* @note 内部使用固定大小缓冲区,超长响应将被丢弃。
|
* @note 内部使用固定大小缓冲区,超长响应将被丢弃。
|
||||||
* @warning 通过 printf 发送,确保串口已正确初始化。
|
* @warning 使用GD32E230标准库函数发送,确保串口已正确初始化。
|
||||||
* @ingroup Command
|
* @ingroup Command
|
||||||
*/
|
*/
|
||||||
static void send_response(uint8_t type, const uint8_t *payload, uint8_t len)
|
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[0] = RESP_HEADER;
|
||||||
buf[1] = type;
|
buf[1] = type;
|
||||||
buf[2] = len;
|
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++) {
|
// 简化逻辑:只有当len > 0且payload非空时才复制数据
|
||||||
printf("%c", buf[i]);
|
if (len > 0 && payload != NULL) {
|
||||||
|
for (uint8_t i = 0; i < len; i++) {
|
||||||
|
buf[3 + i] = payload[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 刷新缓冲区
|
buf[buf_len - 1] = command_sum_crc_calc(buf, buf_len);
|
||||||
fflush(stdout);
|
|
||||||
|
// 使用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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user