青檬 发表于 2012-1-16 14:32:35

STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

目前本人整在使用STM32F107+LWIP+DP83848进行tcp通讯,如何判断网络已经连接成功或者网络是断开的?怎么样能够使STM32F107随时接入以太网都可以通讯

znmyaclk 发表于 2014-12-22 19:25:55

讨论:客户端网线拔出在重新插上连接问题?
通过判断PCB连接状态和心跳来判断是否连接服务器
if((pcb_t->state!=ESTABLISHED)||(ethHeart==0))
{       
       。。。。延时10S
      tcp_abandon(pcb_t,1);//断开
        tcp_close(pcb_t);//关闭
        TCP_TEL_Init();//重新连接
}
没有插网线的话 10S重新连一次!
实现也可以 不知道有没有什么问题?

nosignal 发表于 2017-4-21 10:10:40

1.检查网线是否插入,直接读取PHY状态寄存的link状态值
2.检查TCP是否断开,如果你是客户端,则netconn_recv()会有返回值,根据返回值来判断,如果你是服务端,如果有客户端跟你连接,同样netconn_recv也会有对应的返回值来判断

目前我就是这样判断,至于网线检测,其实在系统加载的时候,初始化的时候就会去读PHY状态寄存器。你可以去看看

linas 发表于 2016-4-15 09:45:21

denton 发表于 2012-1-30 14:44
看了一下...应该在ETH_Init函数里面
没有连上网线的话获取网络状态
ETH_ReadPHYRegister(PHYAddress, PHY_B ...

你好。最近工作碰到了网线正常连接但是网络不通的情况,请问你有什么高见吗?!!

青檬 发表于 2012-1-16 14:45:07

RE:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

void LwIP_Init(void)
{
struct ip_addr ipaddr;        //IP地址
struct ip_addr netmask;        //子掩码
struct ip_addr gw;      //网关地址
uint8_t macaddress={0,0,0,0,0,1};//以太网控制器物理地址,即MAC地址
/* Initializes the dynamic memory heap defined by MEM_SIZE.*/
mem_init();
/* Initializes the memory pools defined by MEMP_NUM_x.*/
memp_init();

#if LWIP_DHCP
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
#else
IP4_ADDR(&ipaddr, 192, 168, 1, 200);
IP4_ADDR(&netmask, 255, 255, 255, 0);
IP4_ADDR(&gw, 192, 168, 1, 1);
#endif
Set_MAC_Address(macaddress);
/* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
            struct ip_addr *netmask, struct ip_addr *gw,
            void *state, err_t (* init)(struct netif *netif),
            err_t (* input)(struct pbuf *p, struct netif *netif))
   
   Adds your network interface to the netif_list. Allocate a struct
netif and pass a pointer to this structure as the first argument.
Give pointers to cleared ip_addr structures when using DHCP,
or fill them with sane numbers otherwise. The state pointer may be NULL.
The init function pointer must point to a initialization function for
your ethernet netif interface. The following code illustrates it's use.*/
netif_add(&netif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);
/*Registers the default network interface.*/
netif_set_default(&netif);

#if LWIP_DHCP
/*Creates a new DHCP client for this interface on the first call.
Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
the predefined regular intervals after starting the client.
You can peek in the netif->dhcp struct for the actual DHCP status.*/
dhcp_start(&netif);
#endif
/*When the netif is fully configured this function must be called.*/
netif_set_up(&netif);
tcp_client_init();
}
/**
* @briefCalled when a frame is received
* @paramNone
* @retval None
*/
void LwIP_Pkt_Handle(void)
{
/* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
ethernetif_input(&netif);
}
/**
* @briefLwIP periodic tasks
* @paramlocaltime the current LocalTime value
* @retval None
*/
void LwIP_Periodic_Handle(__IO uint32_t localtime)
{
/* TCP periodic process every 250 ms */
if (localtime - TCPTimer >= TCP_TMR_INTERVAL)
{
    TCPTimer =localtime;
    tcp_tmr();
}
/* ARP periodic process every 5s */
if (localtime - ARPTimer >= ARP_TMR_INTERVAL)
{
    ARPTimer =localtime;
    etharp_tmr();
}
#if LWIP_DHCP
/* Fine DHCP periodic process every 500ms */
if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS)
{
    DHCPfineTimer =localtime;
    dhcp_fine_tmr();
}
/* DHCP Coarse periodic process every 60s */
if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS)
{
    DHCPcoarseTimer =localtime;
    dhcp_coarse_tmr();
}
#endif
}
/**
* @briefLCD & LEDs periodic handling
* @paramlocaltime: the current LocalTime value
* @retval None
*/
void Display_Periodic_Handle(__IO uint32_t localtime)
{
/* 250 ms */
if (localtime - DisplayTimer >= LCD_TIMER_MSECS)
          {
                  DisplayTimer = localtime;
                        #if LWIP_DHCP
                  /* We have got a new IP address so update the display */
                  if (IPaddress != netif.ip_addr.addr)
                        {               
                              /* Read the new IP address */
                              IPaddress = netif.ip_addr.addr;                               
                              /* Display the new IP address */
                              if (netif.flags & NETIF_FLAG_DHCP)
                                {                                             
                                          tcp_client_init();                                  
                                }
                
                        }
                  else if (IPaddress == 0)
                          {
               
                              /* If no response from a DHCP server for MAX_DHCP_TRIES times */
                                  /* stop the dhcp client and set a static IP address */
                                  if (netif.dhcp->tries > MAX_DHCP_TRIES)
                              {
                                struct ip_addr ipaddr;
                                struct ip_addr netmask;
                                struct ip_addr gw;
                       
                                dhcp_stop(&netif);
                       
                                IP4_ADDR(&ipaddr, 192, 168, 1, 8);
                                IP4_ADDR(&netmask, 255, 255, 255, 201);
                                IP4_ADDR(&gw, 192, 168, 1, 1);
                       
                                netif_set_addr(&netif, &ipaddr , &netmask, &gw);
                              }
                          }
                        #endif
       
          }
}

