基础任务一:完成主控板W5500初始化(静态IP配置),并能使用局域网电脑ping通
[复制链接]
这篇来测试基础任务一,使用Arduino IDE软件编译。
一、配置软件
1.1、添加开发板库文件
1.2、安装开发板库文件
1.3、下载软件包
1.4、添加库
手动添加库文件,将下载的文件复制到下面文件。
二、软件
测试代码:
-
-
-
-
-
- // 网卡mac地址
- byte mac[] = { 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF };
-
- DNSClient dnClient;
- IPAddress dstip;
- SOCKET pingSocket = 0;
- char buffer[256];
- EthernetICMPPing ping(pingSocket, (uint16_t)random(0, 255));
-
- // 静态ip地址、DNS服务、网关、子网掩码
- IPAddress ip(192, 168, 1, 205);
- IPAddress myDns(192, 168, 1, 1);
- IPAddress gateway(192, 168, 1, 1);
- IPAddress subnet(255, 255, 255, 0);
-
- void setup() {
- // 配置LED
- pinMode(LED_BUILTIN, OUTPUT);
- // 配置串口
- Serial.begin(115200);
- while (!Serial) {
- ; // 等待串口连接
- }
-
- // 静态IP设置
- Serial.println("Ethernet Begin");
- Ethernet.init(17);
- Ethernet.begin(mac, ip, myDns, gateway, subnet);
-
- // 输出网卡mac地址、IP地址、子网掩码、DNS、网关
- Serial.print("My Mac address: ");
- byte macBuffer[6]; // 设置mac地址存储buff
- Ethernet.MACAddress(macBuffer); // 读取mac地址
- for (byte octet = 0; octet < 6; octet++) {
- Serial.print(macBuffer[octet], HEX);
- if (octet < 5) {
- Serial.print('-');
- }
- }
- Serial.println("");
-
- Serial.print("My IP address: ");
- Serial.println(Ethernet.localIP());
-
- Serial.print("My subnet: ");
- Serial.println(Ethernet.subnetMask());
-
- Serial.print("My DNS address: ");
- Serial.println(Ethernet.dnsServerIP());
-
- Serial.print("My GateWay address: ");
- Serial.println(Ethernet.gatewayIP());
-
- dnClient.begin(Ethernet.dnsServerIP());
-
- const char domains[] = "www.eeworld.com.cn";
- if (dnClient.getHostByName(domains, dstip) == 1) {
- Serial.print(domains);
- Serial.print(" = ");
- Serial.println(dstip);
- }
- else Serial.println(F("dns lookup failed"));
-
- }
-
- void loop() {
-
- EthernetICMPEchoReply echoReply = ping(dstip, 4);
- if (echoReply.status == SUCCESS)
- {
- sprintf(buffer,
- "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
- echoReply.data.seq,
- echoReply.addr[0],
- echoReply.addr[1],
- echoReply.addr[2],
- echoReply.addr[3],
- REQ_DATASIZE,
- millis() - echoReply.data.time,
- echoReply.ttl);
- }
- else
- {
- sprintf(buffer, "Echo request failed; %d", echoReply.status);
- }
- Serial.println(buffer);
-
- digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(100); // wait for a second
- digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
- delay(500); // wait for a second
- }
三、运行
3.1、静态IP获取
下载程序后,串口输出配置的静态IP地址
3.2、ping网络
3.3、抓捕工具捕获
|