【发布时间】:2017-03-02 22:52:23
【问题描述】:
我想使用我的 Arduino Mega(带有传感器屏蔽)和 ENC28J60 以太网模块(直接连接到我的 PC)从飞行模拟器(X-Plane 11,能够通过网络)。
网络模块:http://www.ebay.de/itm/281353516180?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
但是,我无法将接收到的 UDP 数据写入串行监视器。 我什至不确定我是否真的收到了 UDP。
以太网模块和 PC 之间的连接似乎很好,因为以太网模块的 I/O 的绿色 LED 一直亮着,而当我启动飞行 sim 并从那里发送 UDP 时,黄色的 LED 会闪烁。
标准以太网电缆和交叉线我都试过了。
根据 2 种不同的指南,我尝试了 2 种将 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 GND 到 Arduino Gnd 引脚
-
Arduino Mega 的推荐接线
https://en.code-bude.net/2013/06/22/how-to-use-enc28j60-ethernet-shield-with-arduino-mega-2560/
- 地到地
- 3.3 至 3.3V
- SO 到 Pin50
- SI 到 Pin51
- SCK 转 Pin52
- CS 转 Pin53
我也尝试了几个库:
-
EtherCard:建议在库文件中将cs pin设置为53,我也是这样做的。另外一个应该在草图中使用这行代码(没有编译。错误是
sizeof与Ethernet::buffer结合使用)ether.begin(sizeof Ethernet::buffer, mac, 53) UIPEthernet(我假设我可以在这里使用标准接线,因为据说这个库使用标准以太网屏蔽设置?)
没有一种组合可以在串行监视器中进行任何输出。
我尝试过的草图之一如下:
#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);
}
所以我的问题是:
-
测试两个连接的最简单方法是什么:
PC -> 以太网模块和
以太网模块 -> Arduino? 我需要在我的草图中设置使用过的 Arduino 引脚还是 lib 会这样做?
草图应该可以正常工作吗?
【问题讨论】:
标签: c++ networking arduino udp ethernet