arduino写库函数(Arduino-Ethernet库学习笔记2)

在此之前可以阅读前篇博文Arduino-Ethernet库学习笔记(1)这篇博文是关于该库的简介、调试工具之UDP/TCP网络调试助手NetAssist介绍、Postman介绍以及网络方面的小经验下面开始来学习Arduino-Ethernet库中的Ethernet 类,下面我们就来聊聊关于arduino写库函数?接下来我们就一起去了解一下吧!

arduino写库函数(Arduino-Ethernet库学习笔记2)

arduino写库函数

在此之前可以阅读前篇博文Arduino-Ethernet库学习笔记(1)。这篇博文是关于该库的简介、调试工具之UDP/TCP网络调试助手NetAssist介绍、Postman介绍以及网络方面的小经验。下面开始来学习Arduino-Ethernet库中的Ethernet 类。

1 Ethernet 类

以太网类初始化以太网库和网络设置。

1.1 Ethernet.begin()
  • 描述

初始化以太网库和网络设置。在1.0版中,该库支持DHCP。在正确的网络设置下使用Ethernet.begin(mac),以太网板将自动获取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个字节的数组);

gateway:网络网关的IP地址(4个字节的数组)。可选:默认为设备IP地址,最后一个八位位组设置为1;

subnet:网络的子网掩码(4个字节的数组)。可选:默认为255.255.255.0。

  • 返回值

此函数的DHCP版本Ethernet.begin(mac)返回一个int:成功的DHCP连接为1,失败的则为0。其他版本不返回任何内容。

  • 例子

#include <SPI.h>

#include <Ethernet.h>

// 板的媒体访问控制(以太网硬件)地址:

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

//板的IP地址:

byte ip[] = { 10, 0, 0, 177 };

void setup()

{

Ethernet.begin(mac, ip);

}

void loop () {}

1.2 Ethernet.dnsServerIP()
  • 描述

返回设备的DNS服务器IP地址。

  • 语法

Ethernet.dnsServerIP()

  • 参数

  • 返回值

设备的DNS服务器IP地址(IPAddress)

  • 例子

#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);

Serial.print("The DNS server IP address is: ");

Serial.println(Ethernet.dnsServerIP());

}

void loop () {}

  • 串口打印结果:

The DNS server IP address is: 10.0.0.1

1.3 Ethernet.gatewayIP()
  • 描述

返回设备的网关IP地址。

  • 语法

Ethernet.gatewayIP()

  • 参数

  • 返回值

设备的网关IP地址(IPAddress)。

  • 例子

#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);

Serial.print("The gateway IP address is: ");

Serial.println(Ethernet.gatewayIP());

}

void loop () {}

  • 串口打印结果:

The gateway IP address is: 10.0.0.1

1.4 Ethernet.hardwareStatus()
  • 描述

Ethernet.hardwareStatus()告诉您在Ethernet.begin()期间检测到哪个WIZnet以太网控制器芯片(如果有)。这可用于故障排除。如果未检测到以太网控制器,则可能是硬件问题。

  • 语法

Ethernet.hardwareStatus()

  • 参数

  • 返回值

在Ethernet.begin()(EthernetHardwareStatus)期间检测到哪个WIZnet以太网控制器芯片:

EthernetNoHardware

EthernetW5100

EthernetW5200

EthernetW5500

  • 例子

#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);

if (Ethernet.hardwareStatus() == EthernetNoHardware) {

Serial.println("Ethernet shield was not found.");

}

else if (Ethernet.hardwareStatus() == EthernetW5100) {

Serial.println("W5100 Ethernet controller detected.");

}

else if (Ethernet.hardwareStatus() == EthernetW5200) {

Serial.println("W5200 Ethernet controller detected.");

}

else if (Ethernet.hardwareStatus() == EthernetW5500) {

Serial.println("W5500 Ethernet controller detected.");

}

}

void loop () {}

  • 串口打印结果:

W5100 Ethernet controller detected.

1.5 Ethernet.init()
  • 描述

用于配置以太网控制器芯片的CS(片选)引脚。以太网库具有默认的CS引脚,通常是正确的,但是对于某些非标准以太网硬件,可能需要使用其他CS引脚。

  • 语法

Ethernet.init(sspin)

  • 参数

spin:用于CS的引脚号(字节)

  • 返回值

  • 例子

#include <SPI.h>

#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

IPAddress ip(10, 0, 0, 177);

void setup() {

Ethernet.init(53);// 将引脚53用于以太网CS

Ethernet.begin(mac, ip);

}

