Receiving UDP from an external device

I'm trying to read a UDP signal from a device (an optic interrogator). The device has no interface apart from an on/off switch.
I tried connecting the ethernetport from the device to a W5100 ethernet shield mounted on an Arduino Mega 2560 Rev 3 with the following code:

#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>

int packetSize;

byte mac[] = { 0xAC, 0xDC, 0xDA, 0xDA, 0xE3, 0xE8};                   //mac address
IPAddress ip(169, 254, 211, 175);                                     
unsigned int localPort = 80;                                          //local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];                            //buffer for incoming packet

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  Udp.begin(localPort);
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  if(Ethernet.hardwareStatus() == EthernetNoHardware){
     Serial.println("Shield not found");

  }
}

void loop() {
  packetSize = Udp.parsePacket();
  Serial.println(packetSize);
  if(packetSize > 0){
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println(packetBuffer);
    delay(50);
  }
  else{;}
}

However, I just end up with packetSize = 0.

To be clear, the hardware setup is just an arduino Mega 2560 Rev3 connected through the usb cable to my pc. The W5100 shield is on the arduino. A RJ45 CAT5E cable connects the shield directly to the device.

Does anyone have an idea how to fix this?

What does the manual for your optic interrogator have to say about UDP? More specifically, how does it know what IP address to send the UDP packet to?

The manual says the ip of the optic interrogator should be 10.0.0.150 , and the receiving pc (ethernet shield in this case) should fit the subnet mask 255.255.255.0 , which means it could be anything between 10.0.0.0 and 10.0.0.255, except 10.0.0.150 .
That's what I got from the manual. But they also mentioned the IP could be changed through software (I have no record of that since it's a hand-me-down).

So if I were to change the ethernet shield's IP to one that fits the subnet mask, they should be able to communicate right?

The thing you need to find out is which IP the device is sending to. I'd suggest that you plug it into a switch or router on your local lan and use WireShark or the like in promiscuous mode on a PC to view the packet traffic. Then set the IP of the Arduino to whatever it's sending to.

It sounds like there's some software that comes with the device which allows you to configure it. Hopefully, you can get it working without it.

ikdemartijn:
The manual says the ip of the optic interrogator should be 10.0.0.150 , and the receiving pc (ethernet shield in this case) should fit the subnet mask 255.255.255.0 , which means it could be anything between 10.0.0.0 and 10.0.0.255, except 10.0.0.150 .
That's what I got from the manual. But they also mentioned the IP could be changed through software (I have no record of that since it's a hand-me-down).

So if I were to change the ethernet shield's IP to one that fits the subnet mask, they should be able to communicate right?

Yes, you need the ethernet shield to have an IP address from the same range, so between 10.0.0.1 and 10.0.0.254, excluding 10.0.0.150.

So i managed to get my hands on the software for the pc and found out the IP is indeed 10.0.0.150 . I set my shield to the same IP as my pc's (10.0.0.37). Yet I still can't get my arduino to give me the size of the packets it's receiving, or wether it's recieving at all. Any ideas on where I might go wrong?

#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>

int packetSize;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE};                   //mac address
IPAddress ip{10,0,0,37};                                              //ip address of arduino
byte subnet[] = {255, 255, 255, 0};                                   //subnet mask
byte gateway[] = {10, 0, 0 ,1};                                       //gateway                              
unsigned int LocalPort = 53 ;                                         //local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];                            //buffer om het inkomende pakket op te vangen

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP UDP;

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip, subnet, gateway);
  UDP.begin(LocalPort);
  delay(1500);
  if(Ethernet.hardwareStatus() == EthernetNoHardware){
     Serial.println("Shield not found");
  }
}

void loop() {
  packetSize = UDP.parsePacket();
  delay(3000);
  
  Serial.println(packetSize);
  delay(500);
  if(packetSize > 0){
    UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println(packetBuffer);
    delay(50);
  }
  else{;}
}

I suspect that the software on the PC will let you configure the target IP. I don't see any reason for it to be the IP of your PC though. It's more likely to be that of whatever machine was receiving the udp packets when it was in service, I.e. Before you got your hands on it.

I managed to get my hands on the software for the pc and found out the IP is indeed 10.0.0.150 . I set my shield to the same IP as my pc's (10.0.0.37).

I'm not clear what you have done here. The way I read the above is that you have set the IP address of the Arduino to the same IP address as you PC, is that correct? That will never work.

I'm going to struggle to help as this is at the limit of what I know about, particularly as you are using a device I know nothing about. Here's what I think:

Your 'Optic Interrogator' (I have not the slightest clue what that is) has the IP address 10.0.0.150.

Your PC has the IP address 10.0.0.37.

You have set the IP address of your Arduino to 10.0.0.37. You can't do that (Well, OK, you CAN do that, but 2 devices on the same network with the same IP address is asking for trouble).

How do you know what IP address the Optic Interrogator is going to send the packet to? Or maybe it sends packet to the same IP address that it receives commands from, so you send it a packet asking it something and it replies. That would seem like a reasonable possibility, but, as far as I can see, your code is only listening for packets from the OI, not sending it anything and asking for a reply (unless I've misread your code, always very possible with me!).

If this OI is just sending out packets unbidden then you need to know what IP address it is sending to. If you can't get into it's configuration then you need Wireshark and a switch with port mirroring so you can see the packets on your network.

I think! :o
Unless someone else on here has a better idea (I'm sure someone does!)

If I missed something obvious, like something you said that I misread, then you have my apologies.