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

标签 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 它们似乎已从文本中删除,如 C++ 中使用的左箭头和右箭头符号。

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

程序确实可以正常工作,并使用 udpSerialPrint 方法监听并显示正确接收到的 udp 数据包,因此监听端可以正常工作。我想回显一些东西给 udp 数据包的发送者,但 udpSend 不起作用。

我正在使用的防护罩如链接所示:http://hobbycomponents.com/index.php/microduino-enc28j60-ethernet-module-arduino-compatible.html

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

https://github.com/jcw/ethercard

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

最佳答案

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

接下来,如果您的代码用于在 Ethernet Shield 上工作,那么使用了哪些库,更具体地说,Ethernet Shield 上的芯片是什么?

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

从这个问题中给出的信息无法确定这一点。但是,这是我首先要看的东西。

附言我目前正在为 ENCJ2860 整理库,并且正在等待具有 ENCJ2860 的以太网屏蔽,因此我可以帮助您解决这个问题,或者将我的库发送给您(仍有待调试!!)。

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

相关文章:

c - 线程参数打印垃圾值

c# - 如何在 C# 中创建 UDP 服务器?

c++ - Arduino Uart 类和库

澄清在 Arduino 中使用结构和在 PROGMEM 中存储结构

c - float 的平方

c - 这4K有什么区别?

c - wprintw : In ncurses, 当写入与窗口宽度完全相同的换行符终止行时,打印两个换行符

linux -/proc/net/udp中字段的含义

java - 为在线多人游戏构建服务器

c++ - 使用 I2C 发送 0x00