USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(Open_USART, &USART_InitStructure);
/* Enable the Open_USART Transmit interrupt: this interrupt is generated when the
Open_USART transmit data register is empty */
USART_ITConfig(Open_USART,USART_IT_RXNE,ENABLE);
USART_Cmd(Open_USART, ENABLE);
}
void USART_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = Open_USART_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
然后将udp_echoserver_init();注释掉
在while(1)里面添加一下函数
udp_send_data(udp_data, sizeof(udp_data));
代码是
unsigned char udp_data[]= "hello world!";
void udp_send_data(uint8_t* pData, uint16_t len)
{
struct udp_pcb *upcb;
struct pbuf* buff;
struct ip_addr ipaddr;
err_t err;
buff = pbuf_alloc(PBUF_TRANSPORT, 1024, PBUF_ROM);
buff->payload = pData;
buff->len = len;
buff->tot_len = len;
upcb = udp_new();//建立一个新的UDP包
udp_bind(upcb, IP_ADDR_ANY, 7);
IP4_ADDR(&ipaddr, 192, 168, 1, 11); //切记,此处的IP是PC的IP,因为使用PC的截包软件接收
err = udp_connect(upcb, &ipaddr, 7);
if(err == ERR_OK)
{
err = udp_send(upcb, buff);
if(ERR_IS_FATAL(err))
printf("udp_send...%d\r\n",err);
}
udp_disconnect(upcb);
pbuf_free(buff);
udp_remove(upcb);
}
最终main函数是
int main(void)
{
USART_Configuration();
USART_NVIC_Config();
/*Initialize LCD and Leds */
LCD_LED_Init();
/* configure ethernet */
ETH_BSP_Config();
/* Initilaize the LwIP stack */
LwIP_Init();
/* UDP echoserver */
//udp_echoserver_init();
/* Infinite loop */
while (1)
{
udp_send_data(udp_data, sizeof(udp_data));
//Delay(100);
/* check if any packet received */
if (ETH_CheckFrameReceived())
{
/* process received ethernet packet */
LwIP_Pkt_Handle();
}
/* handle periodic timers for LwIP */
LwIP_Periodic_Handle(LocalTime);
}
} |