![](/img/trans.png)
[英]Microduino ENC28J60 Ethernet Module (Arduino compatible) UDP Send not working
[英]Microduino ENC28J60 Ethernet Module Arduino compatible, UDP Send not working
我正在使用新的Microduino ENC28J60以太网模块(与Arduino兼容)。
我正在使用udpListener草图,并希望在UDP数据包到达时将消息回显给发送方。
我收到的消息还可以,但是回调方法中的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());
}
=====其他信息====
你好
抱歉,包括:
include EtherCard.h include IPAddress.h在c ++中,它们似乎已从带有左箭头和右箭头符号的文本中删除。
我注意到你关于以太坊的情况。 我使用了Ethercard文件夹中的一个名为“ udpListener”的示例,并对未声明该类感到困惑。 我以为是在ethercard.h中完成的。
该程序照常运行,并使用udpSerialPrint方法侦听并显示正确接收的udp数据包,因此侦听端可以正常工作。 我想将某些信息回显到udp数据包的发送者,这是udpSend不起作用的。
我使用的屏蔽位于链接上: http : //hobbycomponents.com/index.php/microduino-enc28j60-ethernet-module-arduino-compatible.html
可通过同一网页找到兼容的库:
https://github.com/jcw/ethercard
我希望这会在必要时为您提供更多信息。
问题似乎是常规的:
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以使其符合要求,并增加消息长度。
我遇到了同样的问题,输入正确的IP后发现我的网关IP地址错误,我的代码可以正常工作。 如果所有参数正确,下面的功能可以正常工作,我也使用Java udp客户端进行了检查
ether.sendUdp(msg, sizeof msg, portMy, ipDestination, portDestination);
这是一个老问题,但对于后代来说,这是一种解决方法:
我已确认您从2016年10月起对Ethercard库的观察。问题是,如果目标位于本地子网上,则该库必须对属于目标IP地址的MAC进行ARP查找-否。 您可以通过运行Wireshark并查看ENC28J60传输的UDP数据包来确认这一点。 展开“ Ethernet II”信息,您将看到目标MAC是00:00:00:00:00:00。
一种可能的解决方法是将传输到本地子网的广播地址。 然后,无论目标MAC地址如何,本地UDP服务器都将接收数据包。
遇到相同的问题:/您可以尝试使用makeUdpReply-它适用于您的示例:D
ether.makeUdpReply(msg, sizeof msg, portDestination);
我有同样的问题,可以发送但无法接收。
我认为您上面的代码很好。
ether.sendUdp(msg, sizeof msg, portMy, ipDestination, portDestination);
解决方案是:
这样的IP配置
电脑:
IP : 192, 168, 0, 9
Gateway : (none)
DNS : (none)
Port open:8888
的Arduino:
IP : 192,168,0,201
Gateway : 192, 168, 0, 9
DNS : (none)
Port open:8888
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.