硬汉嵌入式论坛

 找回密码
 立即注册
查看: 955|回复: 16
收起左侧

[以太网] 如何再STM32上做一个比较稳定的tcp服务程序

[复制链接]

4

主题

30

回帖

42

积分

新手上路

积分
42
发表于 2025-4-24 11:07:53 | 显示全部楼层 |阅读模式
目前使用的STM32H723芯片,PHY芯片LAN8720A,移植LWIP程序,单独测试固定IP,使用ATKPING程序ping网络稳定性基本上30万包数据未出现超时丢包问题,但在使用简单的TCPserver,通过网络助手发送数据,单片机收到数据返回,低数据量(十几个字节)能在50Hz频率下正常工作,但是当数据量增加到几百个字节时,会出现网络卡死,丢数据的情况。出现这种情况应该怎么样处理,如何在网络本身ping稳定性好的情况下做一个比较健壮的STM32芯片下的tcpserver程序。本人网络通信方面的小白,有描述不对的地方请指正。
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
116230
QQ
发表于 2025-4-24 11:45:25 | 显示全部楼层
当前楼主用的那种方式,raw原始api,netconn还是bsd socket
回复

使用道具 举报

4

主题

30

回帖

42

积分

新手上路

积分
42
 楼主| 发表于 2025-4-24 13:56:43 | 显示全部楼层
eric2013 发表于 2025-4-24 11:45
当前楼主用的那种方式,raw原始api,netconn还是bsd socket

