Sign In

Don't have a Xilinx account yet?

  • Choose to receive important news and product information
  • Gain access to special content
  • Personalize your web experience on Xilinx.com

Create Account

Username

Password

Forgot your password?
XClose Panel
Xilinx Home
Reply
Visitor
1916939
Posts: 7
Registered: ‎03-27-2012
0

Get no UDP Connection

Hi again.

This week I'm working on a UDP connection over ethernet but it does not working. I just wanna send data packages from my ML507 board to the PC. The board is a client and the pc is the server.  I did have made this connection with TCP and it worked fine. But I need UDP. I looked in many examples but it still not send any data. I attached my sourcecode below.

Hope that anybody can give me a hint.

Thanks. Tobi

 

/*
 * SimpleUDPClient.c
 *
 *  Created on: 03.05.2012
 *      Author: test
 */
/************************************************************************/
/* Includes                                                             */
/************************************************************************/
#include <xparameters.h>
#include <stdio.h>
#include <string.h>

#include "SimpleUDPClient.h"
#include "platform.h"
#include "xuartns550.h"
#include "netif/xadapter.h"
#include "lwip/err.h"
#include "lwip/udp.h"

/************************************************************************/
/* Defines                                                              */
/************************************************************************/
#define BUFFER_SIZE 128

/********** Global Variables **********/
/********** Interrupt Handler **********/

/********** Methods **********/
void
print_ip(char *msg, struct ip_addr *ip)
{
	print(msg);
	xil_printf("%d.%d.%d.%d\n\r", ip4_addr1(ip), ip4_addr2(ip),
			ip4_addr3(ip), ip4_addr4(ip));
}

void
print_ip_settings(struct ip_addr *ip, struct ip_addr *mask, struct ip_addr *gw)
{

	print_ip("Board IP: ", ip);
	print_ip("Netmask : ", mask);
	print_ip("Gateway : ", gw);
}

int main(void)
{
	struct netif *netif, server_netif;
	struct ip_addr ipaddr, netmask, gw, pc_ipaddr;
	struct udp_pcb *pcb;
	err_t err;
	//unsigned port = (u16_t)5001;
	unsigned pc_port = (u16_t)5001;
	struct pbuf *pbuf_to_be_sent;
	char send_buffer[BUFFER_SIZE];
	int i;

	/* the mac address of the board. this should be unique per board */
	unsigned char mac_ethernet_address[] = { 0x00, 0x0A, 0x35, 0x01, 0xD7, 0xA6 }; // MAC of my ML507 Board -- Tobi

	netif = &server_netif;

	/**** Initialize Platform ****/
	init_platform();

	xil_printf("\n\nSimple UDP Client\n");
	xil_printf("-----------------------\n");
	xil_printf("Successful Initialization of RS232 UART\n");

	/* initliaze IP addresses to be used */
	IP4_ADDR(&ipaddr,  	192, 168,   1, 10);
	IP4_ADDR(&netmask, 	255, 255, 255,  0);
	IP4_ADDR(&gw,      	192, 168,   1,  1);
	IP4_ADDR(&pc_ipaddr,192, 168, 1 , 20); /* server address */


	xil_printf("\n\n-----lwIP UDP Test App ------\n");
	print_ip_settings(&ipaddr, &netmask, &gw);

	lwip_init();
	//udp_init();

  	/* Add network interface to the netif_list, and set it as default */
	if (!xemac_add(netif, &ipaddr, &netmask, &gw, mac_ethernet_address, XPAR_HARD_ETHERNET_MAC_CHAN_0_BASEADDR)) {
		xil_printf("Error adding N/W interface\n");
		return -1;
	}
	netif_set_default(netif);

	/* now enable interrupts */
	platform_enable_interrupts();

	/* specify that the network if is up */
	netif_set_up(netif);

	/* create new UDP PCB structure */
	pcb = udp_new();
	if (!pcb) {
		xil_printf("Error creating PCB. Out of Memory\n");
		return -1;
	}

	/* bind local address */
	if ((err = udp_bind(pcb, IP_ADDR_ANY, 0)) != ERR_OK) {
		xil_printf("error on udp_bind: %x\n", err);
	}

	/* connect to server */
	if ((err = udp_connect(pcb, &pc_ipaddr, pc_port)) != ERR_OK) {
		xil_printf("error on udp_connect: %x\n", err);
	}

	/* initialize data buffer being sent */
	for (i = 0; i < BUFFER_SIZE; i++)
		send_buffer[i] = (i % 10) + '0';

	pbuf_to_be_sent = pbuf_alloc(PBUF_RAW, 200, PBUF_POOL);

	if (!pbuf_to_be_sent)
		xil_printf("error allocating pbuf to send\n");
	else {
		//memcpy(pbuf_to_be_sent->payload, send_buffer, 1024);
		//pbuf_to_be_sent->len = 1450;
		pbuf_to_be_sent->payload = send_buffer;
	}

	/* send buffer to pc */
	err = udp_send(pcb, pbuf_to_be_sent);
	if (err != ERR_OK) {
		xil_printf("Error on udp_send: %d\n", err);
	}

	xil_printf("enter while loop!\n");
	while(1)
	{
		xemacif_input(netif);

		if (XUartNs550_IsReceiveData (XPAR_RS232_BASEADDR)) break;
	}

	/* never reached */
	cleanup_platform();
	xil_printf("Exiting main()!\n");

	return XST_SUCCESS;
}