Arduino示例中的DhcpAddressPrinter无法正常工作

时间:2014-04-25 14:31:30

标签: arduino ethernet

我使用Arduino mega r3和以太网盾的组合。使用DhcpAddressPrinter的示例时,我无法获得任何结果。我没有更改任何代码。

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
}

void loop() {

}

之后,我添加了一些&#34; println&#34;在代码中如下:     #包括     #include

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
   Serial.println("1");
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
   Serial.println("2");
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
   Serial.println("3");
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
}

void loop() {

}

我可以从串口监视器获得结果1和2,但不能接收3; 所以我怀疑Ethernet.begin(mac)功能一直在运行,并且不知道为什么。 我已将mac地址更改为其他人,但得到相同的结果。

1 个答案:

答案 0 :(得分:0)

您是否同时使用microSD?有时它可能会在以太网盾上产生DHCP问题