Skip to main content
Visitor II
March 13, 2024
Question

Problem With UDP Broadcast receive

  • March 13, 2024
  • 1 reply
  • 2862 views

I am using the STM32F107VCT6 controller. use STMcubeIDE.

I write the code of the UDP Server. but when I change the IP, subnet, and Gateway using TCP and reset the device. I do not get Broadcast info or the udp_recv function not working.LWIP.PNG

void udpServer_init(void)
{

 
// UDP Control Block structure
 
struct udp_pcb *upcb;
//   struct udp_pcb *upcb;
   err_t err;
 
   /* 1. Create a new UDP control block  */
   upcb = udp_new();
   if(upcb == NULL){
   HAL_UART_Transmit(&huart1, "fail\n", 5, 5);
   }
 
   /* 2. Bind the upcb to the local port */
 
 
   err = udp_bind(upcb, IP_ADDR_ANY , 1225);  // 7 is the server UDP port
 
 
   /* 3. Set a receive callback for the upcb */
   if(err == ERR_OK)
   {
   HAL_UART_Transmit(&huart1, "ENTER IN UDP bind\n", 20,20);
   udp_recv(upcb, udp_receive_callback, NULL);
   }
   else
   {
   HAL_UART_Transmit(&huart1, "ENTER IN UDP remo\n", 20,20);
   udp_remove(upcb);
   }
}
 
// udp_receive_callback will be called, when the client sends some data to the server
/* 4. Process the datagram packet and send a reply to client. */
 
void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
HAL_UART_Transmit(&huart1, "ENTER IN UDP rec\n", 20,20);
struct pbuf *txBuf;
 
/* Get the IP of the Client */
char *remoteIP = ipaddr_ntoa(addr);
 
 
char buf[100];
memset (buf, '\0', 100);
char load[100];
memset (load, '\0', 100);
 
strcpy(load,p->payload);
uint8_t len = 0;
 
if(load[0]=='I'&& load[1]=='N'&&load[2]=='F'&& load[3]=='O')
{
HAL_UART_Transmit(&huart1, "ENTER IN UDP rec1\n", 20,20);
 
 
len = sprintf(buf,"IP = %u.%u.%u.%u \n SUBNET = %u.%u.%u.%u \n GETWAY = %u.%u.%u.%u \n",(char*)IP_ADDRESS[0],(char*)IP_ADDRESS[1],(char*)IP_ADDRESS[2],(char*)IP_ADDRESS[3]
,(char*)NETMASK_ADDRESS[0],(char*)NETMASK_ADDRESS[1],(char*)NETMASK_ADDRESS[2],(char*)NETMASK_ADDRESS[3]
,(char*)GATEWAY_ADDRESS[0],(char*)GATEWAY_ADDRESS[1],(char*)GATEWAY_ADDRESS[2],(char*)GATEWAY_ADDRESS[3]);
 
}
 
else
{
len = sprintf(buf,"Invalid Command");
}
 
/* allocate pbuf from RAM*/
txBuf = pbuf_alloc(PBUF_TRANSPORT,len, PBUF_RAM);
 
/* copy the data into the buffer  */
pbuf_take(txBuf, buf, len);
 
/* Connect to the remote client */
udp_connect(upcb, addr, port);
 
/* Send a Reply to the Client */
udp_send(upcb, txBuf);
// udp_sendto(upcb, txBuf, addr, port);
 
/* free the UDP connection, so we can accept new clients */
udp_disconnect(upcb);
 
/* Free the p_tx buffer */
pbuf_free(txBuf);
 
/* Free the p buffer */
pbuf_free(p);
 
}
    This topic has been closed for replies.

    1 reply

    Super User
    March 13, 2024

    > err = udp_bind(upcb, IP_ADDR_ANY , 1225); // 7 is the server UDP port

    Which is it - port 1225 or port 7?

    What IP/mask/gateway values worked?  What are the IP address/mask/gateway settings of the device sending the UDP broadcast?

    GETANSAuthor
    Visitor II
    March 14, 2024

    IP : 192.168.4.111

    Subnetmask : 255.255.255.0

    Gateway : 192.168.4.1

    and port : 1225

    Super User
    March 15, 2024

    Maybe I mis-understood your original post because you re-posted the same IP address as in your first post.  Did this code EVER work?  To me it sounded like it worked, and then you changed the IP address and it stopped working.

    If it DID work and now does not - provide BOTH the original IP address info and new IP address info

    What is the IP address of the device that is sending the UDP broadcasts?

    What does your program print on the serial port?

    Do you call MX_LwIP_Process() somewhere in your main loop?