大学生
最后登录1970-1-1
在线时间 小时
注册时间2014-4-11
|
楼主 |
发表于 2014-4-23 16:35:57
|
显示全部楼层
#include <stdio.h>
#include "stm32f10x.h"
#include "err.h"
#include "etharp.h"
#include "netconfig.h"
#include "bsp_SysTick.h"
#include "spi.h"
__IO uint32_t LocalTime = 0;
const static u8 TCP_TestData[]={0x01,0x02,0x03,0x04,0x05,0x06,0x07};
const static u8 TCP_TestData1[]={0x02,0x03,0x08,0x09,0x07,0x04,0x01};
struct tcp_pcb *Check_TCP_Connect(void);
err_t TcpCli_Connected(void *arg,struct tcp_pcb *pcb,err_t err)
{
tcp_write(pcb,TCP_TestData,sizeof(TCP_TestData),0);
tcp_write(pcb,TCP_TestData1,sizeof(TCP_TestData1),0);
return ERR_OK;
}
void Delay(u32 ulVal)
{
while ( --ulVal != 0 );
}
void TCP_Client_Init()
{
struct tcp_pcb *Clipcb;
struct ip_addr ipaddr;
IP4_ADDR(&ipaddr,169,254,223,245);
Clipcb = tcp_new();
tcp_bind(Clipcb,IP_ADDR_ANY,1026);
tcp_connect(Clipcb,&ipaddr,1026,TcpCli_Connected);
}
void delay(u32 kcf)
{
while(kcf--);
}
int main(void)
{
struct tcp_pcb *pcb;
/*³õʼ»¯ ÒÔÌ«ÍøSPI½Ó¿Ú*/
ENC_SPI_Init();
/*³õʼ»¯systick£¬ÓÃÓÚ¶¨Ê±ÂÖѯÊäÈë»ò¸øLWIPÌṩ¶¨Ê±*/
SysTick_Init();
/* ³õʼ»¯LWIPЭÒéÕ»*/
LwIP_Init();
TCP_Client_Init();
while ( 1 )
{
pcb = Check_TCP_Connect();
if(pcb != 0)
{
tcp_write(pcb,TCP_TestData,sizeof(TCP_TestData),0);
tcp_write(pcb,TCP_TestData1,sizeof(TCP_TestData1),0);
tcp_output(pcb);
}
Delay(0xfffff);
LwIP_Periodic_Handle(LocalTime);
}
}
struct tcp_pcb *Check_TCP_Connect(void)
{
struct tcp_pcb *cpcb = 0;
unsigned char connect_flag = 0;
connect_flag = 0;
for(cpcb = tcp_active_pcbs;cpcb != NULL; cpcb = cpcb->next)
{
if(cpcb -> state == ESTABLISHED)
{
connect_flag = 1; //Á¬½Ó±êÖ¾
break;
}
}
if(connect_flag == 0) // TCP_LOCAL_PORTÖ¸¶¨µÄ¶Ë¿ÚδÁ¬½Ó»òÒѶϿª
{
TCP_Client_Init();//ÖØÐÂÁ¬½Ó
cpcb = 0;
}
return cpcb;
}
|
|