AnsweredAssumed Answered

Help with UDP example

Question asked by Dao Yan Lim on Feb 16, 2017
Latest reply on Mar 7, 2017 by Daniel Chen

Hi,

 

I am using TWR-K60N512 with PK60DN512Z VMD10 chip, and Codewarrior 10.5 IDE with MQX 3.8. I was following a very simple UDP example provided by Garoba, which is found in this link Simple UDP communication .

 

My code is almost the same, except I changed 'local_sin.sin_port' to something random, port 7. And I removed the ENET_initialize() after 'InitializeNetworking()', because that's already done in the function anyway. 

 

It gets stuck at this line: sock = RTCS_selectall(0). This function waits for activity to be detected on any socket that the calling task owns.

 

1) What does this mean?

2) In the main task, where this function is called, a socket is created, so is it waiting for activity to be detected on that particular socket?

3) How do I create an activity on that socket?

4) Do I open another virtual terminal with port number set to 7, but with what Host address?

 

I tried the 169.254.3.3, 169.254.3.4, and the IP address of my system, nothing works. How should I troubleshoot this?

 

 

 

#ifndef ENET_IPADDR
#define ENET_IPADDR IPADDR(169,254,3,3)
#endif

#ifndef ENET_IPMASK
#define ENET_IPMASK IPADDR(255,255,0,0)
#endif

#ifndef ENET_IPGATEWAY
#define ENET_IPGATEWAY IPADDR(0,0,0,0)

endif

 

void Main_task(uint_32 initial_data)
{
   uint_32 sock;
   sockaddr_in local_sin;
   uint_32 result;
   uint_32 count;
   uint_32 counter = 0;   
   char my_buffer[10];
   uint_16 local_len = sizeof(local_sin);

   _enet_address enet_address;
   _enet_handle ehandle;
   _rtcs_if_handle ihandle;

   ENET_STATS_PTR enet;
   UDP_STATS_PTR udp;

   InitializeNetworking(6,6,6,1);

   printf("\nInitializing UDP server...\n");

   sock = socket(PF_INET,SOCK_DGRAM,0);

   if ( sock == RTCS_SOCKET_ERROR )
   {
      printf("\nError, socket created failed");
      return;
   }

   memset((char *)&local_sin,0,sizeof(local_sin)); // reset local_sin strucutre to zero
   local_sin.sin_family = AF_INET;
   local_sin.sin_port = 7;
   local_sin.sin_addr.s_addr = INADDR_ANY;

   result = bind(sock,&local_sin, sizeof(sockaddr_in));

   if ( result != RTCS_OK )
   {
      printf( "\nError, bind() failed with error code %lx",result);
   }

   while(1)
   {
      if ( counter > 100 )
      {
         break;
      }

      sock = RTCS_selectall(0);

      if ( sock == 0 )
      {
         printf("TimeOut expired. No data received. Trying again\n");
      }
      else
      {
         count = recvfrom(sock,my_buffer,10,0,&local_sin,&local_len);

         if ( count == RTCS_ERROR )
         {
            printf("\nrecvfrom() failed with error %lx",RTCS_geterror(sock));
         }
         else
         {
            counter++;
            printf("\nReceived %ld bytes of data.",count);
            printf("Data received: %s, Packet %d",my_buffer,counter);
         }
      }
   }
   udp = UDP_stats();
   enet = ENET_get_stats(ehandle);
   printf("\n%d Ethernet packets ST_RX_TOTAL", enet->COMMON.ST_RX_TOTAL);
   printf("\n%d Ethernet packets ST_RX_MISSED", enet->COMMON.ST_RX_MISSED);
   printf("\n%d Ethernet packets ST_RX_DISCARDED", enet->COMMON.ST_RX_DISCARDED);
   printf("\n%d UDP ST_RX_BAD_CHECKSUM", udp->ST_RX_BAD_CHECKSUM);
   printf("\n%d UDP ST_RX_SMALL_DGRAM", udp->ST_RX_SMALL_DGRAM);
   printf("\n%d UDP ST_RX_SMALL_PKT", udp->ST_RX_SMALL_PKT);
}

 

void InitializeNetworking(uint_32 pcbs, uint_32 msgs, uint_32 sockets, boolean dhcp)
{
   int_32 error;
   IPCFG_IP_ADDRESS_DATA ip_data;
   _enet_address enet_address;

   // runtime RTCS configuration
   _RTCSPCB_init = pcbs; // number of RTCS pcb available
   _RTCS_msgpool_init = msgs;
   _RTCS_socket_part_init = sockets;

   error = RTCS_create();

   if ( error == RTCS_OK )
   {
      ip_data.ip = ENET_IPADDR;
      ip_data.mask = ENET_IPMASK;
      ip_data.gateway = ENET_IPGATEWAY;

      ENET_get_mac_address(BSP_DEFAULT_ENET_DEVICE,ENET_IPADDR,enet_address);
      ipcfg_init_device(BSP_DEFAULT_ENET_DEVICE,enet_address);
      ipcfg_add_dns_ip(BSP_DEFAULT_ENET_DEVICE,LWDNS_server_ipaddr); 


      // check link status
      printf("\nWaiting for ethernet cable to be plugged in... ");
      while( !ipcfg_get_link_active(BSP_DEFAULT_ENET_DEVICE) ){}; // returns immediate ethernet link state
      printf("Cable connected\n");

      // If Dymanic Host Configuation Protocol is enabled, get IP address from DHCP server
      if ( dhcp )
      {
         printf("\nDHCP bind ...");
         error = ipcfg_bind_dhcp_wait(BSP_DEFAULT_ENET_DEVICE,1,&ip_data); 

         if ( error != IPCFG_ERROR_OK )
         {
            printf("Error %08x!\n",error);
         }
         else
         {
            printf("Successful!\n");
         }
      }
      else // else bind with static IP
      {
         printf("\nStatic IP bind... ");
         error = ipcfg_bind_staticip(BSP_DEFAULT_ENET_DEVICE,&ip_data);

         if ( error != IPCFG_ERROR_OK )
         {
            printf("Error %08x!\n",error);
         }
         else
         {
            printf("Successful!\n");
         }
      }

      if ( error == IPCFG_ERROR_OK )
      {
         ipcfg_get_ip(BSP_DEFAULT_ENET_DEVICE,&ip_data); 
         printf("\nIP Address : %d.%d.%d.%d\n",IPBYTES(ip_data.ip)); 
         printf("Subnet Address : %d.%d.%d.%d\n",IPBYTES(ip_data.mask));
         printf("Gateway Address : %d.%d.%d.%d\n",IPBYTES(ip_data.gateway));
         printf("DNS Address : %d.%d.%d.%d\n",IPBYTES(ipcfg_get_dns_ip(BSP_DEFAULT_ENET_DEVICE,0)));
      }
      else
      {
         printf("\nRTCS_create failed!\n");
         _task_block();
      }
   }
}

 

 

Thanks.

Outcomes