目的 :把开发版挂到路由器上,通过PC发送UDP包控制开发版的LED亮灭
#include "stdint.h"
#include "udp.h"
#include "main.h"
#define LOCAL_PORT 80
#define REMOTE_PORT 777
struct udp_pcb *udp_my_led;
extern void app_printf(char * format,...);
void app_udp_myled_process();
uint16_t _init_my_led_udp(struct udp_pcb *appPCB)
{
struct ip_addr remote_ipaddr;
struct ip_addr local_ipaddr;
appPCB = udp_new();
IP4_ADDR(&local_ipaddr, 192, 168, 0, 253);
udp_bind(appPCB,&local_ipaddr,LOCAL_PORT);
IP4_ADDR(&remote_ipaddr, 192, 168, 0, 200);
udp_connect(appPCB, &remote_ipaddr,REMOTE_PORT);
udp_recv(appPCB,app_udp_myled_process,0);
return 0;
}
void app_udp_myled_process(void *arg, struct udp_pcb *upcb, struct pbuf *p,
struct ip_addr *addr, u16_t port)
{
uint8_t *data;
struct pbuf *q;
const char *info = "This is my first UDP data \n";
GPIO_ToggleBits(GPIOE, GPIO_Pin_14);
data =(uint8_t *) p->payload;
app_printf("play load %s: \r\n",data);
pbuf_free(p);
q = pbuf_alloc(PBUF_TRANSPORT, strlen(info)+1, PBUF_RAM);
memset(q->payload,0,q->len);
memcpy(q->payload,info,strlen(info));
udp_sendto(upcb,q,addr,port);
}
uint16_t app_udp_init(void)
{
_init_my_led_udp(udp_my_led);
return 0;
}
void app_udp_send(void)
{
struct pbuf *p;
udp_send(udp_my_led,p);
}
|