参照的时V7-2001_LwIP V2.X_实验_工程移植模板(FreeRTOS)V1.1,具体参照的代码如下:
[C] 纯文本查看 复制代码
void LwIPTest(void)
{
	struct netconn *conn, *newconn;
	err_t err;

	/* 创建服务器 */
	conn = netconn_new(NETCONN_TCP);
	netconn_bind(conn, IP_ADDR_ANY, PORT_NUM);

	/* 设置监听 */
	netconn_listen(conn);
	
	while (1) 
	{
		/* 等待接收新的连接 */
		err = netconn_accept(conn, &newconn);

		/* 处理新连接 */
		if (err == ERR_OK) 
		{
			struct netbuf *buf;
			void *data;
			u16_t len;

			/* 简单的数据回环 */
			while ((err = netconn_recv(newconn, &buf)) == ERR_OK) 
			{
		        do {
		             netbuf_data(buf, &data, &len);
		             err = netconn_write(newconn, data, len, NETCONN_COPY);
		        } while (netbuf_next(buf) >= 0);
				netbuf_delete(buf);
			}
			
			/* 删除连接  */
			netconn_close(newconn);
			netconn_delete(newconn);
		}

	}
}
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
116230
QQ
发表于 2025-4-25 10:36:00 | 显示全部楼层
Ainit 发表于 2025-4-24 13:56
参照的时V7-2001_LwIP V2.X_实验_工程移植模板(FreeRTOS)V1.1,具体参照的代码如下:
[mw_shl_code=c,tru ...

考虑判断下netconn_write的返回值,看看是不是好点
回复

使用道具 举报

4

主题

30

回帖

42

积分

新手上路

积分
42
 楼主| 发表于 2025-4-25 15:10:42 | 显示全部楼层
eric2013 发表于 2025-4-25 10:36
考虑判断下netconn_write的返回值,看看是不是好点

试了一下,也会有相同的问题,可能是我描述问题错误了。我的理解是,比如使用串口通信,波特率限制了这个串口的收发字节数,比如1S内,115200波特率下,串口收发数据是有一个极限的,比如为11520字节/s,那我将发送数据周期修改成20ms时,那么一次发送数据最大值不能超过230字节。类比到以太网,应该有个理论速度,且我可以根据理论速度设置20ms周期内,一次最多可以发送多少字节数据。我不确定这样理解对不对。如我理解错误,请帮忙指正,谢谢
回复

使用道具 举报

4

主题

30

回帖

42

积分

新手上路

积分
42
 楼主| 发表于 2025-4-25 16:13:43 | 显示全部楼层
使用TCP server 简单数据回环测试,一帧数据120字节,会出现收发一会儿自动卡死的情况,并且tcp连接自动断开。
50Hz数据收发测试-120字节卡死.png
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
116230
QQ
发表于 2025-4-26 10:15:34 | 显示全部楼层
Ainit 发表于 2025-4-25 15:10
试了一下,也会有相同的问题,可能是我描述问题错误了。我的理解是,比如使用串口通信,波特率限制了这个 ...

TCP没有这个问题,TCP是服务器和客户端自动协商的。
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
116230
QQ
发表于 2025-4-26 10:17:34 | 显示全部楼层
Ainit 发表于 2025-4-25 16:13
使用TCP server 简单数据回环测试,一帧数据120字节,会出现收发一会儿自动卡死的情况,并且tcp连接自动断 ...

这个软件接收处理速度一般。

不过你楼主位的50Hz速度无压力
回复

使用道具 举报

7

主题

146

回帖

167

积分

初级会员

积分
167
发表于 2025-4-26 10:29:56 | 显示全部楼层
ping 和TCP 没有关联,PING 就是个UDP包,而且开销很少内存,用的内存好像是LWIP 自己管理的,而TCP 发送接收的数据 内存要你自己管理 ,你这个肯定是哪里搞错了,
很早之前我STM32F107 72M的跑也很稳定,你这H7不知道快多少倍了
回复

使用道具 举报

4

主题

30

回帖

42

积分

新手上路

积分
42
 楼主| 发表于 2025-4-26 10:30:56 | 显示全部楼层
eric2013 发表于 2025-4-26 10:17
这个软件接收处理速度一般。

不过你楼主位的50Hz速度无压力

卡死的情况不是软件的问题,实际上软件一直在发送数据,但是单片机那边出现网络卡死,没有数据返回。只单纯在单片机内做接收数据,并将收到的数据返回上层,50hz-大于100字节,好像有点压力,通过ping稳定性测试是百万包数据没有出现丢包,延时卡顿的。
回复

使用道具 举报

4

主题

30

回帖

42

积分

新手上路

积分
42
 楼主| 发表于 2025-4-26 10:35:03 | 显示全部楼层
mygod 发表于 2025-4-26 10:29
ping 和TCP 没有关联,PING 就是个UDP包,而且开销很少内存,用的内存好像是LWIP 自己管理的,而TCP 发送接 ...

那可能是我这个做TCP回环测试程序太简单了,支撑不住我使用大数据量的操作。请问可以参考参考你写的代码么?
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
116230
QQ
发表于 2025-4-27 11:21:31 | 显示全部楼层
Ainit 发表于 2025-4-26 10:30
卡死的情况不是软件的问题,实际上软件一直在发送数据,但是单片机那边出现网络卡死,没有数据返回。只单 ...

ST做的回环,试试

[C] 纯文本查看 复制代码
/**
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
 * All rights reserved. 
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE.
 *
 * This file is part of and a contribution to the lwIP TCP/IP stack.
 *
 * Credits go to Adam Dunkels (and the current maintainers) of this software.
 *
 * Christiaan Simons rewrote this file to get a more stable echo application.
 *
 **/

 /* This file was modified by ST */

#include "tcp_echoserver.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"

#if LWIP_TCP

static struct tcp_pcb *tcp_echoserver_pcb;

/* ECHO protocol states */
enum tcp_echoserver_states
{
  ES_NONE = 0,
  ES_ACCEPTED,
  ES_RECEIVED,
  ES_CLOSING
};

/* structure for maintaining connection infos to be passed as argument 
   to LwIP callbacks*/
struct tcp_echoserver_struct
{
  u8_t state;             /* current connection state */
  u8_t retries;
  struct tcp_pcb *pcb;    /* pointer on the current tcp_pcb */
  struct pbuf *p;         /* pointer on the received/to be transmitted pbuf */
};


static err_t tcp_echoserver_accept(void *arg, struct tcp_pcb *newpcb, err_t err);
static err_t tcp_echoserver_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
static void tcp_echoserver_error(void *arg, err_t err);
static err_t tcp_echoserver_poll(void *arg, struct tcp_pcb *tpcb);
static err_t tcp_echoserver_sent(void *arg, struct tcp_pcb *tpcb, u16_t len);
static void tcp_echoserver_send(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es);
static void tcp_echoserver_connection_close(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es);


/**
  * @brief  Initializes the tcp echo server
  * @param  None
  * @retval None
  */
void tcp_echoserver_init(void)
{
  /* create new tcp pcb */
  tcp_echoserver_pcb = tcp_new();

  if (tcp_echoserver_pcb != NULL)
  {
    err_t err;
    
    /* bind echo_pcb to port 7 (ECHO protocol) */
    err = tcp_bind(tcp_echoserver_pcb, IP_ADDR_ANY, 7);
    
    if (err == ERR_OK)
    {
      /* start tcp listening for echo_pcb */
      tcp_echoserver_pcb = tcp_listen(tcp_echoserver_pcb);
      
      /* initialize LwIP tcp_accept callback function */
      tcp_accept(tcp_echoserver_pcb, tcp_echoserver_accept);
    }
    else 
    {
      /* deallocate the pcb */
      memp_free(MEMP_TCP_PCB, tcp_echoserver_pcb);
    }
  }
}

/**
  * @brief  This function is the implementation of tcp_accept LwIP callback
  * @param  arg: not used
  * @param  newpcb: pointer on tcp_pcb struct for the newly created tcp connection
  * @param  err: not used 
  * @retval err_t: error status
  */
static err_t tcp_echoserver_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
{
  err_t ret_err;
  struct tcp_echoserver_struct *es;

  LWIP_UNUSED_ARG(arg);
  LWIP_UNUSED_ARG(err);

  /* set priority for the newly accepted tcp connection newpcb */
  tcp_setprio(newpcb, TCP_PRIO_MIN);

  /* allocate structure es to maintain tcp connection information */
  es = (struct tcp_echoserver_struct *)mem_malloc(sizeof(struct tcp_echoserver_struct));
  if (es != NULL)
  {
    es->state = ES_ACCEPTED;
    es->pcb = newpcb;
    es->retries = 0;
    es->p = NULL;
    
    /* pass newly allocated es structure as argument to newpcb */
    tcp_arg(newpcb, es);
    
    /* initialize lwip tcp_recv callback function for newpcb  */ 
    tcp_recv(newpcb, tcp_echoserver_recv);
    
    /* initialize lwip tcp_err callback function for newpcb  */
    tcp_err(newpcb, tcp_echoserver_error);
    
    /* initialize lwip tcp_poll callback function for newpcb */
    tcp_poll(newpcb, tcp_echoserver_poll, 0);
    
    ret_err = ERR_OK;
  }
  else
  {
    /*  close tcp connection */
    tcp_echoserver_connection_close(newpcb, es);
    /* return memory error */
    ret_err = ERR_MEM;
  }
  return ret_err;  
}


/**
  * @brief  This function is the implementation for tcp_recv LwIP callback
  * @param  arg: pointer on a argument for the tcp_pcb connection
  * @param  tpcb: pointer on the tcp_pcb connection
  * @param  pbuf: pointer on the received pbuf
  * @param  err: error information regarding the reveived pbuf
  * @retval err_t: error code
  */
static err_t tcp_echoserver_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
  struct tcp_echoserver_struct *es;
  err_t ret_err;

  LWIP_ASSERT("arg != NULL",arg != NULL);
  
  es = (struct tcp_echoserver_struct *)arg;
  
  /* if we receive an empty tcp frame from client => close connection */
  if (p == NULL)
  {
    /* remote host closed connection */
    es->state = ES_CLOSING;
    if(es->p == NULL)
    {
       /* we're done sending, close connection */
       tcp_echoserver_connection_close(tpcb, es);
    }
    else
    {
      /* we're not done yet */
      /* acknowledge received packet */
      tcp_sent(tpcb, tcp_echoserver_sent);
      
      /* send remaining data*/
      tcp_echoserver_send(tpcb, es);
    }
    ret_err = ERR_OK;
  }   
  /* else : a non empty frame was received from client but for some reason err != ERR_OK */
  else if(err != ERR_OK)
  {
    /* free received pbuf*/
    if (p != NULL)
    {
      es->p = NULL;
      pbuf_free(p);
    }
    ret_err = err;
  }
  else if(es->state == ES_ACCEPTED)
  {
    /* first data chunk in p->payload */
    es->state = ES_RECEIVED;
    
    /* store reference to incoming pbuf (chain) */
    es->p = p;
    
    /* initialize LwIP tcp_sent callback function */
    tcp_sent(tpcb, tcp_echoserver_sent);
    
    /* send back the received data (echo) */
    tcp_echoserver_send(tpcb, es);
    
    ret_err = ERR_OK;
  }
  else if (es->state == ES_RECEIVED)
  {
    /* more data received from client and previous data has been already sent*/
    if(es->p == NULL)
    {
      es->p = p;
  
      /* send back received data */
      tcp_echoserver_send(tpcb, es);
    }
    else
    {
      struct pbuf *ptr;

      /* chain pbufs to the end of what we recv'ed previously  */
      ptr = es->p;
      pbuf_chain(ptr,p);
    }
    ret_err = ERR_OK;
  }
  else if(es->state == ES_CLOSING)
  {
    /* odd case, remote side closing twice, trash data */
    tcp_recved(tpcb, p->tot_len);
    es->p = NULL;
    pbuf_free(p);
    ret_err = ERR_OK;
  }
  else
  {
    /* unknown es->state, trash data  */
    tcp_recved(tpcb, p->tot_len);
    es->p = NULL;
    pbuf_free(p);
    ret_err = ERR_OK;
  }
  return ret_err;
}

