GwakSeongJong
Member
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
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?
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);
}
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?