Ethernet.begin()
描述
初始化 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 位址是 86Duino 的 IP 位址,但位址最后面的值是 1。例如:86Duino IP 位址是 192.168.0.123,若没有设定此参数,DNS server 预设的 IP 位址为 192.168.0.1。
gateway
:gateway 的 IP 位址 (4 bytes 大小的阵列)。若没有设定此参数,预设的 IP 位址是 86Duino 的 IP 位址,但位址最后面的值是 1。例如:86Duino 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 }; // 设定IP 位址: byte ip[] = { 10, 0, 0, 177 }; void setup() { Ethernet.begin(mac, ip); } void loop () {}
本页由热血自造者 Allen Chang 译自英文版。
The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.