Arduino以太网的库IP地址()有什么好处?

时间:2013-04-15 20:49:53

标签: arduino ethernet libraries c

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

1 个答案:

答案 0 :(得分:3)

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


我该如何使用?:

Arduino's EthernetIpAdress page上,有以下代码:

1

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