簡體   English   中英

Arduino以太網的庫IPAddress()有什么好處?

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

Arduino默認以太網庫類包含IPAddress變量類型。 這是什么IPAddress 我為什么要使用它,為什么它不用於官方示例中的網關和子網IP?

就像你說的那樣,它只是一種可以存儲IP地址的變量(例如int (整數))。 使用整數,您無法添加. 在IP地址中需要的。 此外,該庫只接受整數,因為對於字符串,事情“可能變得混亂”。 例如,如果字符串中有1 ,則無法將其與另一個數字相加。 但是,如果您具有值為1的整數變量類型,則可以輕松添加。


我該怎么用?:

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

在線IPAddress ip(192,168,1,1); ,它創建一個保存IP地址的變量。 在線路Ethernet.begin(mac, ip, gateway, subnet); 查找變量並將其提供給Ethernet庫。 我不知道它的優點是什么,除了試圖阻止人們使用整數類型並使其看起來更干凈。 它可以查找自動發出的IP地址,然后將其存儲以供日后使用,如果它進入“空閑模式”,它可以要求相同的IP地址,因此它幾乎就像一個不會干擾其他設備的動態IP並在按下重置按鈕時重置。 我確信它有一些用處,但我想不出一個。 我只想告訴你它是什么以及如何使用它。 我想雖然如果你想讓它易於更改或者更易於用戶閱讀,那么使用#define IPadress 192.168.1.1或類似的東西會更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM