AnsweredAssumed Answered

MQX Ethernet: Send UDP Frame?

Question asked by arnogir on May 11, 2015
Latest reply on May 12, 2015 by arnogir

Hello

 

I'm on a K60D512 (on TWR) and I'm using MQX 4.2 and KDS v3.0.0.

 

I'm trying to work on Ethernet with UDP Frame.

 

I test to send from my laptop to the TWR an UDP frame with success, but not the reverse.

 

I have the following code (simplified)

 

 

#define ENET_IPADDR  IPADDR(192,168,1,128) 
#define ENET_IPMASK  IPADDR(255,255,0,0) 
#define ENET_IPGATEWAY  IPADDR(0,0,0,0) 


error = RTCS_create();

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_bind_staticip(BSP_DEFAULT_ENET_DEVICE, &ip_data);
sock = socket(AF_INET, SOCK_DGRAM, 0);
local_sin.sin_family = AF_INET;
local_sin.sin_port = 32768;
local_sin.sin_addr.s_addr = INADDR_ANY;
 
result = bind(sock, &local_sin, sizeof (sockaddr_in));
while(1)
{
  sock = RTCS_selectall(1000);
  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\n", my_buffer, counter);
      
      count = sendto(sock, my_buffer, count, 0, &local_sin, local_len);
      printf("--------SendTO Return = %d", count);
    }
  }
}

 

So If I set a static IP with ipcfg_bind_staticip send work correctly.

If I do not make it or if I try with IP = 255.255.255.255, sendto() return -1.

 

 

 

Until now, I have in my mind thet UDP not request a  specific IP address?

 

 

In fact, I must have a configurable system which auto adapt to its environment:

 

 

1) TWR <-> LapTop (On TCP)

2) TWR <-> Ethernet  Switch <-> LapTop (On TCP)

3) TWR <-> Ethernet  DHCP ROUTER <-> LapTop (On TCP)

 

So the  idea was to use UDP broadcast frame to request from the tower its IP address. (Allowed by DHCP if exist): LapTop make a Request on UDP, TWR respond on UDP.

If IP address not configured in TWR (because no DHCP), Laptop software will send an UDP frame with a chosen IP address to set TWR TCP/IP address.

This is why I want send UDP frame without previous IP address configuration...

Outcomes