【问题标题】:What is the benefit of Arduino Ethernet's library IPAddress()?Arduino 以太网的库 IPAddress() 有什么好处?
【发布时间】:2013-04-15 20:49:53
【问题描述】:

Arduino 默认的Ethernet library class 包含一个IPAddress 变量类型。这是什么IPAddress?为什么要使用它,为什么不用于official example 中的网关和子网IP?

【问题讨论】:

    标签: arduino ethernet libraries c


    【解决方案1】:

    就像你说的那样,一种可以存储IP地址的变量(例如int(整数))。使用整数,你不能添加所需的.s在 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 或类似的东西会更容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多