void loop () {}

1.6 Ethernet.linkStatus()
  • 描述

告诉您链接是否处于活动状态。 LinkOFF可能表示以太网电缆已拔出或有故障。仅当使用W5200和W5500以太网控制器芯片时,此功能才可用。

  • 语法

Ethernet.linkStatus()

  • 参数

  • 返回值

链接状态(EthernetLinkStatus):

Unknown

LinkON

LinkOFF

  • 例子

#include <SPI.h>

#include <Ethernet.h>

void setup() {

//打开串行通信并等待端口打开

Serial.begin(9600);

while (!Serial) {

; // 等待串行端口连接。仅本地USB端口需要

}

}

void loop () {

if (Ethernet.linkStatus() == Unknown) {

Serial.println("Link status unknown. Link status detection is only available with W5200 and W5500.");

}

else if (Ethernet.linkStatus() == LinkON) {

Serial.println("Link status: On");

}

else if (Ethernet.linkStatus() == LinkOFF) {

Serial.println("Link status: Off");

}

}

  • 串口打印结果:

Link status unknown. Link status detection is only available with W5200 and W5500.

1.7 Ethernet.localIP()
  • 描述

获取以太网板的IP地址。通过DHCP自动分配地址时很有用。

  • 语法

Ethernet.localIP();

  • 参数

  • 返回值

IP地址

  • 例子

#include <SPI.h>

#include <Ethernet.h>

// 在下面输入控制器的MAC地址。

// 较新的以太网防护罩在防护罩上的标签上印有MAC地址

byte mac[] = {

0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

// 初始化以太网客户端库

// 服务器的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地址

}

void loop () {}

1.12 Ethernet.setMACAddress()
  • 描述

设置MAC地址。不适用于DHCP。

  • 语法

Ethernet.setMACAddress(mac)

  • 参数

mac:要使用的MAC地址(6个字节的数组)

  • 返回值

  • 例子

#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);

byte newMac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};

Ethernet.setMACAddress(newMac); //更改MAC地址

}

void loop () {}

1.13 Ethernet.setRetransmissionCount()
  • 描述

设置以太网控制器在放弃之前要进行的传输尝试次数。初始值为8。8次传输尝试乘以200毫秒的默认超时,等于通信失败期间的1600毫秒的阻塞延迟。如果通信出现问题,您可能希望设置一个较小的数字以使您的程序更具响应性。尽管有名称,但它设置了传输尝试的总数(而不是第一次尝试失败后的重试次数),因此您想要设置的最小值为1。

  • 语法

Ethernet.setRetransmissionCount(number)

  • 参数

number:以太网控制器在放弃之前应该进行的传输尝试次数(字节)。

  • 返回值

  • 例子

#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);

Ethernet.setRetransmissionCount(1); // 将以太网控制器配置为仅尝试一个

}

void loop () {}

1.14 Ethernet.setRetransmissionTimeout()
  • 描述

设置以太网控制器的超时时间。初始值为200毫秒。 200 ms的超时时间乘以默认的8次尝试,等于通信失败期间的1600 ms阻塞延迟。您可能希望设置较短的超时时间,以使程序在通信出现问题时更加敏感。您将需要做一些试验,以确定适合您的特定应用程序的值。

  • 语法

Ethernet.setRetransmissionTimeout(milliseconds)

  • 参数

milliseconds:超时时间(uint16_t)

  • 返回值

  • 例子

#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);

Ethernet.setRetransmissionTimeout(50); // 将以太网控制器的超时设置为50毫秒

}

void loop () {}

1.15 Ethernet.setSubnetMask()
  • 描述

设置网络的子网掩码。不适用于DHCP。

  • 语法

Ethernet.setSubnetMask(subnet)

  • 参数

subnet:网络的子网掩码(IP地址)。

  • 返回值

  • 例子

#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);

IPAddress subnet(255, 255, 0, 0);

void setup() {

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

IPAddress newSubnet(255, 255, 255, 0);

Ethernet.setSubnetMask(newSubnet); // 更改子网掩码

}

void loop () {}

1.16 Ethernet.subnetMask()
  • 描述

返回设备的子网掩码。

  • 语法

Ethernet.subnetMask()

  • 参数

  • 返回值

设备的子网掩码(IPAddress)

  • 例子

#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);

Serial.print("The subnet mask is: ");

Serial.println(Ethernet.subnetMask());

}

void loop () {}

  • 串口打印结果:

The subnet mask is: 255.255.255.0

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页