RS485 send: disable RX during tx + drain/clear after (anti-loopback); COM_DEBUG on with RX byte trace

This commit is contained in:
2026-10-09 19:58:51 +08:00
parent 5771fee576
commit 318d4c60f4
3 changed files with 18 additions and 1 deletions
+1 -1
View File
@@ -27,7 +27,7 @@
/* >>>>>>>>>>>>>>>>>>>>[COMMAND DEBUG]<<<<<<<<<<<<<<<<<<<< */
#define COM_DEBUG CFG_DISABLE // CFG_DISABLE: Command debug off; CFG_ENABLE: Command debug on
#define COM_DEBUG CFG_ENABLE // CFG_DISABLE: Command debug off; CFG_ENABLE: Command debug on(调试期开启)
/* 命令协议框架已移植(Src/command.c,数据口 RS485),业务命令待定义 */
/* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */
+5
View File
@@ -463,6 +463,11 @@ void command_process(void) {
int byte = rs485_rx_get();
if (byte < 0) break;
#if COM_DEBUG == CFG_ENABLE
/* 调试:打印 RS485 收到的每个原始字节 */
printf("[RX] %02X\r\n", (uint8_t)byte);
#endif
if (cmd_len == 0) {
if ((uint8_t)byte == PROTOCOL_PACKAGE_HEADER) {
cmd_buf[cmd_len++] = (uint8_t)byte;
+12
View File
@@ -63,6 +63,10 @@ void rs485_init(void) {
}
void rs485_send(const uint8_t *data, uint16_t len) {
/* 半双工防冲突:发送期间暂停 RX 中断(若收发器 RE 独立使能,
自己发出的字节会回环进 USART,既污染接收缓冲也可能与本方解析交错) */
usart_interrupt_disable(RS485_PHY, USART_INT_RBNE);
/* 进入发送态 */
gpio_bit_set(RS485_DE_PORT, RS485_DE_PIN);
@@ -76,6 +80,14 @@ void rs485_send(const uint8_t *data, uint16_t len) {
/* 回到接收态 */
gpio_bit_reset(RS485_DE_PORT, RS485_DE_PIN);
/* 丢弃发送期间挤入/回环进接收通道的残留字节(读 DATA 清 RBNE/ORE),
并清空接收环形缓冲,避免垃圾字节污染后续协议帧 */
(void)usart_data_receive(RS485_PHY);
rs485_rx_clear();
/* 恢复接收 */
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
}
uint8_t rs485_rx_available(void) {