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

C++ arduino mega&x2B;ENC28J60以太网模块直接连接到PC以接收/发送UDP,c++,networking,arduino,udp,ethernet,C++,Networking,Arduino,Udp,Ethernet,我想使用我的Arduino Mega(带传感器屏蔽)和ENC28J60以太网模块(直接连接到我的电脑)从飞行模拟器(X-Plane 11,能够通过网络发送UDP)发送和接收UDP 网络模块: 但是,我无法将收到的UDP数据写入串行监视器。 我甚至不确定我是否真的收到UDP 以太网模块和PC之间的连接似乎很好,因为以太网模块I/O的绿色LED一直亮着,而黄色LED在我启动飞行sim卡并从那里发送UDP时闪烁 我已经试过了,标准以太网电缆和交叉线 根据两个不同的指南,我已经尝试了两种将ENC28J6

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

网络模块:

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

以太网模块和PC之间的连接似乎很好,因为以太网模块I/O的绿色LED一直亮着,而黄色LED在我启动飞行sim卡并从那里发送UDP时闪烁

我已经试过了,标准以太网电缆和交叉线

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


  • Arduino Uno的标准接线

    • Enc28j60 SO至Arduino引脚12
    • Enc28j60 SI至Arduino引脚11
    • Enc28j60 SCK至Arduino引脚13
    • Enc28j60 CS至Arduino针脚10
    • Enc28j60 VCC至Arduino 3V3引脚
    • Enc28j60接地至Arduino接地引脚

  • Arduino Mega的推荐接线

    • 接地到接地
    • 3.3至3.3V
    • 所以,我想打50分
    • SI至Pin51
    • SCK至Pin52
    • CS至Pin53
  • 我还尝试了几种LIB:

  • 以太卡:建议在库文件中将cs pin设置为53,我就是这么做的。另外,应该在草图中使用这行代码(未编译。错误在于将
    sizeof
    Ethernet::buffer
    结合使用)

  • 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);
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    字节mac[]={0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
    ip地址ip(192,168,1,6);//本地IP-我的Arduino的地址
    unsigned int localPort=49001;//要侦听的本地端口-默认X平面端口
    字节buf=00;//UDP数据包缓冲区(字节,非字符)
    以太网Udp;//用于通过UDP发送和接收数据包的EthernetUDP实例
    //-------------------------------------------------------------------------------
    无效设置()
    {
    Ethernet.begin(以太网的大小::缓冲区,mac,53)
    Ethernet.begin(mac,ip);//启动以太网
    Udp.begin(localPort);/…和Udp:
    Serial.begin(9600);//初始化串行端口
    }
    void循环(){
    int packetSize=Udp.parsePacket();//检查是否存在Udp数据包,并返回其大小
    if(packetSize)//接收到UDP数据包并定义了其大小
    {
    Serial.println();
    串行打印(“数据包大小:”);
    Serial.println(packetSize);//数据包大小(字节)
    //如果使用Udp.read时不带参数,则返回下一个字符(本例中为字节):
    Serial.println(“Xplane数据:”);
    用于(int i=0;i/以太网模块和
    以太网模块->Arduino

  • 我需要在草图中设置使用过的Arduino管脚吗?还是库可以这样做

  • 草图应该正常工作吗

  • 下载这个,编译并运行udplistener示例

    下载这个程序(SocketTest)。SocketTest是一个非常简单的程序

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

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

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