使用LwIP UDP 协议实现与PC端通讯。现象为:
如果板卡作为客户端给PC发送报文,PC端应用不能收到包,而用wireshark能够抓到包。
如果板卡作为服务端能够接收到PC端发送的报文,也能够恢复,但PC端应用不能收到,WireShark也能够抓到报文。
哪位大侠有解决思路?
程序部分如下:
发送部分
void udp_connect_send(void)
{
struct udp_pcb *upcb;
struct pbuf *p;
struct ip_addr DestIPaddr;
struct ip_addr SrcIPaddr;
err_t err;
/* Create a new UDP control block */
upcb = udp_new();
if (upcb!=NULL)
{
/*assign destination IP address */
IP4_ADDR( &DestIPaddr, DEST_IP_ADDR0, DEST_IP_ADDR1, DEST_IP_ADDR2, DEST_IP_ADDR3 );
/* configure destination IP address and port */
err= udp_connect(upcb, &DestIPaddr, UDP_SERVER_PORT);
if (err == ERR_OK)
{
sprintf((char*)data, "sending udp client message %d", (int*)message_count);
/* allocate pbuf from pool*/
p = pbuf_alloc(PBUF_TRANSPORT,strlen((char*)data), PBUF_POOL);
if (p != NULL)
{
/* copy data to pbuf */
pbuf_take(p, (char*)data, strlen((char*)data));
/* send udp data */
err = udp_send(upcb, p);
/* free pbuf */
pbuf_free(p);
}
else
{
/* free the UDP connection, so we can accept new clients */
udp_remove(upcb);
#ifdef SERIAL_DEBUG
printf("\n\r can not allocate pbuf ");
#endif
}
}
else
{
/* free the UDP connection, so we can accept new clients */
udp_remove(upcb);
#ifdef SERIAL_DEBUG
printf("\n\r can not connect udp pcb");
#endif
}
}
else
{
#ifdef SERIAL_DEBUG
printf("\n\r can not create udp pcb");
#endif
}
}
服务部分
void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
{
/* Connect to the remote client */
udp_connect(upcb, addr, UDP_CLIENT_PORT);
/* Tell the client that we have accepted it */
udp_send(upcb, p);
/* free the UDP connection, so we can accept new clients */
udp_disconnect(upcb);
/* Free the p buffer */
pbuf_free(p);
listen = 0;
}
static void StartListen(void)
{
err_t err;
if(!listen) {
listen = 1;
rcvpcb = udp_new();
if (rcvpcb)
{
/* Bind the rcvpcb to the UDP_PORT port */
/* Using IP_ADDR_ANY allow the upcb to be used by any local interface */
err = udp_bind(rcvpcb, IP_ADDR_ANY, UDP_SERVER_PORT);
if(err == ERR_OK)
{
/* Set a receive callback for the upcb */
udp_recv(rcvpcb, udp_receive_callback, NULL);
}
else
{
udp_remove(rcvpcb);
printf("can not bind pcb");
}
}
else
{
printf("can not create pcb");
}
}
}
|