當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Arduino Ethernet - Ethernet.begin()用法及代碼示例

說明

初始化以太網庫和網絡設置。

在 1.0 版中,該庫支持 DHCP。使用 Ethernet.begin(mac) 和正確的網絡設置,Ethernet shield 將自動獲得一個 IP 地址。這會顯著增加草圖大小。為確保在需要時正確更新 DHCP 租約,請務必定期調用 Ethernet.maintain()。

用法

Ethernet.begin(mac);
Ethernet.begin(mac, ip);
Ethernet.begin(mac, ip, dns);
Ethernet.begin(mac, ip, dns, gateway);
Ethernet.begin(mac, ip, dns, gateway, subnet);

參數

  • mac:設備的 MAC(媒體訪問控製)地址(6 字節數組)。這是您的防護罩的以太網硬件地址。較新的 Arduino 以太網擴展板包含帶有設備 MAC 地址的標簽。對於較舊的盾牌,請選擇您自己的。
  • ip:設備的IP地址(4字節數組)
  • dns:DNS 服務器的 IP 地址(4 字節數組)。可選:默認為最後一個八位組設置為 1 的設備 IP 地址
  • gateway:網絡網關的 IP 地址(4 字節數組)。可選:默認為最後一個八位組設置為 1 的設備 IP 地址
  • subnet:網絡的子網掩碼(4 個字節的數組)。可選:默認為 255.255.255.0

返回

  • 此函數的 DHCP 版本 Ethernet.begin(mac) 返回一個 int:1 表示成功的 DHCP 連接,0 表示失敗。
  • 其他版本不返回任何內容。

示例

#include <SPI.h>
#include <Ethernet.h>

// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
//the IP address for the shield:
byte ip[] = { 10, 0, 0, 177 };    

void setup()
{
  Ethernet.begin(mac, ip);
}

void loop () {}

相關用法


注:本文由純淨天空篩選整理自arduino.cc大神的英文原創作品 Ethernet - Ethernet.begin()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。