V1.5: 修复485首次应答丢失(查询前强制复位USART2接收,防ORE残留卡死)

This commit is contained in:
helei 2026-08-22 10:48:04 +08:00
parent a8f0ccc6c7
commit bf828d6a22
4 changed files with 24 additions and 1 deletions

View File

@ -124,6 +124,7 @@ static int feed_scale_parse_weight(const uint8_t *buf, uint16_t len, uint16_t *w
/* 通过 USART2 发送 485 查询命令 */ /* 通过 USART2 发送 485 查询命令 */
static void feed_scale_send_query(void) static void feed_scale_send_query(void)
{ {
USART2_ForceRxReset(); /* 清缓冲+重挂接收,防止前次残留状态吞掉本次应答 */
HAL_UART_Transmit(&huart2, (uint8_t *)RS485_WEIGHT_CMD, sizeof(RS485_WEIGHT_CMD), 100); HAL_UART_Transmit(&huart2, (uint8_t *)RS485_WEIGHT_CMD, sizeof(RS485_WEIGHT_CMD), 100);
} }

View File

@ -262,6 +262,25 @@ void USART2_StartRx(void)
HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1); HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1);
} }
/* 强制复位 USART2 接收:清错误标志/缓冲并重新挂中断接收。
* 485 ORE HAL
* RxState READY */
void USART2_ForceRxReset(void)
{
__HAL_UART_CLEAR_OREFLAG(&huart2);
__HAL_UART_CLEAR_PEFLAG(&huart2);
__HAL_UART_CLEAR_FEFLAG(&huart2);
__HAL_UART_CLEAR_NEFLAG(&huart2);
__HAL_UART_CLEAR_IDLEFLAG(&huart2);
huart2.ErrorCode = HAL_UART_ERROR_NONE;
if (huart2.RxState != HAL_UART_STATE_READY) {
HAL_UART_AbortReceive(&huart2);
}
uart2_rx_ready = 0;
uart2_cmd_index = 0;
HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1);
}
/* 从 USART2 接收缓冲区取出一帧20ms 无新字节结束返回帧长度0 表示暂无完整帧 */ /* 从 USART2 接收缓冲区取出一帧20ms 无新字节结束返回帧长度0 表示暂无完整帧 */
uint16_t USART2_GetFrame(uint8_t *out, uint16_t out_size) uint16_t USART2_GetFrame(uint8_t *out, uint16_t out_size)
{ {

View File

@ -35,7 +35,8 @@ void USART1_StartRx(void); /*
void USART2_StartRx(void); /* 启动 USART2 中断接收 */ void USART2_StartRx(void); /* 启动 USART2 中断接收 */
void UART5_StartRx(void); /* 启动 UART5 中断接收HLW8032 */ void UART5_StartRx(void); /* 启动 UART5 中断接收HLW8032 */
void USART2_Tick(void); /* 触发 USART2 帧超时自动提交 */ void USART2_Tick(void); /* 触发 USART2 帧超时自动提交 */
uint16_t USART2_GetFrame(uint8_t *out, uint16_t out_size); /* 从 USART2 取一帧原始字节 */ uint16_t USART2_GetFrame(uint8_t *out, uint16_t out_size);
void USART2_ForceRxReset(void); /* 强制复位 USART2 接收485 查询前调用) */ /* 从 USART2 取一帧原始字节 */
int USART1_CheckCommand(const char *cmd); /* 检查是否收到指定命令(不区分大小写) */ int USART1_CheckCommand(const char *cmd); /* 检查是否收到指定命令(不区分大小写) */
const char *USART1_GetCommand(void); /* 获取当前接收到的命令字符串 */ const char *USART1_GetCommand(void); /* 获取当前接收到的命令字符串 */

View File

@ -180,3 +180,5 @@
(无应答/帧短/CRC错/秤异常帧/站号不符);无秤模式仍显示开关状态 (无应答/帧短/CRC错/秤异常帧/站号不符);无秤模式仍显示开关状态
- 断电预警时 OLED 关显示0xAE 黑屏,省电+避免残影) - 断电预警时 OLED 关显示0xAE 黑屏,省电+避免残影)
- 去掉 OLED 防烧屏左右偏移(断电已关屏,偏移意义不大) - 去掉 OLED 防烧屏左右偏移(断电已关屏,偏移意义不大)
- 修复 485 首次应答丢失USART2 接收可能因 ORE 残留状态卡死(自愈重挂因 RxState 非 READY 静默失败),
现每次查询前 USART2_ForceRxReset() 强制复位接收(清错误/缓冲+重挂中断),首包不再丢