Microduino ENC28J60以太网模块(Arduino兼容)UDP发送不工作

时间:2013-08-23 16:25:55

标签: c udp arduino

我正在使用新的Microduino ENC28J60以太网模块(兼容Arduino)。

我正在使用udpListener草图,并希望在UDP数据包到达时将消息回送给发件人。

我收到的消息没问题,但回调方法中的udpSend不起作用。

这适用于带有以太网屏蔽的Arduino Uno

任何人都可以提供帮助。

先谢谢

以下是代码:

// 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());
}

=====其他信息====

您好,

对不起,包括:

包括EtherCard.h 包括IPAddress.h 他们似乎被左箭头&amp;在c ++中使用的右箭头符号。

我记下关于以太类的说明。我使用了名为“udpListener”的Ethercard文件夹中的一个例子,并对未被声明的类感到困惑。我假设是在ethercard.h中完成的。

程序正常工作并使用udpSerialPrint方法监听并显示正确接收的udp数据包,因此Listening端正常工作。我想向udp数据包的发送者回复一些内容,而udpSend不起作用。

我使用的盾牌如链接:http://hobbycomponents.com/index.php/microduino-enc28j60-ethernet-module-arduino-compatible.html

兼容的库可以通过同一个网页找到:

https://github.com/jcw/ethercard

我希望这可以在必要时为您提供更多信息。

1 个答案:

答案 0 :(得分:1)

首先,发布的代码似乎并不完整。有几个不完整的#include,我也看不到定义ether类的位置。

接下来,如果您的代码用于使用Ethernet Shield,那么使用了哪些库,更具体地说是以太网屏蔽上的芯片?

ENCJ2860是“自己的”芯片。也就是说,控制ENCJ2860所需的库可能与以太网屏蔽上使用的库不同,当且仅当以太网屏蔽上的芯片 ENCJ2860时。

根据此问题中给出的信息无法确定这一点。但是,这是我要看的第一件事。

P.S。我正在努力修改ENCJ2860的库,并且正在等待具有ENCJ2860的以太网屏蔽,所以我可以帮助你,或者发送你的libs(仍然需要调试!!)。