stm32f107裸机+HR911105A+lwIP 1.31 动态获取IP与远端服务器连接,网口link灯常亮,信号灯闪黄灯。
配置静态或者动态IP与远端服务器发起TCP连接,ARP回包后(Wareshark抓包),pcb状态一直为TCP_SENT状态,直至重试次数耗尽。tcp_connect()返回0,但是回调函数没有执行。请大佬们给看看,谢谢了
void TCP_Client_Init(u16_t local_port,u16_t remote_port,unsigned char a,unsigned char b,unsigned char c,unsigned char d)
{
struct ip_addr ipaddr;
struct tcp_pcb *tcp_client_pcb = 0;
err_t err;
mqttinfo.pcb = 0;
if((IPaddress!=0)&&(tcp_active_pcbs == NULL)) //确定IP已建立连接,获取到IP地址
{
IP4_ADDR(&ipaddr,a,b,c,d); //服务器IP地址
tcp_client_pcb = tcp_new(); /* 建立通信的TCP控制块(Clipcb) */
if (!tcp_client_pcb)
return ;
err = tcp_bind(tcp_client_pcb,IP_ADDR_ANY,local_port); /* 绑定本地IP地址和端口号 ,本地ip地址在LwIP_Init()中已经初始化*/
if(err != ERR_OK)
return ;
printf("server:%d %d %d %d\n", a, b, c, d);
tcp_connect(tcp_client_pcb,&ipaddr,remote_port,TCP_Connected);//注册回调函数
}
}
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();
}
/* 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();
}
if (localtime - IPaddTimer >= IP_TIMER_MSECS)
{
IPaddTimer = localtime;
ChangeNetifLinkStatues();
/* We have got a new IP address so update the display */
if (IPaddress != netif.ip_addr.addr)
{
IPaddress = netif.ip_addr.addr;
if (netif.flags & NETIF_FLAG_DHCP)
{
/* Initialize the client application */
TCP_Client_Init(TCP_LOCAL_PORT,TCP_SERVER_PORT,TCP_SERVER_IP);
}
}
else if (IPaddress == 0)
{
/* We still waiting for the DHCP server */
/* 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, 252);
IP4_ADDR(&netmask, 255, 255, 255, 0);
IP4_ADDR(&gw, 192, 168, 1, 1);
netif_set_addr(&netif, &ipaddr , &netmask, &gw);
}
}
}
}
#define TCP_SERVER_IP 192.168.1.52
#define TCP_LOCAL_PORT 10300
#define TCP_SERVER_PORT 18183
uint32_t IPaddress = 0;
uint8_t MACaddr[6];
/* Private function prototypes -----------------------------------------------*/
extern CHGSTATINF SysInfo;
extern uint8_t EthInitStatus;
extern void Ethernet_Configuration(void);
extern void client_init(void);
extern void server_init(void);
void My_IP4_ADDR(struct ip_addr *ipaddr,unsigned char a,unsigned char b,unsigned char c,unsigned char d);
void ETH_link_callback(struct netif *xnetif)
{
uint8_t i = 0;
struct tcp_pcb *pcb = 0;
struct tcp_pcb *cpcb = 0;
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
if(netif_is_link_up(xnetif)) //联网
{
/* Restart MAC interface */
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
IPaddress = 0;
netif_set_addr(xnetif, &ipaddr , &netmask, &gw);
dhcp_start(xnetif);
/* When the netif is fully configured this function must be called.*/
netif_set_up(xnetif);
}
else //断网
{
dhcp_stop(xnetif);
netif_set_down(xnetif);
for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
i++;
for(pcb = tcp_active_pcbs; i > 0 ; i--)
{
cpcb = pcb;
pcb = pcb->next;
tcp_abort(cpcb);
}
IPaddress = 0;
g_tcp_link_state = 0;
mqttinfo.pcb = 0;
mqttinfo.state = MQTT_STAT_ERR;
}
}
void LwIP_Init(void)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
/* Initializes the dynamic memory heap defined by MEM_SIZE.*/
mem_init();
/* Initializes the memory pools defined by MEMP_NUM_x.*/
memp_init();
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
Set_MAC_Address();
netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
netif_set_default(&netif);
/* 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);
/* When the netif is fully configured this function must be called.*/
netif_set_up(&netif);
netif_set_link_callback(&netif, ETH_link_callback);
}
void LwIP_ReInit(void)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
netif_remove(&netif);
IPaddress = 0;
/* Initializes the dynamic memory heap defined by MEM_SIZE.*/
mem_init();
/* Initializes the memory pools defined by MEMP_NUM_x.*/
memp_init();
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
Set_MAC_Address();
netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
netif_set_default(&netif);
dhcp_start(&netif);
netif_set_up(&netif);
netif_set_link_callback(&netif, ETH_link_callback);
}
/**
* @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);
}
void Reset_link_up(void)
{
netif_set_link_up(&netif);
}
//检查网口物理连接状态
void ChangeNetifLinkStatues(void)
{
if(((ETH_ReadPHYRegister(0x01, PHY_BSR) & PHY_Linked_Status) == Link_Down))
{
if(g_eth_link_state != Link_Down) //拔网线
{
netif_set_link_down(&netif);
g_eth_link_state = Link_Down;
}
}
else //插网线
{
if(EthInitStatus == 0)
{
Ethernet_Configuration(); // 有1S左右的等待时间
LwIP_ReInit();
// netif_set_link_up(&netif);
g_eth_link_state = Link_Up;
}
else
{
if(g_eth_link_state != Link_Up)
{
netif_set_link_up(&netif);
g_eth_link_state = Link_Up;
// TCP_Client_Close();
// LwIP_ReInit(); //此处不能再ReInit,会导致掉包
}
}
}
}
void TcpMonCom(void)
{
LwIP_Periodic_Handle(LocalTime); //TCPIP
}
int main()
{
while(1)
{
TcpMonCom();
}
}