简体   繁体   English

Arduino以太网的库IPAddress()有什么好处?

[英]What is the benefit of Arduino Ethernet's library IPAddress()?

The Arduino default Ethernet library class contains an IPAddress variable type. Arduino默认以太网库类包含IPAddress变量类型。 What is this IPAddress for? 这是什么IPAddress Why should I use it and why is it not used for the gateway and subnet IPs in the official example ? 我为什么要使用它,为什么它不用于官方示例中的网关和子网IP?

It just, like you said, a type of variable (such as int (integer)) that can store a IP address. 就像你说的那样,它只是一种可以存储IP地址的变量(例如int (整数))。 Using integers, you cannot add the . 使用整数,您无法添加. s needed in the IP address. 在IP地址中需要的。 Also, the library only accepts integers, because with strings, things "can get messy." 此外,该库只接受整数,因为对于字符串,事情“可能变得混乱”。 For example, if you have 1 in a string, you cannot add that with another number. 例如,如果字符串中有1 ,则无法将其与另一个数字相加。 However, if you have the integer variable type with the value of 1 , it would add easily. 但是,如果您具有值为1的整数变量类型,则可以轻松添加。


How can I use this?: 我该怎么用?:

On Arduino's EthernetIpAdress page , there is this code: Arduino的EthernetIpAdress页面上 ,有以下代码:

 #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);
 }

On the line IPAddress ip(192,168,1,1); 在线IPAddress ip(192,168,1,1); , it creates a variable that holds the IP address. ,它创建一个保存IP地址的变量。 In the line Ethernet.begin(mac, ip, gateway, subnet); 在线路Ethernet.begin(mac, ip, gateway, subnet); the variable is looked up and given to the Ethernet library. 查找变量并将其提供给Ethernet库。 I don't know what the advantage would be, besides trying to prevent people from using the integer type and making it look cleaner. 我不知道它的优点是什么,除了试图阻止人们使用整数类型并使其看起来更干净。 It could look up the automatically issued IP address and then store it for later so if it goes into an "idle mode," it can ask for the same IP address so it is almost like a dynamic IP that wouldn't interfere with other devices and would reset when the reset button is pushed. 它可以查找自动发出的IP地址,然后将其存储以供日后使用,如果它进入“空闲模式”,它可以要求相同的IP地址,因此它几乎就像一个不会干扰其他设备的动态IP并在按下重置按钮时重置。 I'm sure that there is some use for it, but I cannot think of one. 我确信它有一些用处,但我想不出一个。 I just wanted to tell you what it is and how to use it. 我只想告诉你它是什么以及如何使用它。 I would think though that it would be easier just to use #define IPadress 192.168.1.1 or a similar thing if you wanted it to easily be changed or to be more user readable. 我想虽然如果你想让它易于更改或者更易于用户阅读,那么使用#define IPadress 192.168.1.1或类似的东西会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM