gpt4 book ai didi

c - mbed 以太网接口(interface)不工作

转载 作者:行者123 更新时间:2023-11-30 15:00:22 24 4
gpt4 key购买 nike

我有一 block NXP FRDM-K64F 板,我想设置以太网示例,但我无法让它工作。这就是我的代码在设置静态 IP 地址后的样子。

#include "mbed.h"
#include "main-hw.h"
#include "EthernetInterface.h"

// Network interface
EthernetInterface net;


int main(void)
{
// Bring up the ethernet interface
printf("Ethernet socket example\r\n");

int ret;
ret = net.set_network("192.168.15.177","255.255.255.0","192.168.15.1");
printf("Set Net: %d\r\n",ret);

char macadd[6];
mbed_mac_address(macadd);
printf("%02x:%02x:%02x:%02x:%02x:%02x \r\n", macadd[0], macadd[1], macadd[2], macadd[3], macadd[4], macadd[5]);

const char *mac = net.get_mac_address();
printf("MAC address is: %s\r\n", mac ? mac : "No MAC");

const char *ip = net.get_ip_address();
printf("IP address is: %s\r\n", ip ? ip : "No IP");

ret = net.connect();
printf("Connect: %d\n",ret);

// Show the network address
// const char *ip = net.get_ip_address();
// printf("IP address is: %s\n", ip ? ip : "No IP");

// Open a socket on the network interface, and create a TCP connection to mbed.org
TCPSocket socket;
socket.open(&net);
socket.connect("developer.mbed.org", 80);

// Send a simple http request
char sbuffer[] = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n";
int scount = socket.send(sbuffer, sizeof sbuffer);
printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);

// Recieve a simple http response and print out the response line
char rbuffer[64];
int rcount = socket.recv(rbuffer, sizeof rbuffer);
printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);

// Close the socket to return its memory and bring down the network interface
socket.close();

// Bring down the ethernet interface
net.disconnect();
printf("Done\n");

return 0;
}

我看到的是,我只能使用 mbed_mac_address 命令获取 macAddress。使用 net.get_mac_address 和 net.get_ip_address 我只得到 NULL 值。

进程到达 net.connect,但我没有看到更多结果。

我做错了什么?

最佳答案

对于 mbed OS 5.3.4,这对我在 K64F 上运行良好:

#include "mbed.h"
#include "EthernetInterface.h"

// Network interface
EthernetInterface net;

// Socket demo
int main() {
// Set static IP
net.set_network("192.168.1.99", "255.255.255.0", "192.168.1.1");

// Bring up the ethernet interface
printf("Ethernet socket example\n");
net.connect();

// Show the network address
const char *ip = net.get_ip_address();
printf("IP address is: %s\n", ip ? ip : "No IP");

printf("MAC address is: %s\n", net.get_mac_address());

// Open a socket on the network interface, and create a TCP connection to mbed.org
TCPSocket socket;
socket.open(&net);
socket.connect("developer.mbed.org", 80);

// Send a simple http request
char sbuffer[] = "GET / HTTP/1.1\r\nHost: developer.mbed.org\r\n\r\n";
int scount = socket.send(sbuffer, sizeof sbuffer);
printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);

// Recieve a simple http response and print out the response line
char rbuffer[64];
int rcount = socket.recv(rbuffer, sizeof rbuffer);
printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);

// Close the socket to return its memory and bring down the network interface
socket.close();

// Bring down the ethernet interface
net.disconnect();
printf("Done\n");
}

更新 mbed 操作系统

如果在线编译器中仍然有 mbed 库(不是 mbed-os),请右键单击“mbed”,然后单击“删除”。然后单击“添加库”>“来自 URL”并输入 https://github.com/armmbed/mbed-os .

如果您有 mbed-os,请右键单击库并选择“升级”。

来自 mbed CLI:

$ mbed remove mbed
$ mbed add mbed-os

或者当您已经拥有 mbed-os 时:

$ cd mbed-os
$ git pull
$ git checkout latest

关于c - mbed 以太网接口(interface)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42158817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com