2013-04-15 20 views

回答

3

就像你说的那样,它只是一种可以存储IP地址的变量(如int(整数))。使用整数,您不能在IP地址中添加所需的.。此外,库只接受整数,因为对于字符串,事情“可能会变得混乱”。例如,如果您的字符串中有1,则无法将其添加到另一个数字中。但是,如果您具有值为1的整数变量类型,则会轻松添加。


我如何使用这个?:

Arduino's EthernetIpAdress page,有这样的代码:

#include <Ethernet.h> 

// network configuration. gateway and subnet are optional. 

    // the media access control (ethernet hardware) address for the shield: 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
// the router's gateway address: 
byte gateway[] = { 10, 0, 0, 1 }; 
// the subnet: 
byte subnet[] = { 255, 255, 0, 0 }; 

EthernetServer server = EthernetServer(23); 

//the IP address is dependent on your network 
IPAddress ip(192,168,1,1); 
void setup() 
{ 
    // initialize the ethernet device 
    Ethernet.begin(mac, ip, gateway, subnet); 

    // start listening for clients 
    server.begin(); 
} 
void loop() 
{ 
    //print out the IP address 
    Serial.println(myIPaddress); 
} 

上线IPAddress ip(192,168,1,1);,它会创建一个保存变量IP地址。在Ethernet.begin(mac, ip, gateway, subnet);行中查找变量并将其提供给Ethernet库。我不知道优点是什么,除了试图阻止人们使用整数类型并使其看起来更干净。它可以查找自动发布的IP地址,然后将其存储以备后用,如果它进入“空闲模式”,它可以请求相同的IP地址,因此它几乎就像一个动态IP,不会干扰其他设备并在重置按钮被按下时重置。我确信这有一些用处,但我想不出来。我只是想告诉你它是什么以及如何使用它。我想尽管如果你想让它易于改变或更易于用户阅读,使用#define IPadress 192.168.1.1或类似的东西会更容易。