Hang on udp.endPacket

Post here first, or if you can't find a relevant section!
Post Reply
hardyn
Posts: 8
Joined: Sun Mar 28, 2021 5:33 am

Hang on udp.endPacket

Post by hardyn »

Hello,

I am having the problem where attempting to use the w5500 ethernet module with a Nucleo STM32F303RE results in a hang/crash on the udp.endPacket command.

I am using the Ethernet library provided in the Arduino IDE. I have had this working working with a Bluepill in the past. I have searched and found similar threads but generally have been related to udp.begin problems, which I do not believe I have.

The UDP message exchange happens exactly once, the message is received and the response is transmitted, but hangs before udp.endPacket can return a value (which I have been sending out the serial port as a debug effort). It has definitely crashed as loop() is no longer looping.


Any help would be appreciated.

Code: Select all

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged\r\n";       // a string to send back

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

void setup() {
  // configure SPI
  //SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  
  // start the Ethernet and UDP:
  //Ethernet.init(D10); // CS pin
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);

  Serial.begin(115200);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();

  Serial.println("parsed Packet");
  
  if (packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Serial.print("begin");
    Serial.println( Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()) );
    Serial.print("write");
    Serial.println( Udp.write(ReplyBuffer) );
    Serial.print("end");
    Serial.println( Udp.endPacket() );
  }
  delay(1000);
}
Last edited by hardyn on Sun Mar 28, 2021 2:49 pm, edited 1 time in total.
User avatar
Juraj
Posts: 47
Joined: Fri Jan 03, 2020 7:47 pm
Answers: 1
Location: Slovakia
Contact:

Re: Hang on udp.endPacket

Post by Juraj »

use the Ethernet library.
hardyn
Posts: 8
Joined: Sun Mar 28, 2021 5:33 am

Re: Hang on udp.endPacket

Post by hardyn »

Same result, I have tried them both.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: Hang on udp.endPacket

Post by stevestrong »

If you are using the Ethernet library form Arduino, then it should not hang at all because the function socketSendUDP() - which is called in udp.endPacket() - has an integrated timeout condition, see here: https://github.com/arduino-libraries/Et ... dp.cpp#L83
and here: https://github.com/arduino-libraries/Et ... t.cpp#L519
hardyn
Posts: 8
Joined: Sun Mar 28, 2021 5:33 am

Re: Hang on udp.endPacket

Post by hardyn »

Thanks for the response... but I don't know what to say. It is. the serial.println I have at the top of the loop stops publishing. I have to take that as either the system has crashed or something is blocking. I will stick the pin on the scope in a minute to see what that pin is doing.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: Hang on udp.endPacket

Post by stevestrong »

Insert some debug prints into socketSendUDP function so that you know where exactly is hanging
Also, you can add some debug prints in the yield() function.

Btw, which core do you use?
Post Reply

Return to “General discussion”