Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Teensy 4.0 Locks up sending udp

  1. #1

    Teensy 4.0 Locks up sending udp

    Teensy 4.0 with w5500 ethernet freezes when you call sendPacket using UDP:

    Code:
    #include <SPI.h>
    #include <Ethernet.h>
    #include <EthernetUdp.h>
    
    byte g_mac[] = {
      0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 42
    };
    IPAddress g_ip(10, 1, 10, 42);
    IPAddress g_clientIP(10, 1, 10, 10);
    unsigned int g_clientPort = 1337;
    EthernetUDP g_UDP;
    
    typedef struct
    {
      char startChar = 'd';
      byte packetNumber = 0;
      uint16_t frameNumber = 0;
      uint16_t dataSize;
      uint16_t dataOffset;
    } dataPacket;
    dataPacket g_header;
    
    byte data[1024] = {127};
    
    void setup() {
      // put your setup code here, to run once:
      while (!Serial)
      {
        delay(100);
      }
      Serial.begin(115200);
      Ethernet.begin(g_mac, g_ip);
      //Ethernet.setRetransmissionCount(0);
      //Ethernet.setRetransmissionTimeout(10);
      Serial.println(Ethernet.linkStatus());
      Serial.println(Ethernet.hardwareStatus());
    }
    
    void loop() {
      int next = millis() + 1000;
      int success = 0;
      // put your main code here, to run repeatedly:
      do
      {
        success = g_UDP.beginPacket(g_clientIP, g_clientPort);
    
         Serial.print(".");
    
        //beginPacket fails if remote ethaddr is unknown. In this case an
        //arp-request is send out first and beginPacket succeeds as soon
        //the arp-response is received.
      }
      while (!success && ((signed long)(millis() - next)) < 0);
      success = g_UDP.write((byte*)&g_header, sizeof(dataPacket));
    
      Serial.print("bytes written: ");
      Serial.println(success);
      success = g_UDP.write((byte*)data, 1024);
    
      Serial.print("bytes written: ");
      Serial.println(success);
    
      success = g_UDP.endPacket(); //<< never returns
      if (success)Serial.print("sent!");
    
      delay(1000);
    }

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,236
    at the end of setup add g_UDP.begin(8888);

    see https://www.arduino.cc/en/Reference/EthernetUDPBegin

    try/study one of the UDP examples in the Ethernet library

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •