My setup is I have an Arduino sending a data structure over an Ethernet cable using UDP to another Arduino. The data structure used on both ends is the same. However, weirdly the first 3 values of the data structure are showing as correct at the received end but the others are shown as having a value of 0.00. The transmitted bytes are shown to equal the received bytes at a total of 68 bytes (17 floats).
I know the issue isn't on the transmission end as the data is being received correctly on a Raspberry Pi.
My transmitting code is as follows:
#include "Data.h"
#include <Ethernet2.h>
#include <EthernetUdp2.h>
byte muMac[] = {0x90, 0xA2, 0xDA, 0x10, 0x5F, 0x92};
IPAddress muIp(192, 168, 0, 30); //Local IP address
IPAddress broadIp(255,255,255,255); //Broadcast address, sends to all nodes (Relay and Station Controller)
unsigned int muPort = 5000; //Local port to listen on
unsigned int broadPort = 8888; //Destination port
EthernetUDP Udp;
Data dataOut;
COMPLEX Impedance;
void Comms_Setup(){
// start the Ethernet and UDP:
Ethernet.begin(muMac, muIp);
Udp.begin(muPort);
int counter=0;
}
/*Send Following:
* 1.Peak voltage/current
* 2.Phase
* 3.Frequency
* Each sample should be timestamped
*/
void send_packet(float idCount)
{
dataOut.ID = idCount;
dataOut.CTpvalue=I1.peak;
dataOut.CTpphase=I1.phase;
dataOut.CT1value=I2.peak;
dataOut.CT1phase=I2.phase;
dataOut.CT2value=I3.peak;
dataOut.CT2phase=I3.phase;
dataOut.CT3value=I4.peak;
dataOut.CT3phase=I4.phase;
dataOut.CT4value=I5.peak;
dataOut.CT4phase=I5.phase;
dataOut.VT1value=V1.peak;
dataOut.VT1phase=V1.phase;
dataOut.VT2value=V2.peak;
dataOut.VT2phase=V2.phase;
dataOut.VT3value=V3.peak;
dataOut.VT3phase=V3.phase;
Udp.beginPacket(broadIp, broadPort); //Begin packet to send to a destination IP and Port address
Udp.write( (byte *)&dataOut,sizeof dataOut); //Write packet which contains data structure
Serial.println("Bytes sent");
Serial.println(sizeof dataOut);
Udp.endPacket(); //Close Packet and send
delay(100);
}
and my Receiving code:
#include <SPI.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>
#include "SampV.h"
#include "Settings.h"
EthernetUDP Udp;
SampV *sampIn; //Instance of sampled value structure
Settings *settingIn; //Instance of settings structure
char DATA_buf[500]; //Temporary buffer to store recieved samples
byte relayMac[] = {0x90, 0xA2, 0xDA, 0x10, 0x5E, 0xD7};
IPAddress relayIp(169, 254, 255, 155); //IP address of Relay
IPAddress piIp(192, 168, 0, 15); //IP address of Pi
unsigned int relayPort = 8888; //Local port to listen on
unsigned int piPort = 5005; //Destination port
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //Buffer for settings data
/*Start Ethernet and UDP*/
void Comms_Setup(){
Ethernet.begin(relayMac, relayIp);
Udp.begin(relayPort);
}
/*Recieve packet from Merging unit*/
void recieve_samples() {
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
Serial.println("Contents:");
for (int i =0; i < 4; i++)
{
Serial.print(remote[i]);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(DATA_buf,UDP_TX_PACKET_MAX_SIZE);
sampIn = (SampV*)DATA_buf;
Serial.print("ID");
Serial.println(sampIn->SampID);
Serial.print("------CTs-------");
delay(1000);
Serial.print(sampIn->CTpvalue);
Serial.print("--");
delay(100);
Serial.print(sampIn->CT1value);
Serial.print("--");
delay(100);
Serial.print(sampIn->CT2value);
Serial.print("--");
delay(100);
Serial.print(sampIn->CT3value);
Serial.print("--");
delay(100);
Serial.println(sampIn->CT4value);
Serial.print("------VTs-------");
Serial.print(sampIn->VT1value);
Serial.print("--");
delay(100);
Serial.print(sampIn->VT2value);
Serial.print("--");
delay(100);
Serial.println(sampIn->VT3value);
}
}
Structure at both ends:
typedef struct {
float SampID;
float CTpvalue;
float CTpphase;
float CT1value;
float CT1phase;
float CT2value;
float CT2phase;
float CT3value;
float CT3phase;
float CT4value;
float CT4phase;
float VT1value;
float VT1phase;
float VT2value;
float VT2phase;
float VT3value;
float VT3phase;
}
SampV;
The output at the received end when printing the data is:
Received packet of size 68
From Contents:
192.168.0.30, port 5000
ID37.00
------CTs-------1.87--3.47--2.24--0.00--0.00
------VTs-------0.00---0.00--0.00
Any help is greatly appreciated.