Handle multiple UDP packets at time
Posted: Mon Feb 17, 2020 2:21 pm
On a Nucleo F429ZI I wrote this code:
It works fine but if I send multiple UDP packet at the same time (even only 4-5) one of them is not received (never!).
The packets are very shorts, about 40 bytes each.
How would you improve my code?
Code: Select all
#include <LwIP.h>
#include <STM32Ethernet.h>
#define UDP_TX_PACKET_MAX_SIZE 512
#include <EthernetUdp.h>
IPAddress remoteIP;
unsigned int remotePort;
EthernetUDP Udp;
uint8_t packetBuffer[UDP_TX_PACKET_MAX_SIZE];
void setup()
{
Serial.begin(115200);
IPAddress localIP = /***/;
IPAddress netmask = /***/;
IPAddress dns = /***/;
IPAddress gateway = /***/;
Ethernet.begin(localIP, netmask, dns, gateway);
uint8_t *mac = Ethernet.macAddress();
int port = 8888;
Udp.begin(port);
}
void loop()
{
processUDPMessage();
}
void processUDPMessage(void)
{
while (int size = Udp.parsePacket())
{
if (size > 0) parsePacket(size);
}
}
void parsePacket(int size)
{
remoteIP = Udp.remoteIP();
remotePort = Udp.remotePort();
Udp.read(packetBuffer, size);
Serial.println((char *) packetBuffer);
}
The packets are very shorts, about 40 bytes each.
How would you improve my code?