青檬 发表于 2012-1-16 14:45:35

RE:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

void tcp_client_init(void)
{
struct tcp_pcb *tpcb;
struct ip_addr ipaddr;
IP4_ADDR(&ipaddr, 192, 168, 1, 125);       //远程主机
/* Create a new TCP control block*/
tpcb = tcp_new();
/* Assign to the new pcb a local IP address and a port number */
tcp_bind(tpcb, IP_ADDR_ANY, TCP_PORT);
/* Connect to the server: send the SYN */
tcp_connect(tpcb, &ipaddr, TCP_PORT, tcp_client_accept);
   
}
/**
* @briefThis funtion is called when a TCP connection has been established on the port TCP_PORT.
* @paramarg        user supplied argument
* @parampcb        the tcp_pcb which accepted the connection
* @paramerr error value
* @retval ERR_OK
*/
err_t tcp_client_accept(void *arg, struct tcp_pcb *tpcb, err_t err)
{
/* Specify the function that should be called when the TCP connection receives data */
tcp_recv(tpcb, tcp_client_recv);
return ERR_OK;
}
/**
* @briefThis function is called when a data is received over the TCP_PORT.
*         The received data contains the number of the led to be toggled.
* @paramarg        user supplied argument
* @parampcb        the tcp_pcb which accepted the connection
* @paramp the packet buffer that was received
* @paramerr error value
* @retval ERR_OK
*/
static err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
   uint8_t Data_len;
/* Inform TCP that we have taken the data. */
   tcp_recved(tpcb, p->tot_len);
   Data_len = p->len;
   memcpy(RX, p->payload, Data_len);
   tcp_write(tpcb,&LocalDisplay,sizeof(LocalDisplay),1);       
/* Free the p buffer */
   pbuf_free(p);
   return ERR_OK;
}

青檬 发表于 2012-1-16 14:46:03

RE:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

上面是我写的程序,请高手指教!

青檬 发表于 2012-1-16 14:49:53

RE:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

