Arduino Mega ENC28J60以太网模块直接连接到PC,以接收/发送UDP

arduino mega + ENC28J60 ethernet module direct connection to PC to receive/send UDP

本文关键字:UDP 发送 PC 连接 ENC28J60 Mega 以太网 模块 Arduino      更新时间:2023-10-16

我想使用我的arduino mega(带有传感器盾牌)和enc28J60以太网模块(直接连接到我的PC)从飞行模拟器(X-Plane 11)发送和接收UDP,能够通过网络发送UDP)。

网络模块:http://www.ebay.de/itm/281353516180?__trksid=p2057872.m2749.l2749.l2649&spagename = stspagename = strk = strk;

我无法将接收到的UDP数据写入串行显示器。我什至不确定我真的会收到UDP。

,以太网模块和PC之间的连接似乎很好,因为以太网模块的i/o的绿色LED永久打开,一旦我启动飞行SIM卡并从那里发送UDP,黄色的一个灯就会闪烁。

我尝试了标准以太网电缆和跨界。

我已经尝试了两种将ENC28J60以太网模块连接到Arduino Mega Sensor Shield的方法。


  1. Arduino Uno的标准布线

    • enc28j60 to arduino pin 12
    • enc28j60 si至arduino pin 11
    • enc28j60 sck至arduino pin 13
    • enc28j60 cs到arduino pin 10
    • enc28j60 vcc到arduino 3v3 PIN
    • enc28j60 gnd to arduino gnd pin

  1. 推荐Arduino Mega

    的接线

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

    • gnd to gnd
    • 3.3至3.3V
    • pin50
    • SI至PIN51
    • SCK至PIN52
    • CS到PIN53

我也尝试了几个libs:

  1. EtherCard:建议将CS PIN设置为库文件中的53,我做到了。再加上一个人应该在草图中使用此代码(未编译。错误是与sizeofEthernet::buffer结合使用)

    ether.begin(sizeof Ethernet::buffer, mac, 53)
    

  2. uipethernet(我想我可以在这里使用标准布线,因为据说该lib使用标准以太网屏蔽设置?)

没有组合使在串行显示器中输出任何输出是可能的。

我尝试过的草图之一是:

#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->以太网模块和
    以太网模块 -> arduino?

  2. 我是否需要在草图中设置二手arduino销子,还是lib这样做?

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

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

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

和Arduino代码,(ether.udpServerListenOnPort(&udpSerialPrint, 1337);

您更改所需的端口号。" udpserialprint"是一个回调函数。" 1337"是端口号。如果存在UDP数据,则程序调用此功能。