njh / EtherCard

EtherCard is an IPv4 driver for the ENC28J60 chip, compatible with Arduino IDE
https://www.aelius.com/njh/ethercard/
GNU General Public License v2.0
1.03k stars 455 forks source link

UDP packet is not sent - NOT STATIC IP #422

Open DexasElectronics opened 3 years ago

DexasElectronics commented 3 years ago

Hello. Im trying to send some test strings using example sketch provided in the description of the library. I have checked all the mentioned issues, but they all seem to share same thing - static ip. I told that also, while using static ip i cannot neither send single packet to specific adress, neither broadcast. When using DHCP, i can broadcast, but cannot send packet to single adress (packet never arrives).

Tried using code provided in Add ARP cache #351, but that gives error about "no member named 'clientResolveIp'", from what i see those functions are deleted from the latest version, not sure why.

Snippets from code i am using:

static byte myip[] = { 192,168,1,200 };
static byte gwip[] = { 192,168,1,254 };
static byte dnsip[] = { 8,8,8,8 };
static byte mask[] = { 255,255,255,0 };
static byte mymac[] = { 0x70,0x69,0x69,0x2D,0x30,0x31 };

ether.staticSetup(myip, gwip, 0, mask);

char payload[] = "My UDP message";
uint8_t nSourcePort = 11;
uint8_t nDestinationPort = 11;
uint8_t ipDestinationAddress[IP_LEN];
ether.parseIp(ipDestinationAddress, "192.168.1.103");
ether.sendUdp(payload, sizeof(payload), nSourcePort, ipDestinationAddress, nDestinationPort);

void loop() // run over and over { ether.packetLoop(ether.packetReceive()); }

Tried changing ports, etc, both static and dynamic ip, no luck in any case.

daonguyen207 commented 3 years ago

The library was not able to get the destination MAC address correctly, so the computer ignored the UDP packet. Open the file tcpip.cpp and locate the function void EtherCard::udpPrepare (uint16_t sport, const uint8_t *dip, uint16_t dport) on line 376 Please replace destmacaddr with gwmacaddr

if(is_lan(myip, dip)) { //setMACandIPs(destmacaddr, dip); setMACandIPs(gwmacaddr, dip); }