我有一块英特尔 Galileo Gen 2 开发板,我正在使用 Arduino IDE 测试一些代码。我在配置以太网时遇到问题。我有以下代码,基本上是从示例演变而来的:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x98, 0x4F, 0xEE, 0x05, 0x65, 0x02 };
IPAddress server(192, 168, 15, 64);
IPAddress ip(192, 168, 15, 177);
IPAddress dnServer(8, 8, 8, 8);
IPAddress gateway(192, 168, 15, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetClient client;
void setup() {
Serial.begin(9600);
Serial.print("ip - ");
Serial.println(ip);
Ethernet.begin(mac, ip, dnServer, gateway, subnet);
delay(1000);
system("ifup eth0");
delay(1000);
Serial.println(Ethernet.localIP());
Serial.println("connecting...");
if (client.connect("www.google.com", 80)) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
// the loop function runs over and over again forever
void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
这是我看到的结果:
ip - 192.168. 15.177
255.255.255.255
connecting...
connection failed
disconnecting.
我看到的是没有分配 IP 地址,因为 localIP 表示 255.255.255.255。
我尝试了一些在论坛中找到的解决方法(界面向上,禁用 de SD 卡),但没有任何效果。
我做错了什么?
谢谢
更新
我已更正了错误的 MAC 地址。添加了 ifup 命令。没有变化。
添加system("ifconfig -a > /dev/ttyGS0");
命令后,我得到了这个:
enp0s20f6 Link encap:Ethernet HWaddr 98:4F:EE:05:65:02
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:980 (980.0 B)
Interrupt:50 Base address:0x4000
enp0s20f6:avahi Link encap:Ethernet HWaddr 98:4F:EE:05:65:02
inet addr:169.254.4.217 Bcast:169.254.255.255 Mask:255.255.0.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
Interrupt:50 Base address:0x4000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:584 errors:0 dropped:0 overruns:0 frame:0
TX packets:584 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:45040 (43.9 KiB) TX bytes:45040 (43.9 KiB)
据我了解,它并没有尝试设置 IP,而是仍在搜索 DHCP。