【问题标题】:Microduino ENC28J60 Ethernet Module Arduino compatible, UDP Send not workingMicroduino ENC28J60 以太网模块 Arduino 兼容,UDP 发送不工作
【发布时间】:2013-09-06 01:40:08
【问题描述】:

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

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

我接收消息OK,但是回调方法中的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++ 中使用的左箭头和右箭头符号。

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

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

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

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

https://github.com/jcw/ethercard

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

【问题讨论】:

  • 这是个好问题。
  • 谢谢,但我还是不能让它工作

标签: c udp arduino send


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    老问题,但为了后代,这里是一个带有各种解决方法的答案:

    截至 2016 年 10 月,我通过 Ethercard 库确认了您的观察。问题是,如果目标位于本地子网上,则库必须对属于目标 IP 地址的 MAC 进行 ARP 查找——它不会不。您可以通过运行 Wireshark 并查看 ENC28J60 传输的 UDP 数据包来确认这一点。展开“Ethernet II”信息,您将看到目标 MAC 为 00:00:00:00:00:00。

    一种可能的解决方法是传输到本地子网的广播地址。然后,无论目标 MAC 地址如何,您的本地 UDP 服务器都会接收数据包。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,在输入正确的 IP 后发现我的网关 IP 地址错误,我的代码可以正常工作。 如果所有参数都正确,则以下函数可以正常工作,我也使用 java udp 客户端进行了检查

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

      【讨论】:

      • 我认为如果目标 IP 位于不同的子网上,您的解决方案将有效。当源和目标位于同一子网上时,OP 似乎存在问题。请参阅我对这个问题的回答。
      【解决方案4】:

      我有同样的问题,我可以发送但我无法接收。

      我认为你上面的代码很好。

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

      解决办法是:

      1. 要对其进行测试,请在计算机(IP:192、168、0、9)和 Arduino(IP:192、168、0、201)之间使用交叉 UTP 电缆梨对梨
      2. 使其与 arduino IP 网关和计算机 IP 目标相同
      3. ether.staticSetup(myip, gwip); “更改为” ether.staticSetup(myip, ipDestination);

      所以IP配置是这样的

      电脑:

      IP       : 192, 168, 0, 9  
      
      Gateway  : (none)           
      
      DNS      : (none)           
      
      Port open:8888               
      

      阿杜诺:

      IP       : 192,168,0,201
      
      Gateway  : 192, 168, 0, 9              
      
      DNS      : (none)     
      
      Port open:8888               
      

      【讨论】:

        【解决方案5】:

        问题似乎是套路:

          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);
        

        似乎在工作。

        sendUdp 的原型是:

        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,然后它会尝试在浏览器中显示它?如果是,则扩展 msg 使其符合,并增加消息长度。

        【讨论】:

        • 我有同样的问题(与 ether.sendUdp)。我在 Wireshark 中看到了 UDP 数据包,但目标主机没有收到它。老实说,我在数据包帧中没有看到任何问题......除了可能,ip id 是 0
        • 嗨,我仍然没有关于此事的信息。我相信其他东西而不是代码有错误。让我知道你的想法。谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多