From 9e830639d3b75aa6c5ca0c7d28aaf3e068eb87a9 Mon Sep 17 00:00:00 2001 From: wangfq Date: Thu, 23 Jul 2026 13:57:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(vd960DBN):=20iot=5Fmqtt=5Fsend=20=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF=E9=87=8D=E8=AF=95=E9=98=B2=20WCHNET=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=8F=91=E9=80=81=E8=87=B4=20=5Fraw?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit root cause: WCHNET_SocketSend 返回部分发送(520/607B), 剩余字节留在TCP发送队列, 与下一个MQTT包拼接, broker收到乱码 → _raw + RST断连 fix: iot_mqtt_send 加 while 循环, 指针偏移重试直到全部发出; 最多10次重试, slen==0或错误时返回-1 --- .../APP/iot_mqtt_srv.c | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c index a083297..c7a77cc 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/iot_mqtt_srv.c @@ -290,13 +290,24 @@ static int iot_make_topic(char *out, uint16_t out_len, const char *direction, * MQTT Packet Helpers *===========================================================================*/ -/* 发送 MQTT 报文到 broker */ +/* 发送 MQTT 报文到 broker — 循环重试防 WCHNET 部分发送 + (2026-07-23: SocketSend 只发 520/607B → 残留拼到下行 → broker 见垃圾 → _raw+RST) */ static int iot_mqtt_send(const uint8_t *buf, uint16_t len) { - uint32_t slen = len; /* 必须是 uint32_t: WCHNET_SocketSend 写 4 字节, uint16_t 会栈破坏致 hard fault */ - PRINT("IOT: SocketSend sock=%d len=%lu\n", g_iot_socket, (unsigned long)slen); - uint8_t ret = WCHNET_SocketSend(g_iot_socket, (uint8_t *)buf, &slen); - PRINT("IOT: SocketSend ret=0x%02X sent=%lu\n", ret, (unsigned long)slen); - return (ret == WCHNET_ERR_SUCCESS) ? 0 : -1; + uint16_t total_sent = 0; + int retries = 0; + PRINT("IOT: SocketSend sock=%d len=%u\n", g_iot_socket, len); + while (total_sent < len) { + uint32_t slen = len - total_sent; + uint8_t ret = WCHNET_SocketSend(g_iot_socket, (uint8_t *)(buf + total_sent), &slen); + total_sent += (uint16_t)slen; + if (slen == 0 || ret != WCHNET_ERR_SUCCESS || ++retries > 10) { + PRINT("IOT: SocketSend PARTIAL ret=0x%02X sent=%u/%u retries=%d\n", + ret, total_sent, len, retries); + return -1; + } + } + PRINT("IOT: SocketSend OK sent=%u\n", total_sent); + return 0; } /* 发送 MQTT CONNECT — 参考 net_srv.c mqtt_connect 实现 */