[SOLVED ???] UDP.write always returns 0

Hello,

in my application the UPD.write functions always returns 0. I can check the assigned ip address with networking tools, mac address is right as well. Where is the bug?

Thanks for your help!
Oliver

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

byte mac[] = {  0x00, 0xAF, 0xFE, 0x04, 0xAF, 0xFE };

IPAddress src_ip(192, 168, 23, 177);
IPAddress dns_ip(192, 168, 23, 1);
IPAddress gw_ip(192, 168, 23, 1);
IPAddress mask(255, 255, 255, 0);

IPAddress dest(192,168,23,251);

unsigned int cmd_size = 13;

byte  cmd_go[]   = { 0x00, 0x00, 0x0b, 0x2f, 0x05, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 };
byte  cmd_stop[] = { 0x00, 0x00, 0x0b, 0x2f, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

EthernetUDP Udp;

void setup() {
  Serial.begin(9600);
  Serial.println("Hallo");

  Ethernet.begin(mac,src_ip, dns_ip, gw_ip, mask);
  bool r = Udp.begin(15730);
  Serial.println(r ? "UDP OK" : "UDP FALSE");
  
  pinMode(10, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(43, INPUT);
}

bool toggle = false;

void loop() {
  if(digitalRead(43) == HIGH){
      // fragment from testing, doesn't change anything... Udp.stop(); removed
    
      bool r = Udp.beginPacket( dest, 15731);
      Serial.println(r ? "UDP.begin = true" : "UDP.begin = false");
      
      int i = 0;
      if(r){
        i = Udp.write(toggle ? cmd_go : cmd_stop, cmd_size);
        Serial.print("UDP.write = "); Serial.println(i);
      }
      if(i > 0)
        Udp.endPacket();  
      digitalWrite(7, toggle ? HIGH : LOW);
    toggle = !toggle;
  }
  delay(500);
}

Don't use Udp.stop(). It releases the socket and resources required to send the UDP packet.

  if(digitalRead(43) == HIGH){
// remove this line
      Udp.stop();

http://arduino.cc/en/Reference/EthernetUDPStop

You're right. This is a fragment from testing around.

Even removing the call to stop doesn't change anything.

I corrected the code above as well ...

I just tested your code on a Mega 2560, and it shows this over and over:

UDP.begin = true UDP.write = 13 UDP.begin = true UDP.write = 13

Don't know what to tell you.

edit: I'm using IDE v1.0.5.

Thanks for checking.

I'm using 1.5.6-r2 with an Arduino Due...

I just checked with IDE v1.5.7 and my Mega and it still shows 13.

Do you have a SD card in the shield's slot? That can cause UDP problems if you do not disable the SD card in your setup. If so, power down the Due, remove the SD card, and power up again.

No, there is no SD card in the slot. To be on the save side I added code to disable the SD reader:

  pinMode(4,OUTPUT);
  digitalWrite(4, HIGH);

I get crazy, still not working.

It can't be a spi defect, since changes to the mac address are reflected in the network monitor tool...

Hi,

I got a new Arduino Mega today and - as expected - the code worked fine. Well, going back to the due I inserted the following code by error:

 pinMode(10, OUTPUT);
  digitalWrite(10, LOW);

And now it WORKS - I have no clue why it now works...

I thought setting the CS pin to LOW enables the SPI communication...??????

Oliver

I thought setting the CS pin to LOW enables the SPI communication...??????

It does. At least it is supposed to.