完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我现在在使用STM32F107+LWIP+DP83848进行tcp通讯,如何判断网络已经连接成功或断开的?怎么样能够使STM32F107随时接入以太网都可以通讯,我的程序如下,求指点
void LwIP_Init(void) { struct ip_addr ipaddr; //IP地址 struct ip_addr netmask; //子掩码 struct ip_addr gw; //网关地址 uint8_t macaddress[6]={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, ðernetif_init, ðernet_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(); } /** * @brief Called when a frame is received * @param None * @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); } /** * @brief LwIP periodic tasks * @param localtime 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 } /** * @brief LCD & LEDs periodic handling * @param localtime: 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 } } 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); } /** * @brief This funtion is called when a TCP connection has been established on the port TCP_PORT. * @param arg user supplied argument * @param pcb the tcp_pcb which accepted the connection * @param err 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; } /** * @brief This function is called when a data is received over the TCP_PORT. * The received data contains the number of the led to be toggled. * @param arg user supplied argument * @param pcb the tcp_pcb which accepted the connection * @param p the packet buffer that was received * @param err 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; } |
|
相关推荐
4个回答
|
|
楼主可以试试这样的解决过程:
1.检查TCP是否断开:if(pcb->state==CLOSED){} 2.关闭之前的pcb:tcp_abort(client_pcb); 3.重新连接TCP:tcp_client_connect(); |
|
|
|
同意楼上,有一些标志位,可以判断是否断网,连接上网络等
|
|
|
|
同样问题,楼主解决没呢,能分享一下吗
|
|
|
|
同样问题,楼主解决没呢,能分享一下吗
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
飞凌嵌入式ElfBoard ELF 1板卡-CAN编程示例之开发板测试
769 浏览 0 评论
该问题是用APP给芯海科技的CST92F25芯片发指令是出现的
2583 浏览 1 评论
849 浏览 0 评论
1641 浏览 1 评论
2551 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 22:21 , Processed in 1.147241 second(s), Total 80, Slave 63 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号