void LwIP_Init(void)
{
struct ip_addr ipaddr;        //IP地址
struct ip_addr netmask;        //子掩码
struct ip_addr gw;      //网关地址
uint8_t macaddress={0,0,0,0,0,1};//以太网控制器物理地址,即MAC地址
/* Initializes the dynamic memory heap defined by MEM_SIZE.*/
mem_init();
/* Initializes the memory pools defined by MEMP_NUM_x.*/
memp_init();

#if LWIP_DHCP
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
#else
IP4_ADDR(&ipaddr, 192, 168, 1, 200);
IP4_ADDR(&netmask, 255, 255, 255, 0);
IP4_ADDR(&gw, 192, 168, 1, 1);
#endif
Set_MAC_Address(macaddress);
netif_set_default(&netif);
#if LWIP_DHCP
dhcp_start(&netif);
#endif
netif_set_up(&netif);
tcp_client_init();
}
void LwIP_Pkt_Handle(void)
{
ethernetif_input(&netif);
}
void LwIP_Periodic_Handle(__IO uint32_t localtime)
{
/* TCP periodic process every 250 ms */
if (localtime - TCPTimer >= TCP_TMR_INTERVAL)
{
    TCPTimer =localtime;
    tcp_tmr();
}
/* ARP periodic process every 5s */
if (localtime - ARPTimer >= ARP_TMR_INTERVAL)
{
    ARPTimer =localtime;
    etharp_tmr();
}
#if LWIP_DHCP
if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS)
{
    DHCPfineTimer =localtime;
    dhcp_fine_tmr();
}
if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS)
{
    DHCPcoarseTimer =localtime;
    dhcp_coarse_tmr();
}
#endif
}
void Display_Periodic_Handle(__IO uint32_t localtime)
{
if (localtime - DisplayTimer >= LCD_TIMER_MSECS)
          {
                  DisplayTimer = localtime;
                        #if LWIP_DHCP
                  if (IPaddress != netif.ip_addr.addr)
                        {               
                              IPaddress = netif.ip_addr.addr;                               
                              if (netif.flags & NETIF_FLAG_DHCP)
                                {                                             
                                          tcp_client_init();                                  
                                }
                
                        }
                  else if (IPaddress == 0)
                          {
               
                       if (netif.dhcp->tries > MAX_DHCP_TRIES)
                              {
                                struct ip_addr ipaddr;
                                struct ip_addr netmask;
                                struct ip_addr gw;
                       
                                dhcp_stop(&netif);
                       
                                IP4_ADDR(&ipaddr, 192, 168, 1, 8);
                                IP4_ADDR(&netmask, 255, 255, 255, 201);
                                IP4_ADDR(&gw, 192, 168, 1, 1);
                       
                                netif_set_addr(&netif, &ipaddr , &netmask, &gw);
                              }
                          }
                        #endif
       
          }
}

青檬 发表于 2012-1-16 14:51:07

RE:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

void tcp_client_init(void)
{
struct tcp_pcb *tpcb;
struct ip_addr ipaddr;
IP4_ADDR(&ipaddr, 192, 168, 1, 125);       //远程主机
tpcb = tcp_new();
tcp_bind(tpcb, IP_ADDR_ANY, TCP_PORT);
tcp_connect(tpcb, &ipaddr, TCP_PORT, tcp_client_accept);
   
}
err_t tcp_client_accept(void *arg, struct tcp_pcb *tpcb, err_t err)
{
tcp_recv(tpcb, tcp_client_recv);
return ERR_OK;
}

static err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
   uint8_t Data_len;
   tcp_recved(tpcb, p->tot_len);
   Data_len = p->len;
   memcpy(RX, p->payload, Data_len);
   tcp_write(tpcb,&LocalDisplay,sizeof(LocalDisplay),1);       
   pbuf_free(p);
   return ERR_OK;
}

青檬 发表于 2012-1-16 14:51:43

RE:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

怕大家看着眼花,我又整理了下程序。

废鱼 发表于 2012-1-16 15:05:06

RE:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

我也没有太多的用过TCP,不过你可以针对LWIP相关寄存器、TCP协议入手。应该有介绍处理TCP通讯的逻辑。

denton 发表于 2012-1-18 10:42:12

回复:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

我也想知道,有没有人实现过?官方提供的启动板子时未连接网线超时后就没法连接了:'(

denton 发表于 2012-1-30 14:44:13

回复:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

看了一下...应该在ETH_Init函数里面
没有连上网线的话获取网络状态
ETH_ReadPHYRegister(PHYAddress, PHY_BSR) & PHY_Linked_Status
返回值为0

dely 发表于 2012-2-29 16:35:35

RE:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

请问谁知道拔了网线怎么知道通信断开, 我没找到ETH_ReadPHYRegister这个函数,请问在那里。。谢谢

pentral0311 发表于 2012-3-12 15:52:35

回复:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

LZ,你有搞出来吗,我也碰到这个问题
 

Veiko 发表于 2012-8-26 08:44:49

RE:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

:'(同是天涯沦落人!

zhaokenaz 发表于 2013-4-3 15:07:09

回复:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

 我的解决过程:
(提示:看懂附件TCP状态图)
1.检查TCP是否断开:if(pcb->state==CLOSED){}
2.(可以不用试试)关闭之前的pcb:tcp_abort(client_pcb);
3.重新连接TCP:tcp_client_connect();

zhy-401674 发表于 2013-6-28 09:52:55

回复:STM32F107+LWIP---如何检查tcp通讯断开?并重新连接

 我是来看附件的~:)
页: [1] 2 3 4
查看完整版本: STM32F107+LWIP---如何检查tcp通讯断开?并重新连接