UdpSend("123",3);
}
void uip_polling(void)
{
u8 i;
static struct timer periodic_timer, arp_timer;
static u8 timer_ok=0;
if(timer_ok==0)//仅初始化一次
{
timer_ok = 1;
timer_set(&periodic_timer,CLOCK_SECOND/2); //创建1个0.5秒的定时器
timer_set(&arp_timer,CLOCK_SECOND*10); //创建1个10秒的定时器
}
uip_len=tapdev_read(); //从网络设备读取一个IP包,得到数据长度.uip_len在uip.c中定义
if(uip_len>0) //有数据
{
//处理IP数据包(只有校验通过的IP包才会被接收)
if(BUF->type == htons(UIP_ETHTYPE_IP))//是否是IP包?
{
uip_arp_ipin(); //去除以太网头结构,更新ARP表
uip_input(); //IP包处理
//当上面的函数执行后,如果需要发送数据,则全局变量 uip_len > 0
//需要发送的数据在uip_buf, 长度是uip_len (这是2个全局变量)
if(uip_len>0)//需要回应数据
{
uip_arp_out();//加以太网头结构,在主动连接时可能要构造ARP请求
tapdev_send();//发送数据到以太网
}
}else if (BUF->type==htons(UIP_ETHTYPE_ARP))//处理arp报文,是否是ARP请求包?
{
uip_arp_arpin();
//当上面的函数执行后,如果需要发送数据,则全局变量uip_len>0
//需要发送的数据在uip_buf, 长度是uip_len(这是2个全局变量)
if(uip_len>0)
tapdev_send();//需要发送数据,则通过tapdev_send发送
}
}else if(timer_expired(&periodic_timer)) //0.5秒定时器超时
{
timer_reset(&periodic_timer); //复位0.5秒定时器
//轮流处理每个TCP连接, UIP_CONNS缺省是40个
for(i=0;i<UIP_CONNS;i++)
{
uip_periodic(i); //处理TCP通信事件
//当上面的函数执行后,如果需要发送数据,则全局变量uip_len>0
//需要发送的数据在uip_buf, 长度是uip_len (这是2个全局变量)
if(uip_len>0)
{
uip_arp_out();//加以太网头结构,在主动连接时可能要构造ARP请求
tapdev_send();//发送数据到以太网
}
}
#if UIP_UDP //UIP_UDP
//轮流处理每个UDP连接, UIP_UDP_CONNS缺省是10个
for(i=0;i<UIP_UDP_CONNS;i++)
{
uip_udp_periodic(i); //处理UDP通信事件
//当上面的函数执行后,如果需要发送数据,则全局变量uip_len>0
//需要发送的数据在uip_buf, 长度是uip_len (这是2个全局变量)
if(uip_len > 0)
{
uip_arp_out();//加以太网头结构,在主动连接时可能要构造ARP请求
tapdev_send();//发送数据到以太网
}
}
#endif
//每隔10秒调用1次ARP定时器函数 用于定期ARP处理,ARP表10秒更新一次,旧的条目会被抛弃
if(timer_expired(&arp_timer))
{
timer_reset(&arp_timer);
uip_arp_timer();
}
}
}
然后是udp文件
void UdpSend(char *str,unsigned short len)
{
char *nptr;
nptr = (char *)uip_appdata;
memcpy(nptr, str, len);
uip_udp_send(len); //发送数据
}
/*---------------------------------------------------------------------------*
*函数名:UdpNewData
*功能:接收数据后的处理函数
*形参:无
*返回:无
*---------------------------------------------------------------------------*/
void UdpNewData()
{
unsigned char ReceiveData[64];
// unsigned char SendData[64];
short len;
len = uip_datalen(); //读取数据长度
if(len>64)
len = 64;
memcpy(ReceiveData,uip_appdata,len);
// ReceiveData="nihaome";
if(ReceiveData[0] == 0x30)
{
memcpy(uip_appdata,ReceiveData,len);
uip_udp_send(len);
}
// UdpSend(SendData,strlen(SendData));
}
/*---------------------------------------------------------------------------*
*函数名:UdpAppcall
*功能:回调函数
*形参:无
*返回:无
*---------------------------------------------------------------------------*/
void UdpAppcall(void)
{
if(uip_udp_conn->rport == HTONS(1400)) //8000端口发来数据
{
if(uip_newdata()) //如果收到数据
{
UdpNewData();
}
}
}
void UDPConnect(void)
{
struct uip_udp_conn *c;
uip_ipaddr_t ipaddr;
uip_ipaddr(&ipaddr,192,168,1,104); //设置IP为192.168.1.103
// uip_connect(&ipaddr,htons(1400)); //端口为1400
LCD_ShowString(30,20,200,16,16,"udp ");
uip_udp_new(&ipaddr,HTONS(12345));
if(c != NULL)
{
LED1=!LED1;
uip_udp_bind(c, HTONS(12344));
}
}
可以ping同 但是发不了数据 不知道为什么 在UDPConnect(void)里的if(c!=NULL)进不去 不知道是为什么 收不到数据 也发不出去数据 不知道哪里有问题