mbed 以太网接口不工作

mbed ethernet interface not working

我有一个 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 OS

如果在线编译器中还有 mbed 库(不是 mbed-os),请右键单击 'mbed',然后单击 'Remove'。然后点击 'Add library' > 'From URL' 并输入 https://github.com/armmbed/mbed-os.

如果您有 mbed-os,请右键单击该库并 select 'Upgrade'。

来自 mbed CLI:

$ mbed remove mbed
$ mbed add mbed-os

或者当您已经 mbed-os:

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