简体   繁体   中英

Microduino ENC28J60 Ethernet Module Arduino compatible, UDP Send not working

I am using the new Microduino ENC28J60 Ethernet Module (Arduino compatible).

I am using the udpListener sketch and want to echo a message back to the sender when a UDP packet arrives.

I am receiving messages OK, but the udpSend in the callback method will not work.

This works fine on the Arduino Uno with Ethernet shield

Can anyone help.

Thanks in Advance

Here is the Code:

// Demonstrates usage of the new udpServer feature.
//You can register the same function to multiple ports, and multiple functions to the same port.
//
// 2013-4-7 Brian Lee cybexsoft@hotmail.com

#include 
#include

#define STATIC 1 // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,0,201 };
// gateway ip address
static byte gwip[] = { 192,168,0,1 };

static byte ipDestination[] = {192, 168, 0, 9};

unsigned int portMy = 8888; 
unsigned int portDestination = 9000;

#endif

 // ethernet mac address - must be unique on your network
static byte mymac[] = { 0x70,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

char msg[] = {"Hello World"};

//callback that prints received packets to the serial port
void udpSerialPrint(word port, byte ip[4], const char *data, word len) {
IPAddress src(ip[0], ip[1], ip[2], ip[3]);
Serial.println(src);
Serial.println(port);
Serial.println(data);
Serial.println(len);

//I Added this to echo the packet <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
ether.sendUdp(msg, sizeof msg, portMy, ipDestination, portDestination);
Serial.println("UDP Sent !!");

}

void setup(){
Serial.begin(9600);
Serial.println("\n[backSoon]");

if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
#if STATIC
ether.staticSetup(myip, gwip);
Serial.println("Serial Started on FixedIP");
#else
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
#endif

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

//register udpSerialPrint() to port 1337
ether.udpServerListenOnPort(&udpSerialPrint, portMy);

//register udpSerialPrint() to port 42.
//ether.udpServerListenOnPort(&udpSerialPrint, 42);
}

void loop(){
//this must be called for ethercard functions to work.
ether.packetLoop(ether.packetReceive());
}

===== ADDITIONAL INFORMATION ====

Hi,

Sorry the includes are:

include EtherCard.h include IPAddress.h They seem to get removed from the text with the left arrow & right arrow symbol as used in c++.

I take your note about the ether class. I used an example from the Ethercard folder called "udpListener" and was puzzled about the class not being declared. I assumed in was done in the ethercard.h.

The program as is does work and listens to and displays correctly received udp packets with the udpSerialPrint method, so the Listening side works. I wanted to echo back something to the sender of the udp packet and it is udpSend does not work.

The shield that I am using is as on the link: http://hobbycomponents.com/index.php/microduino-enc28j60-ethernet-module-arduino-compatible.html

The compatible libraries are found through the same webpage:

https://github.com/jcw/ethercard

I hope this provides you with further info as necessary.

The question seems to be the routine:

  void udpSerialPrint(word port, byte ip[4], const char *data, word len) {//etc.

Gets called-back And specifically from this code:

void loop()
{
   //this must be called for ethercard functions to work.
   ether.packetLoop(ether.packetReceive());
}

And it displays at the PC text messages, so we know it is getting invoked. But the line:

ether.sendUdp(msg, sizeof msg, portMy, ipDestination, portDestination);

Does not appear to be working.

The prototype for sendUdp is:

void EtherCard::sendUdp (char *data, byte datalen ,word sport, byte *dip, word dport)

The second parameter is defined as a byte?, but what does the sizeof msg , statement in the actual code code generate? K&R says it is implementation defined.

So, initially as a test I would try:

   ether.sendUdp(msg, 12, portMy, ipDestination, portDestination);

Also, another change to try is to modify msg so that it has the correct headers used for html pages. This message is going to an ip on the local machine, which would then attempt to display it in a browser?? if so, then expand msg to make it conform, and increase the message length.

I had the same problem, and found my Gateway IP address was wrong after entering the correct IP my code works fine. the below function works fine if all the parameters are correct, also i checked using java udp client

ether.sendUdp(msg, sizeof msg, portMy, ipDestination, portDestination);

Old question, but for posterity here is an answer with a work-around of sorts:

I confirmed your observation with the Ethercard library as of October 2016. The problem is that, if the destination is on the local subnet, the library must do an ARP lookup of the MAC belonging to the destination IP address -- it doesn't. You can confirm that by running Wireshark and looking at a UDP packet transmitted by the ENC28J60. Expand the "Ethernet II" info and you will see that the destination MAC is 00:00:00:00:00:00.

A possible work around is to transmit to your local subnet's broadcast address. Your local UDP server will then receive the packets regardless of the destination MAC address.

遇到相同的问题:/您可以尝试使用makeUdpReply-它适用于您的示例:D

ether.makeUdpReply(msg, sizeof msg, portDestination);

I have the same problem, I can send but I cannot receive.

I think your code above is fine.

ether.sendUdp(msg, sizeof msg, portMy, ipDestination, portDestination);

the solution is :

  1. to test it, use cross UTP cable pear to pear, between computer (IP:192, 168, 0, 9) & Arduino (IP:192,168,0,201 )
  2. make it same arduino IP Gateway and Computer IP destination
  3. ether.staticSetup(myip, gwip); "change to" ether.staticSetup(myip, ipDestination);

So the IP configuration like this

Computer PC:

IP       : 192, 168, 0, 9  

Gateway  : (none)           

DNS      : (none)           

Port open:8888               

Arduino:

IP       : 192,168,0,201

Gateway  : 192, 168, 0, 9              

DNS      : (none)     

Port open:8888               

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM