Unable to receive UDP broadcast packets
- February 25, 2026
- 3 replies
- 263 views
I am able to receive UDP unicast packets on the Nucleo-h563ZI using the netconn API, however I cannot receive broadcast UDP packets (global or subnet). I have attached my lwiopts.h for reference along with the relevant discovery service code below. Note that netif->flags has NETIF_FLAG_BROADCAST set in addition to NETIF_FLAG_ETHARP. I was unable to find any broadcast related settings in the IOC file for the ETH peripheral nor related HAL_ETH_xyz calls. I did try enabling broadcast in the MACFilter, but this had no effect.
#define NET_TASK_DISCO_PORT (5555)
#define NET_TASK_DISCO_REQUEST "SCPI_DISCOVERY"
#define NET_TASK_DISCO_RESPONSE "SCPI_DEVICE"
#define NET_TASK_DISCO_RESP_LENGTH (64)
//
// @brief Discovery application
// @return none
//
static void disco(void *pArgument)
{
char response[NET_TASK_DISCO_RESP_LENGTH];
uint16_t length;
void *pBuffer;
void *pSend;
struct netbuf *prBuffer;
struct netbuf *prSend;
trNet *prNet = (trNet*)pArgument;
// Validate argument[s]
ASSERT(NULL != prNet);
// Create connection and bind to port
if (NULL == (prNet->rDisco.prConnection = netconn_new(NETCONN_UDP)))
{
LOGERROR(eErrorOutOfResources);
return;
}
netconn_bind(prNet->rDisco.prConnection, IP_ADDR_ANY, NET_TASK_DISCO_PORT);
// Forever loop
for(;;)
{
// Receive request
if (ERR_OK == netconn_recv(prNet->rDisco.prConnection, &prBuffer))
{
// Get receive buffer
netbuf_data(prBuffer, &pBuffer, &length);
LOG(eLogLevelDebug, "%s", pBuffer);
// Check request
if (0 == strncmp(pBuffer, NET_TASK_DISCO_REQUEST, length))
{
// Get send buffer
if (NULL != (prSend = netbuf_new()))
{
// Build response
snprintf(response, sizeof(response) - 1,
"%s;IDN=%s;IP=%s;PORT=%u", NET_TASK_DISCO_RESPONSE,
"SPPTS-101", "192.168.0.10", NET_TASK_DISCO_PORT);
LOG(eLogLevelDebug, "%s", response);
// Allocate and copy buffer
pSend = netbuf_alloc(prSend, strlen(response));
memcpy(pSend, response, strlen(response));
// Send response
netconn_sendto(prNet->rDisco.prConnection, prSend,
netbuf_fromaddr(prBuffer), netbuf_fromport(prBuffer));
// Return send buffer
netbuf_delete(prSend);
}
}
// Return buffer
netbuf_delete(prBuffer);
}
}
}Here is the successful response when sending a unicast packet:
bob@xyz:~$ echo -n "SCPI_DISCOVERY" | nc -u 192.168.50.123 5555
SCPI_DEVICE;IDN=SPPTS-101;IP=192.168.0.10;PORT=5555
Relevent settings in lwipopts.h
#define IP_SOF_BROADCAST 1
#define IP_SOF_BROADCAST_RECV 1
#define LWIP_BROADCAST_PING 1