/**
  * @brief  This function implements the tcp_err callback function (called
  *         when a fatal tcp_connection error occurs. 
  * @param  arg: pointer on argument parameter 
  * @param  err: not used
  * @retval None
  */
static void tcp_echoserver_error(void *arg, err_t err)
{
  struct tcp_echoserver_struct *es;

  LWIP_UNUSED_ARG(err);

  es = (struct tcp_echoserver_struct *)arg;
  if (es != NULL)
  {
    /*  free es structure */
    mem_free(es);
  }
}

/**
  * @brief  This function implements the tcp_poll LwIP callback function
  * @param  arg: pointer on argument passed to callback
  * @param  tpcb: pointer on the tcp_pcb for the current tcp connection
  * @retval err_t: error code
  */
static err_t tcp_echoserver_poll(void *arg, struct tcp_pcb *tpcb)
{
  err_t ret_err;
  struct tcp_echoserver_struct *es;

  es = (struct tcp_echoserver_struct *)arg;
  if (es != NULL)
  {
    if (es->p != NULL)
    {
      tcp_sent(tpcb, tcp_echoserver_sent);
      /* there is a remaining pbuf (chain) , try to send data */
      tcp_echoserver_send(tpcb, es);
    }
    else
    {
      /* no remaining pbuf (chain)  */
      if(es->state == ES_CLOSING)
      {
        /*  close tcp connection */
        tcp_echoserver_connection_close(tpcb, es);
      }
    }
    ret_err = ERR_OK;
  }
  else
  {
    /* nothing to be done */
    tcp_abort(tpcb);
    ret_err = ERR_ABRT;
  }
  return ret_err;
}

