cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo-F767ZI UDP Echo Server not working

TKham.1
Associate

Dear STM support,

I'm trying to work on udp communication between my nucleo-f767zi (server) and my ubuntu PC (client). I use LwIP middleware generated by STM32CudeIDE for UDP.

I set the static IP address on STM to be 192.168.0.10

static IP address on my PC 192.168.0.11

same netmask (255.255.255.0) and gateway (192.168.0.1) on both sides.

My code is as follows.

main.c

int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART3_UART_Init();
  MX_USB_OTG_FS_PCD_Init();
  MX_LWIP_Init();
  /* USER CODE BEGIN 2 */
  RetargetInit(&huart3);
  udp_echoserver_init();
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  printf("\r\nHello STM32f767\r\n");
  uint8_t a = 0;
  while (1)
  {
    /* USER CODE END WHILE */
	  printf("\r\nHello STM32f767 %u\r\n", a++);
	  HAL_Delay(1000);
    /* USER CODE BEGIN 3 */
	  MX_LWIP_Process();
  }
  /* USER CODE END 3 */
}

udp_echoserver.c

#define UDP_SERVER_PORT    7   /* define the UDP local connection port */
#define UDP_CLIENT_PORT    7   /* define the UDP remote connection port */
 
void udp_echoserver_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
 
void udp_echoserver_init(void)
{
   struct udp_pcb *upcb;
   err_t err;
 
   /* Create a new UDP control block  */
   upcb = udp_new();
 
   if (upcb)
   {
     /* Bind the upcb to the UDP_PORT port */
     /* Using IP_ADDR_ANY allow the upcb to be used by any local interface */
      err = udp_bind(upcb, IP_ADDR_ANY, UDP_SERVER_PORT);
 
      if(err == ERR_OK)
      {
        /* Set a receive callback for the upcb */
        udp_recv(upcb, udp_echoserver_receive_callback, NULL);
      }
      else
      {
        udp_remove(upcb);
      }
   }
}
 
void udp_echoserver_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
  HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);
  printf("\r\nHi UDP %d\r\n", p->len);
/* Connect to the remote client */
  if(udp_connect(upcb, addr, UDP_CLIENT_PORT) == ERR_OK){
	  printf("\r\nconnect ok\r\n");
  }
/* Tell the client that we have accepted it */
  if(udp_send(upcb, p) == ERR_OK){
	  printf("\r\nsend ok\r\n");
  }
/* free the UDP connection, so we can accept new clients */
  udp_disconnect(upcb);
/* Free the p buffer */
  pbuf_free(p);
}

There is no error from udp_connect() and udp_send() as the sentences are printed out.

On the client side, I use netcat i.e.

nc -u 192.168.0.10 7

The server is able to receive the message from client (shown by toggling LED and prinf functions inside the callback function). However, when the server echo back to client, the client doesn't receive that message.

Could you help me with this issue?

Best regards,

0 REPLIES 0