Why do I need to stop and begin UDP on every loop?

Hi!

Could anyone explain me why do I need to stop and begin UDP on every loop?
With the code below, I manage to receive only one message (osc actually, if that matters). I’ve tried to use udp.flush() also the message is read, but it didn’t help.
If I restart and stop udp on every loop, things are going fine. I’d like to safe some time…

I’m using Arduino Nano and ENC28j60 network chip.

Thank you!

Toni.

#include <UIPEthernet.h>

EthernetUDP udp;

void setup() {
  
  Serial.begin(115200);
  
  byte mac[] = { 0x1C, 0xE1, 0xCE, 0xBA, 0xBE, 0xEE };
  IPAddress ip(192, 168, 1, 196);

  Ethernet.begin(mac, ip);
  udp.begin(1337);
  
}

void loop() {
  
  //udp.begin(1337);
  
  int size;
    
  if ((size = udp.parsePacket()) > 0) {
    while(size--) {
      
      Serial.print(udp.read());   
      
    }
  udp.flush();
  }
  
  //udp.stop(); 
  
}

do you use the latest version of UIPEthernet? could you try EthernetENC?

is the next message from the same IP and port?

Yes, they are from the same address/port (Qlab on macbook).

I'll check the UIPEthernet, I think it's latest though..

I can't remember why I ended up with UIPEthernet some year ago, maybe because it was the only one I learned to use.. :slight_smile:

Toni.

I tested your sketch and I remmebered. in UIPEthernet the UDP listener after first message sets the IP and port and then filters the messages for this IP and port. It is a misunderstanding of UIPUDP on how uip works. I fixed this for EthernetENC, but it would be a breaking change for UIPEthernet library.

EthernetENC is new ( but based on the same old library)

hmmmm…

Did I get this right, I just can include the EthernetENC.h instead of UIPEthernet?

However, this didn’t work also…

//#include <UIPEthernet.h>
#include <EthernetENC.h>

EthernetUDP udp;

void setup() {
  
  Serial.begin(115200);
  
  byte mac[] = { 0x1C, 0xE1, 0xCE, 0xBA, 0xBE, 0xEE };
  IPAddress ip(192, 168, 1, 196);

  Ethernet.begin(mac, ip);
  udp.begin(1337);
  
}

void loop() {
  
  //udp.begin(1337);
  
  int size;
    
  if ((size = udp.parsePacket()) > 0) {
    while(size--) {
      
      Serial.println(udp.read());   
      
    }
  udp.flush();
  }
  
  //udp.stop(); 
  
}

sorry use the GitHub version of EthernetENC. the fix was after release

I downloaded the zip from here GitHub - jandrassy/EthernetENC: Ethernet library for ENC28J60. This is a modern version of the UIPEthernet library. EthernetENC library is compatible with all Arduino architectures with Arduino SPI library with transactions support. Only include EthernetENC.h instead of Ethernet.h
but still doesn't work.. :frowning:

But there's nothing wrong in my code?

OOH, My bad! I didn't remove the old library first! :slight_smile:

Now it's working! Thank you! :slight_smile:

Toni.