V1.5: 修复485回声吞应答帧头(TX后清回声+解析定位01 03 04帧头)

This commit is contained in:
helei 2026-08-22 11:09:48 +08:00
parent bf828d6a22
commit a4d8420bff
2 changed files with 21 additions and 0 deletions

View File

@ -80,6 +80,23 @@ static int feed_scale_parse_weight(const uint8_t *buf, uint16_t len, uint16_t *w
{
feed_scale_dump_frame(buf, len);
/* 定位应答帧头(站号+03+字节数04485 回声或前导垃圾可能混在帧头前面 */
int hdr = -1;
for (int i = 0; i + 8 < len; i++) {
if (buf[i] == RS485_WEIGHT_CMD[0] && buf[i+1] == 0x03 && buf[i+2] == 0x04) {
hdr = i;
break;
}
}
if (hdr > 0) {
log_info("> FEED: 跳过前导 %d 字节(回声/干扰)", hdr);
buf += hdr;
len -= hdr;
} else if (hdr < 0 && len >= 9) {
log_warn("> FEED: 解析失败-找不到帧头 01 03 04");
return -6;
}
if (len < 9) {
log_warn("> FEED: 解析失败-帧长度过短(%d<9),可能波特率不对或线路干扰", (int)len);
return -1;
@ -126,6 +143,8 @@ static void feed_scale_send_query(void)
{
USART2_ForceRxReset(); /* 清缓冲+重挂接收,防止前次残留状态吞掉本次应答 */
HAL_UART_Transmit(&huart2, (uint8_t *)RS485_WEIGHT_CMD, sizeof(RS485_WEIGHT_CMD), 100);
HAL_Delay(5); /* 等 485 回声(自己 TX 被 RX 听到)收尾 */
USART2_ForceRxReset(); /* 清掉回声,应答帧从干净状态开始 */
}
/* 读取 485 返回帧 */

View File

@ -182,3 +182,5 @@
- 去掉 OLED 防烧屏左右偏移(断电已关屏,偏移意义不大)
- 修复 485 首次应答丢失USART2 接收可能因 ORE 残留状态卡死(自愈重挂因 RxState 非 READY 静默失败),
现每次查询前 USART2_ForceRxReset() 强制复位接收(清错误/缓冲+重挂中断),首包不再丢
- 修复 485 回声吃应答帧头:查询 TX 后等 5ms 清回声缓冲;解析时改为在帧内定位 01 03 04 帧头,
抗回声/前导垃圾(快速应答不再被吞前几个字节)