我正在使用新的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 [email protected]

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

===附加信息====
你好,
抱歉,包括:
包括以太卡.h
包括IPAddress.h
在c++中,它们似乎是用左箭头和右箭头符号从文本中删除的。
我记下你关于乙醚课的笔记。我使用了Ethercard文件夹中的一个名为“udpListener”的示例,对没有声明类感到困惑。我以为是在以太卡里完成的。
程序按原样工作,使用udpSerialPrint方法侦听并显示正确接收的udp数据包,因此侦听端工作。我想回显一些东西给udp包的发送者,它是udpSend不工作的。
我使用的盾牌和链接上的一样:http://hobbycomponents.com/index.php/microduino-enc28j60-ethernet-module-arduino-compatible.html
可通过同一网页找到兼容的库:
https://github.com/jcw/ethercard
我希望这能给你提供更多的信息。

最佳答案

问题似乎是例行公事:

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

从以下代码中回调:
void loop()
{
   //this must be called for ethercard functions to work.
   ether.packetLoop(ether.packetReceive());
}

它显示在PC的文本信息中,所以我们知道它被调用了。但是这句话:
ether.sendUdp(msg, sizeof msg, portMy, ipDestination, portDestination);

似乎不起作用。
sendup的原型是:
void EtherCard::sendUdp (char *data, byte datalen ,word sport, byte *dip, word dport)

第二个参数定义为字节?,但实际代码中的sizeof msg语句会生成什么呢?K&R说这是实现定义的。
因此,最初作为一项测试,我会尝试:
   ether.sendUdp(msg, 12, portMy, ipDestination, portDestination);

另外,要尝试的另一个更改是修改msg,使其具有用于html页面的正确标题。此消息将发送到本地计算机上的ip,然后该ip将尝试在浏览器中显示它??如果是,则展开msg使其一致,并增加消息长度。

关于c - Microduino ENC28J60以太网模块Arduino兼容,UDP发送不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18581804/

10-10 16:10