njh / EtherCard

EtherCard is an IPv4 driver for the ENC28J60 chip, compatible with Arduino IDE

Home Page:https://www.aelius.com/njh/ethercard/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UDP not send packet

Avenitos opened this issue · comments

Hello!

sendUdp() function not send broadcast packets :(
in Wireshark no packets.

UIP library worked correctly!

#include <EtherCard.h>

const byte myMac[] PROGMEM = { 0x70, 0x69, 0x69, 0x2D, 0x30, 0x31 };
byte Ethernet::buffer[350];                          // Buffer must be 350 for DHCP to work

void setup() {
  Serial.begin(57600);
  Serial.println(F("\n[EtherCard UDP send]"));

  if (ether.begin(sizeof Ethernet::buffer, myMac, SS) == 0)
    Serial.println(F("Failed to access Ethernet controller"));

  static byte myip[] = { 172,16,1,147 };
  static byte gwip[] = { 172,16,1,1 };
  static byte dnsip[] = { 8,8,8,8 };
  static byte mask[] = { 255,255,255,0 };

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

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);

  char payload[] = "My UDP message";
  uint8_t nSourcePort = 6454;
  uint8_t nDestinationPort = 6454;
  uint8_t broadcast[4] = { 255,255,255,255};
  
//ether.client_arp_whohas_blocking(ipDestinationAddress);
//delay(10);
//ether.sendUdp_mac(payload, sizeof(payload), nSourcePort, ipDestinationAddress, ether.returned_mac, nDestinationPort);

  ether.sendUdp(payload, sizeof(payload), nSourcePort, broadcast, nDestinationPort);

  Serial.println("UDP sent.");  
}

void loop() {
  ether.packetLoop(ether.packetReceive());
}

I did not immediately understand why the ntp demo works, but sending to my local server does not work. It turns out that any sending within the local segment does not work, sending via the gateway to the Internet address works normally. Why is that?

It is probably something to do with the code that chooses the Layer 2 MAC address to send to.
When sending to the Internet, EtherCard just needs the use the routers MAC address. When sending to the local subnet, it needs to find and use the right address. Although for broadcast this should just be FF:FF:FF:FF:FF:FF.

Have you tried sending to your subnet's broadcast address ? 172.16.1.255?

See also #59

 uint8_t nSourcePort = 6454;
  uint8_t nDestinationPort = 6454;

is the same as byte so you're not able to set port number larger than 255