TEENSY4.1 Ethernet UDP communication problem

TEENSY4.1 Ethernet UDP communication problem.


hello.
1. Usage model: TEENSY4.1
2. Ethernet Module: Ethernet Kit for Teensy 4.1
https://www.pjrc.com/store/ethernet_kit.html
3. Library: NativeEthernet

Code:
#include <NativeEthernet.h>
#include <NativeEthernetUdp.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned int localPort = 12007;      
IPAddress ip(192, 168, 0, 188);
IPAddress myDns(168, 126, 63, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress SERVER_IP(x, x, x, x);
unsigned int SERVERPort = 12000; 



char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; 
char ReplyBuffer[] = "acknowledged";        
EthernetUDP Udp;
//=========================================================================================//


//=========================================================================================//
void setup() {

  //---------------------------------------------------------------------//
  Serial.begin(115200);
  //---------------------------------------------------------------------//


  //---------------------------------------------------------------------//
  Ethernet.begin(mac, ip, myDns, gateway, subnet);
  //---------------------------------------------------------------------//


  //---------------------------------------------------------------------//
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {     delay(1);     }
  }
  if (Ethernet.linkStatus() == LinkOFF) {   Serial.println("Ethernet cable is not connected.");  }
  //---------------------------------------------------------------------//
  
  //---------------------------------------------------------------------//
  Udp.begin(localPort);  // start UDP
  //---------------------------------------------------------------------//

  //---------------------------------------------------------------------//
//  Udp.beginPacket(SERVER_IP, SERVERPort);
//  Udp.write(ReplyBuffer); delay(100); Udp.write(ReplyBuffer);
//  Udp.endPacket();
  //---------------------------------------------------------------------//

  //---------------------------------------------------------------------//
  Serial.println("SETUP OK!");
  //---------------------------------------------------------------------//

  
}






void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  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);


   
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    String stringOne = "A string dfdsf123";
    char Buf[30];
    stringOne.toCharArray(Buf, 30);
    Udp.write(Buf); 
    Udp.endPacket();
    delay(5);
    
  }
  delay(10);
}


fig.jpg

If you look at the photo,
1. When sending, there is a 30% chance of success.
2. Receiving has a 99.9% chance of success.



Why doesn't it arrive well when sending udp packets?
'udp.write' does not work well. Why?
 
Here's a modified version to work with QNEthernet. I'm curious how it works for you. I took the liberty of modifying the structure to be closer to how I would do it.

Code:
#include <QNEthernet.h>

using namespace qindesign::network;

IPAddress ip(192, 168, 0, 188);
IPAddress myDns(168, 126, 63, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

constexpr uint16_t kLocalPort = 12007;      

EthernetUDP udp;
constexpr int kPacketBufSize = Ethernet.mtu() - 8 - 20;
uint8_t packetBuf[kPacketBufSize];

void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 4000) {
    // Wait for Serial or a timeout
  }
  stdPrint = &Serial;  // Make printf work, a QNEthernet feature

  // Print the MAC address
  uint8_t mac[6];
  Ethernet.macAddress(mac);
  printf("MAC = %02x:%02x:%02x:%02x:%02x:%02x\n",
         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

  // Listen for link changes
  Ethernet.onLinkState([](bool state) {
    printf("[Ethernet] Link %s\n", state ? "ON" : "OFF");
  });

  // Start Ethernet
  if (!Ethernet.begin(ip, gateway, subnet)) {
    printf("Error starting Ethernet!\n");
  } else {
    Ethernet.setDNSServerIP(myDns);
    printf("Ethernet started\n");
  }

  // Start listening on our UDP port
  udp.begin(kLocalPort);
}

void loop() {
  int packetSize = udp.parsePacket();
  if (packetSize > 0) {
    IPAddress remoteIP = udp.remoteIP();
    printf("Received: size=%d remote=%u.%u.%u.%u:%u\n",
           packetSize,
           remoteIP[0], remoteIP[1], remoteIP[2], remoteIP[3],
           udp.remotePort());

    // Print the contents
    // Because IP reassembly can happen, it's possible for the packet
    // to be larger than our buffer, so check to make sure we haven't
    // received more than the buffer size. Alternatively, we could
    // keep reading until there's no more data to read.
    // Also note that the contents may or may not be a valid string;
    // that's why this prints each byte instead of printing the entire
    // thing as a string
    printf("Contents: ");
    do {
      int read = udp.read(packetBuf, min(packetSize, kPacketBufSize));
      for (int i = 0; i < read; i++) {
        printf(" %02x", packetBuf[i]);
      }
      packetSize -= read;
    } while (packetSize > 0);
    printf("\n");

    // Send a reply if we've received a packet
    const char kReply[] = "A string dfdsf123";
    udp.beginPacket(remoteIP, udp.remotePort());
    udp.write(kReply);
    udp.endPacket();
  }
  // No need for delays
}
 
Back
Top