【问题标题】:mbed ethernet interface not workingmbed 以太网接口不工作
【发布时间】:2017-02-10 11:55:52
【问题描述】:

我有一块 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,我看不到更多结果。

我做错了什么?

【问题讨论】:

  • 我根本不知道这一点(net. 看起来比 C++ 更像 C++)但是你可以进入 EthernetInterface 构造函数或 net.set_network() 吗?除非其中任何一个对其进行了初始化,否则我不希望该类具有 MAC 地址。希望你有 EthernetInterface 源代码?

标签: c mbed


【解决方案1】:

使用 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

【讨论】:

  • 它在 net.connect(); 上的同一点停止。代码太简单了,我真的不知道要更改或搜索什么。
  • 您使用的是哪个版本的 mbed OS?我已经在我的家庭网络上对此进行了测试,它运行良好......
  • 周五我下载了存储库中的最后一个版本。我无法找到如何获取修订号,但我知道我是最新的。
  • 您是在本地构建吗?如果是这样,请使用终端进入 mbed-os 目录并运行 git rev-parse HEAD。如果您在在线编译器中构建,请单击库并单击“修订”按钮。
  • 我认为我遇到了网络问题。在我检查之前,我认为这是导致故障的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多