新しい 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++ で使用されている左矢印と右矢印記号でテキストから削除されているようです。
エーテルクラスについてメモを取ります。「udpListener」という名前の Ethercard フォルダーの例を使用しましたが、クラスが宣言されていないことに戸惑いました。私は、ethercard.h で行われたと仮定しました。
プログラムはそのまま動作し、正しく受信した udp パケットを udpSerialPrint メソッドでリッスンして表示するので、リッスン側は動作します。udp パケットの送信者に何かをエコー バックしたかったのですが、udpSend が機能しません。
私が使用しているシールドは、リンクのとおりです: http://hobbycomponents.com/index.php/microduino-enc28j60-ethernet-module-arduino-compatible.html
互換性のあるライブラリは、同じ Web ページから見つかります。
https://github.com/jcw/ethercard
これにより、必要に応じてさらに情報が提供されることを願っています。