Arduino ethernet spamming 0xFF bytes along send/receive between two arduinos

I'm currently attempting to send and receive data between two arduino mega 2560 ETH boards. They are physically tethered to each other via a crossover link cord. I'm trying to make it so that one only needs to change the client_server variable to distinguish which board is a client and which is a server. Problem is, when receiving on the client side, I get tons of 0xFF bytes even if I limit the number of bytes printed per loop. Occasionally I'll get output that looks closer to what it should be. Server side works as intended, I've checked the data right as it is sent and its what it should be, but it seems to get scrambled when received by the client.

What I get:

Local IP address:2969741504
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
F1FFFFFF83900BE700F82000000
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

What it SHOULD print on every line: F1FFFFFF83900BE700F82000000
Here's my code:

#include <SPI.h>
#include <Ethernet3.h>

#define server_client 1

#define SS 10    //W5500 CS
#define RST 7    //W5500 RST
#define CS 4     //SD CS pin
#define BUF_SIZE 20


#if server_client
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
#else
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xFE, 0xEF, 0xED };
#endif

EthernetServer serverEth(80); // Declare server and it's port
EthernetClient client;

IPAddress ip(192, 168, 2, 8); // This device's IP address
IPAddress server(192, 168, 2, 177); // Device to connect to

union ethernetDataStruct{
   uint32_t value;
   byte bytes[4];
};

const uint32_t startOfPacket = 0xFFFFFFF1;

byte sendBuffer[BUF_SIZE]; // Writing to ethernet can only be done byte by byte
uint32_t packet[BUF_SIZE / 4]; // Structure of the packet to send

ethernetDataStruct ethData; 
void setup() {
  TCCR5B = (TCCR5B & 0xF8) | 0x01;
  Serial.begin(9600); // Set serial baud rate
  pinMode(7, OUTPUT );
  digitalWrite(7, HIGH);
  pinMode(SS, OUTPUT);
  pinMode(RST, OUTPUT);
  pinMode(CS, OUTPUT);
  digitalWrite(SS, LOW);
  digitalWrite(CS, HIGH);
  SPI.begin(); // Required for ethernet library
  delay(10);
  ethernetSetup();
}

void loop() {
  uint32_t x = 2435;
  uint32_t y = 1982;
  uint32_t z = 760;
  sendReceive(x,y,z);
  delay(1000);
}

void sendReceive(uint32_t x, uint32_t y, uint32_t z){

  memset(sendBuffer, 0, sizeof(sendBuffer));
  memset(packet, 0x00000000, sizeof(packet));
  memset(ethData.bytes,0,4); // Clear struct's memory
  ethData.value = 0x00000000; // Clear struct's memory
  boolean val = false;

  if(server_client){
    client = serverEth.available();
    val = (client) ? true : false;
  }
  else{
    val = (client.connect(server, 80)) ? true : false;
  }

  if(val){// Check if there's data being recieved from the client
    for(int i = 0; i < 20; i ++){Serial.print(client.read(),HEX);}
    Serial.println();
  }
  // Construct a small packet of data to send
  packet[0] = startOfPacket; //start of data 
  packet[1] = x;
  packet[2] = y;
  packet[3] = z;

  int j = 0;
  for(int i = 0; i < 4; i++){ // Populate sendBuffer using data from packet
    
    ethData.value = packet[i]; // Cycle through all values in packet

    for(int k = 0; k < 4; k++){ // Split 32bit values into 8bit and store into sendBuffer
      sendBuffer[j] = ethData.bytes[k]; // Feed sendBuffer byte by byte
      j++; // Keep track of where we are in sendBuffer
    }
  }

  if(server_client){serverEth.write(sendBuffer,BUF_SIZE);} // Send data to ethernet connected device
  else{client.write(sendBuffer,BUF_SIZE);}
  client.stop();
}

void ethernetSetup(){ // Starts up server or client
  Ethernet.begin(mac, server);
  Serial.println((String)"Local IP address:" + Ethernet.localIP()); // Print IP address to serial on bootup
  if(server_client){
    Serial.println("Starting server...");
    serverEth.begin();
  }
  else{client.connect(server, 80);}
}

client.read() returns -1 if there is no byte to read. -1 interpreted as unsigned hex is FF

Is there a reason why the client isn't receiving data from the server as well as the server is receiving from the client?

the MCU is faster than the network

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.