[Ethernet]
描述
初始化 Ethernet 函式庫和網路設定。
此函式庫也有支援 DHCP 功能。使用 Ethernet.begin(mac)
或 Ethernet.begin()
之後,內部會設定適合的網路參數,並自動取得一個 IP 位址。
語法
Ethernet.begin(); 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 (Media access control) 位址的陣列,大小為 6 bytes。每一片 86Duino 在出廠時,內部已經燒錄一個專屬的 MAC 位址,所以這個參數可以不設定。保留此參數的目的,是為了與 Arduino Ethernet 函式庫維持相容性。ip
:你想要設定的 86Duino IP 位址 (4 bytes 大小的陣列)。dns
DNS server 的 IP 位址 (4 bytes 大小的陣列)。若沒有設定此參數,預設的 IP 位址是設備的 IP 位址,但位址最後面的值是 1。例如:設備位址是 192.168.0.123,若沒有設定此參數,DNS server 預設的 IP 位址為 192.168.0.1。gateway
gateway 的 IP 位址 (4 bytes 大小的陣列)。若沒有設定此參數,預設的 IP 位址是設備的 IP 位址,但位址最後面的值是 1。例如:設備IP位址是 192.168.0.123,若沒有設定此參數,gateway 預設的 IP 位址為 192.168.0.1。subnet
子網路遮罩 (4 bytes 大小的陣列)。若沒有設定此參數,預設值為 255.255.255.0 。
回傳
在 DHCP 模式下,呼叫 Ethernet.begin(mac)
或 Ethernet.begin()
會回傳一個 int 值:1 代表 DHCP 連接成功,0 是失敗。在其它模式下不回傳任何值。
範例
#include <Ethernet.h> byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //the IP address: byte ip[] = { 10, 0, 0, 177 }; void setup() { Ethernet.begin(mac, ip); } void loop () {}
函式庫參考主頁面
86Duino 參考的文本是根據 Creative Commons Attribution-ShareAlike 3.0 License,部分文本是從 the Arduino reference 修改的。 參考中的代碼示例已發佈到公共領域。