/**
  * @brief  This function implements the tcp_sent LwIP callback (called when ACK
  *         is received from remote host for sent data) 
  * @param  None
  * @retval None
  */
static err_t tcp_echoserver_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
{
  struct tcp_echoserver_struct *es;

  LWIP_UNUSED_ARG(len);

  es = (struct tcp_echoserver_struct *)arg;
  es->retries = 0;
  
  if(es->p != NULL)
  {
    /* still got pbufs to send */
    tcp_sent(tpcb, tcp_echoserver_sent);
    tcp_echoserver_send(tpcb, es);
  }
  else
  {
    /* if no more data to send and client closed connection*/
    if(es->state == ES_CLOSING)
      tcp_echoserver_connection_close(tpcb, es);
  }
  return ERR_OK;
}


/**
  * @brief  This function is used to send data for tcp connection
  * @param  tpcb: pointer on the tcp_pcb connection
  * @param  es: pointer on echo_state structure
  * @retval None
  */
static void tcp_echoserver_send(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es)
{
  struct pbuf *ptr;
  err_t wr_err = ERR_OK;
 
  while ((wr_err == ERR_OK) &&
         (es->p != NULL) && 
         (es->p->len <= tcp_sndbuf(tpcb)))
  {
    
    /* get pointer on pbuf from es structure */
    ptr = es->p;

    /* enqueue data for transmission */
    wr_err = tcp_write(tpcb, ptr->payload, ptr->len, 1);
    
    if (wr_err == ERR_OK)
    {
      u16_t plen;
      u8_t freed;

      plen = ptr->len;
     
      /* continue with next pbuf in chain (if any) */
      es->p = ptr->next;
      
      if(es->p != NULL)
      {
        /* increment reference count for es->p */
        pbuf_ref(es->p);
      }
      
     /* chop first pbuf from chain */
      do
      {
        /* try hard to free pbuf */
        freed = pbuf_free(ptr);
      }
      while(freed == 0);
     /* we can read more data now */
     tcp_recved(tpcb, plen);
   }
   else if(wr_err == ERR_MEM)
   {
      /* we are low on memory, try later / harder, defer to poll */
     es->p = ptr;
   }
   else
   {
     /* other problem ?? */
   }
  }
}

