arduino mega + ENC28J60以太网模块直接连接到PC接收/发送UDP

时间:2017-03-02 22:52:23

标签: c++ networking arduino udp ethernet

我想使用我的Arduino Mega(带传感器屏蔽)和ENC28J60以太网模块(直接连接到我的PC)从飞行模拟器(X-Plane 11,它能够通过发送UDP发送和接收UDP)发送和接收UDP网络)。

网络模块:http://www.ebay.de/itm/281353516180?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

然而,我无法将收到的UDP数据写入串行监视器。 我甚至不确定我是否确实收到了UDP。

以太网模块和PC之间的连接似乎很好,因为以太网模块I / O的绿色LED是永久打开的,一旦我开始我的飞行模拟并发送黄色LED闪烁UDP来自那里。

我已尝试过标准以太网电缆和交叉电话。

根据2种不同的指南,我已经尝试了两种将ENC28J60以太网模块连接到Arduino Mega传感器屏蔽的方法。

  1. Arduino Uno的标准接线

    • Enc28j60 SO到Arduino pin 12
    • Enc28j60 SI到Arduino pin 11
    • Enc28j60 SCK到Arduino pin 13
    • Enc28j60 CS到Arduino pin 10
    • Enc28j60 VCC至Arduino 3V3引脚
    • Enc28j60 GND到Arduino Gnd pin
    1. Arduino Mega的推荐接线

      https://en.code-bude.net/2013/06/22/how-to-use-enc28j60-ethernet-shield-with-arduino-mega-2560/

      • GND至GND
      • 3.3至3.3V
      • SO to Pin50
      • SI to Pin51
      • SCK到Pin52
      • CS to Pin53
    2. 我也尝试了几个库:

      1. EtherCard:我建议在库文件中将cs引脚设置为53。另外,应该在草图中使用这行代码(没有编译。错误是使用sizeofEthernet::buffer结合使用)

        ether.begin(sizeof Ethernet::buffer, mac, 53)
        
      2. UIPEthernet(我假设我可以在这里使用标准接线,因为据说这个lib使用标准以太网屏蔽设置?)

      3. 这些组合都不能在串行监视器中输出任何内容。

        我尝试的其中一个草图如下:

        #include <Dhcp.h>
        #include <Dns.h>
        #include <ethernet_comp.h>
        #include <UIPClient.h>
        #include <UIPEthernet.h>
        #include <UIPServer.h>
        #include <UIPUdp.h>
        
        
        byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
        IPAddress ip(192, 168, 1, 6);    // local IP - address of my Arduino 
        unsigned int localPort = 49001;      // local port to listen - default X-Plane port 
        byte buf = 00;   // buffer for  UDP packet (BYTE, not char)
        
        EthernetUDP Udp; // An EthernetUDP instance to send and receive packets over UDP
        
        //-------------------------------------------------------------------------------
        
        void setup() 
        {
          Ethernet.begin(sizeof Ethernet::buffer, mac, 53)
          Ethernet.begin(mac,ip);   // start the Ethernet 
          Udp.begin(localPort);     //..and UDP:
        
          Serial.begin(9600);       // init serial port
        }
        void loop() {
          int packetSize = Udp.parsePacket();   //  Checks for the presence of a UDP packet, and returns its size
          if(packetSize)                        //  UDP packet was received and its size defined
          {
            Serial.println();
            Serial.print("Packet size: ");
            Serial.println(packetSize);         // Packet Size in bytes
        
         // When Udp.read used without parameters, it returns next char (byte in this case) :
        
              Serial.println("Xplane Data:");  
                for (int i =0; i/<packetSize; i++)
                {
                  buf = Udp.read();
                  Serial.print(buf);
                  Serial.print("-");
                }
          }
          delay(10);
        }
        

        所以我的问题是:

        1. 测试两个连接的最简单方法是什么:

          PC - &gt;以太网模块和
          以太网模块 - &gt; Arduino的?

        2. 我是否需要在草图中设置使用过的Arduino引脚,或者lib是否会这样做?

        3. 草图应该正常工作吗?

1 个答案:

答案 0 :(得分:0)

https://github.com/jcw/ethercard下载此内容。并编译并运行udplistener示例。

https://sourceforge.net/projects/sockettest/下载此程序(SocketTest)。 SocketTest是非常简单的程序。

在arduino代码中,(ether.udpServerListenOnPort(&udpSerialPrint, 1337);

您更改了所需的端口号。 “udpSerialPrint”是一个回调函数。“1337”是一个端口号。如果存在udp数据,程序将调用此函数。

相关问题