Hi there,
I am currently trying to set up a udp system with my arduino due and my ethernet Poe shield. I am using a packet generator in c# to send the arduino packets and using 2 wiresharks captures to monitor incoming and outgoing packets
I currently have a struct which i would like to send constantly (this will be dynamic in the future), and when i receive a pack i want to send a similar packet back. However my code seems to run correctly for about 5000 packets received then stop, and i will run it again and this time it will run for 10000 packets received then stop. I am at loss at what would cause this inconsistency, memory leak? any optimization of the code is very much appreciated.
#include <Ethernet.h>
#include <SPI.h>
EthernetUDP udp;
EthernetClient client;
typedef struct __attribute__((__packed__)) sent {
unsigned short major;
unsigned short minor;
unsigned char messageType;
unsigned char channel;
unsigned short payLoadLength;
unsigned long ID;
double dateTime;
byte sourceIP1;
byte sourceIP2;
byte sourceIP3;
byte sourceIP4;
signed short zeroes1;
signed short zeroes2;
} sent;
unsigned short majorVersion = 2;
unsigned short minorVersion = 0;
unsigned long IDCount = 1;
unsigned long IDCountPacket = 1;
double messageTimeStamp = 0;
double messageTimeStamp1 = 0;
byte timeStampBuff[8];
sent* sentData = (sent*) malloc(sizeof(sent));//allocate memory
void setup() {
Serial.begin(9600);
uint8_t mac[6] = {0x90, 0xA2, 0xDA, 0x00, 0xFA, 0x7D};
Ethernet.begin(mac);
int success = udp.begin(18005);
Serial.print("initialize: ");
Serial.println(success ? "success" : "failed");
Serial.print("localIP: ");
Serial.println(Ethernet.localIP());
Serial.print("subnetMask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("gatewayIP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("dnsServerIP: ");
Serial.println(Ethernet.dnsServerIP());
sentData->major = majorVersion;
sentData->minor = minorVersion;
sentData->messageType = 2;
sentData->channel = 3;
sentData->payLoadLength = 36;
sentData->ID = 1;
sentData->dateTime = messageTimeStamp;
sentData->sourceIP1 = Ethernet.localIP()[0];
sentData->sourceIP2 = Ethernet.localIP()[1];
sentData->sourceIP3 = Ethernet.localIP()[2];
sentData->sourceIP4 = Ethernet.localIP()[3];
sentData->zeroes1 = 00;
sentData->zeroes2 = 00;
}
void acknowledgementCreator ()
{
sentData->ID = IDCount;
IDCount++;//increment the id count
udp.beginPacket(IPAddress(10, 234, 186, 18), 18006);
udp.write((byte*) sentData, sizeof(sent));
udp.endPacket();
}
void packetCreator ()
{
sentData->ID = IDCountPacket;
IDCountPacket++;//increment the id count
udp.beginPacket(IPAddress(10, 234, 186, 18), 5001);
udp.write((byte*) sentData, sizeof(sent));
udp.endPacket();
}
void loop()
{
//check for new udp-packet:
int size = 0;
size = udp.parsePacket();
if (size > 0)
{
char* msg = (char*)malloc(size);//alocate memory
int len = udp.read(msg, size);
msg[len] = 0;
udp.flush();
acknowledgementCreator();
free(msg);
}
packetCreator();
}