以太网电缆被拔出什么意思 笔记本以太网网络电缆被拔出( 三 )


// 初始化以太网客户端库
// 服务器的IP地址和端口
// 您要连接的端口(HTTP默认为端口80):
EthernetClient client;
void setup() {
//启动串行库:
Serial.begin(9600);
// 启动以太网连接:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
//进行下去没有意义,因此永远不要做任何事情:
for(;;)
;
}
// 打印您的本地IP地址:
Serial.println(Ethernet.localIP());
}
void loop() {
}
【以太网电缆被拔出什么意思 笔记本以太网网络电缆被拔出】
1.8 Ethernet.MACAddress()

  • 描述
用设备的MAC地址填充提供的缓冲区 。
  • 语法
Ethernet.MACAddress(mac_address)
  • 参数
mac_address:缓冲区,用于接收MAC地址(6个字节的数组)
  • 返回值

  • 例子
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);
void setup() {
Serial.begin(9600);
while (!Serial) {
; // 等待串行端口连接 。仅本地USB端口需要
}
Ethernet.begin(mac, ip);
byte macBuffer[6]; // 创建一个缓冲区来保存MAC地址
Ethernet.MACAddress(macBuffer); // 填满缓冲区
Serial.print("The MAC address is: ");
for (byte octet = 0; octet < 6; octet++) {
Serial.print(macBuffer[octet], HEX);
if (octet < 5) {
Serial.print('-');
}
}
}
void loop () {}
  • 串口打印结果:
The MAC address is: DE-AD-BE-EF-FE-ED
1.9 Ethernet.setDnsServerIP()
  • 描述
设置DNS服务器的IP地址 。不适用于DHCP 。
  • 语法
Ethernet.setDnsServerIP(dns_server)
  • 参数
dns_server:DNS服务器的IP地址(IPAddress) 。
  • 返回值

  • 例子
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);
IPAddress myDns(192, 168, 1, 1);
void setup() {
Ethernet.begin(mac, ip, myDns);
IPAddress newDns(192, 168, 1, 1);
Ethernet.setDnsServerIP(newDns); // 更改DNS服务器IP地址
}
void loop () {}
1.10 Ethernet.setGatewayIP()
  • 描述
设置网络网关的IP地址 。不适用于DHCP 。
  • 语法
Ethernet.setGatewayIP(gateway)
  • 参数
gateway:网络网关的IP地址(IPAddress) 。
  • 返回值

  • 例子
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
void setup() {
Ethernet.begin(mac, ip, myDns, gateway);
IPAddress newGateway(192, 168, 100, 1);
Ethernet.setGatewayIP(newGateway); // 更改网关IP地址
}
void loop () {}
1.11 Ethernet.setLocalIP()
  • 描述
设置设备的IP地址 。不适用于DHCP 。
  • 语法
Ethernet.setLocalIP(local_ip)
  • 参数
local_ip:要使用的IP地址(IPAddress) 。
  • 返回值

  • 例子
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);
void setup() {
Ethernet.begin(mac, ip);
IPAddress newIp(10, 0, 0, 178);
Ethernet.setLocalIP(newIp); // 更改IP地址

推荐阅读