/**
  * @brief  This functions closes the tcp connection
  * @param  tcp_pcb: pointer on the tcp connection
  * @param  es: pointer on echo_state structure
  * @retval None
  */
static void tcp_echoserver_connection_close(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es)
{
  
  /* remove all callbacks */
  tcp_arg(tpcb, NULL);
  tcp_sent(tpcb, NULL);
  tcp_recv(tpcb, NULL);
  tcp_err(tpcb, NULL);
  tcp_poll(tpcb, NULL, 0);
  
  /* delete es structure */
  if (es != NULL)
  {
    mem_free(es);
  }  
  
  /* close tcp connection */
  tcp_close(tpcb);
}

#endif /* LWIP_TCP */
回复

使用道具 举报

4

主题

30

回帖

42

积分

新手上路

积分
42
 楼主| 发表于 2025-4-27 18:38:59 | 显示全部楼层
eric2013 发表于 2025-4-27 11:21
ST做的回环,试试

[mw_shl_code=c,true]/**

好的,感谢硬汉,目前在测试中,连续一天了,没出现什么问题。数据传输相对比较稳定。测试了一下网速,大概在30Mbps左右,想请教一下这种以太网的速度是怎么调节的。我看论坛中帖子,都能够跑满100M,比我的块三倍了。
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
116230
QQ
发表于 2025-4-28 09:36:00 | 显示全部楼层
Ainit 发表于 2025-4-27 18:38
好的,感谢硬汉,目前在测试中,连续一天了,没出现什么问题。数据传输相对比较稳定。测试了一下网速,大 ...

单反向,单独测试。
回复

使用道具 举报

4

主题

30

回帖

42

积分

新手上路

积分
42
 楼主| 发表于 2025-4-28 12:16:03 | 显示全部楼层
eric2013 发表于 2025-4-28 09:36
单反向,单独测试。

我测试是只有PC端发送数据,MCU端数据不返回。
网络速度.png

另外使用官方的数据回环测试。使用100字节/包-50Hz频率,也会有出现问题。昨天测试到180万包数据后,单片机挂了。我使用一个LED一直翻转电平,出问题后LED灯功能失效。另外做了一个判断,如果发送函数返回值错误。我会打印错误信息,但是看日志是没进入mcu发送错误。这种情况下,是Freertos内核出现问题吗?这种情况只能复位MCU才能恢复。


回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
116230
QQ
发表于 2025-4-29 11:00:33 | 显示全部楼层
Ainit 发表于 2025-4-28 12:16
我测试是只有PC端发送数据,MCU端数据不返回。

速度确实不太行。

我早期做的这个应用部分,稳定性可以,是从一个项目上改过来的。

基于F407的LwIP裸机TCP通信案例
https://www.armbbs.cn/forum.php? ... 8568&fromuid=58
(出处: 硬汉嵌入式论坛)

回复

使用道具 举报

4

主题

30

回帖

42

积分

新手上路

积分
42
 楼主| 发表于 2025-5-1 20:50:15 | 显示全部楼层
eric2013 发表于 2025-4-29 11:00
速度确实不太行。

我早期做的这个应用部分,稳定性可以,是从一个项目上改过来的。

好的,感谢感谢,这几天有空学习一下专业的人怎么做的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-6-11 07:21 , Processed in 0.350203 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表