STM32 LWIP TCP 数据包分包后合并,超过591字节后分会分包问题
最新在使用LWIP + MQTT的應用過程中,出現一個很奇怪的問題,當上位機反送的JSON長度過大時,我的stm32收到的數據就會有問題:
上圖中成功接收到失敗接收口相關1個字節,用wireshark看數據包長度分別是591和590:
發現可能是我的接收BUFF不夠大引起的,程序中有MqttRxBuff大小定義為1024,改成2048還是一樣接收失敗:
看來問題不是在這個BUFF,
接著看MQtt對應的TCP接收BUFF,在LWIP的opt.h中看到有如下一些定義,從圖中看到PBUF_POOL_BUFSIZE大小取決到TCP_MESS大小。當前TCP_MESS大小于536 對應計算出的PBUF_POOL_BUFSIZE為592,剛好與我們之前的591相近,看到就是這個問題我們把
按理把這個參數改大后應該可以解決,但是發現實際發現還是不行!
仿真發現數據從路由器發送到MCU時,就已經分成了兩包,最終將TCP的接收函數改寫成如下問題解決。
?
/**
? * @brief tcp_receiv callback
? * @param arg: argument to be passed to receive callback
? * @param tpcb: tcp connection control block
? * @param err: receive error code
? * @retval err_t: retuned error
? */
err_t MqttTcp_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
??? struct mqttclinet *es;
??? err_t ret_err;
??? int i = 0;
?
?
??? LWIP_ASSERT("arg != NULL",arg != NULL);
?
??? es = (struct mqttclinet *)arg;
?
??? /* if we receive an empty tcp frame from server => close connection */
??? if (p == NULL)
??? {
??????? /* remote host closed connection */
??????? es->state = ES_CLOSING;
??????? if(es->p_tx == NULL)
??????? {
??????????? /* we're done sending, close connection */
??????????? MqttTcp_connection_close(tpcb, es);
??????? }
??????? else
??????? {
??????????? /* send remaining data*/
??????????? MqttTcp_send(tpcb, es);
??????? }
??????? ret_err = ERR_OK;
??? }
??? /* else : a non empty frame was received from echo server but for some reason err != ERR_OK */
??? else if(err != ERR_OK)
??? {
??????? /* free received pbuf*/
??????? pbuf_free(p);
?
??????? ret_err = err;
??? }
??? else if(es->state == ES_CONNECTED)
??? {
??????? /* Acknowledge data reception */
??????? tcp_recved(tpcb, p->tot_len);
?
#if 0
??????? // 回顯功能
??????? es->p_tx = p;
??????? MqttTcp_send(tpcb, es);
?
#else
??????? // 將要接收的數據放到BUFF里
??????? if( p->flags == 0 )? //? 如果出現了分包的,在這里時行判斷
??????? {
? ??????????MqttRxOffset = p->len;
????????????? ?? memcpy((void *)MqttRxTemp,p->payload,p->len);
??????? }
??????? else
??????? {
??????????? if( MqttRxOffset )
??????????? {
??????????????? if( (MqttRxBwp + MqttRxOffset) -? MqttRxBrp < MQTTBUFFSIZE?? )
??????????????? {
??????????????????? for( i=0; i<MqttRxOffset; i++ )
??????????????????? {
??????????????????????? MqttRxBuff[MqttRxBwp] = MqttRxTemp[i];
????????????????????? ??MqttRxBwp = (MqttRxBwp+1)%MQTTBUFFSIZE;
??????????????????? }
??????????????? }
???????????????????? ? MqttRxOffset = 0;
??????????? }
??????????? if( (MqttRxBwp + p->tot_len) -? MqttRxBrp < MQTTBUFFSIZE?? )
??????????? {
??????????????? for( i=0; i<p->tot_len; i++)
? ??????????????{
??????????????????? MqttRxBuff[MqttRxBwp] = *((uint8_t *)(p->payload)+i);
??????????????????? MqttRxBwp = (MqttRxBwp+1)%MQTTBUFFSIZE;
??????????????? }
??????????? }??
??????? }
??????? pbuf_free(p);
#endif
??????? ret_err = ERR_OK;
??? }
?
??? /* data received when connection already closed */
??? else
??? {
??????? /* Acknowledge data reception */
??????? tcp_recved(tpcb, p->tot_len);
?
??????? /* free pbuf and do nothing */
??????? pbuf_free(p);
??????? ret_err = ERR_OK;
??? }
??? return ret_err;
}
總結
以上是生活随笔為你收集整理的STM32 LWIP TCP 数据包分包后合并,超过591字节后分会分包问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STM32 进阶教程 15 - 串口DM
- 下一篇: STM32 进阶教程 16 - ADC1