Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在嵌入式设备上通过UDP接收数据_C++_C_Udp_Stm32_Mbed - Fatal编程技术网

C++ 如何在嵌入式设备上通过UDP接收数据

C++ 如何在嵌入式设备上通过UDP接收数据,c++,c,udp,stm32,mbed,C++,C,Udp,Stm32,Mbed,目前,我正在为Mbed项目编写UDP客户端脚本。我与B-L475E-IOT01A开发板合作,希望从其他网络设备接收数据 发送端(在另一个设备上)工作。我在我的计算机上用python脚本测试了它 我有以下问题:我可以建立网络连接,并且可以使用open()函数初始化UDP套接字。 我的问题是,如果我尝试将套接字bind()绑定到端口或套接字地址,我会得到nsapi\u错误\u tcode-3002,这意味着“不受支持的功能”。 代码如下: #include "UDPSocket.h&quo

目前,我正在为Mbed项目编写UDP客户端脚本。我与B-L475E-IOT01A开发板合作,希望从其他网络设备接收数据

发送端(在另一个设备上)工作。我在我的计算机上用python脚本测试了它

我有以下问题:我可以建立网络连接,并且可以使用
open()
函数初始化UDP套接字。 我的问题是,如果我尝试将套接字
bind()
绑定到端口或套接字地址,我会得到
nsapi\u错误\u t
code
-3002
,这意味着“不受支持的功能”。 代码如下:

#include "UDPSocket.h"
#include "mbed.h"

const char SSID[] = "";
const char password[] = "";
const uint16_t Port = 37020;

UDPSocket socket;
WiFiInterface *wifi;
BufferedSerial pc(USBTX, USBRX, 115200);

int main() {
  pc.set_format(8, SerialBase::None, 1);

  wifi = WiFiInterface::get_default_instance();
  if (!wifi) {
    printf("ERROR: No WiFiInterface found.\n");
    return -1;
  }

  printf("\nConnecting to %s...\n", SSID);
  int ret = wifi->connect(SSID, password, NSAPI_SECURITY_WPA2);
  if (ret != 0) {
    printf("\nConnection error: %d\n", ret);
    return -1;
  }

  SocketAddress a;
  wifi->get_ip_address(&a);
  printf("IP: %s\n", a.get_ip_address());

  int returnable = 1;
  while (returnable != 0) {
    returnable = socket.open(wifi);
    printf("%d\r\n", returnable);
    wait_us(500000);
  }

  returnable = 1;
  while (returnable != 0) {
    returnable = socket.bind(Port);
    printf("%d\r\n", returnable);
    wait_us(500000);
  }

  while (1) {
    char message[500];
    int n = socket.recv(&message, sizeof(message));
    printf("%d\r\n", n);
    printf("%s", message);
    wait_us(100000);
  }
  wifi->disconnect();

  printf("\nDone\n");
}
出于安全原因,我删除了wifi数据。以下是输出:

Connecting to ...
IP: 192.168.178.25
0
-3002
我的问题是,为什么我不能将板绑定到特定端口。你有什么想法吗?
提前感谢。

MBED声明
socket.bind
bind(const-Host&me)
。参数不是端口号,而是主机实例。换成

sock.bind(Host(IpAddr(), port));

C还是C++?请标记您实际使用的语言。C和C++有一些重叠,但它们是不同的语言,使用两种语言,但是主要语言是C++,你编译的代码是C还是C++?我编译为C++代码段,弃权版本<代码> bDUDE()/Cube >,就像你提到的一样。但是在mbed 6版本中,我使用了这两个函数:
nsapi\u error\u t bind(const SocketAddress&address)
nsapi\u error\u t bind(uint16\u t port)
。最后一个函数调用ip地址为
0.0.0.0
的第一个函数。我两个都试过了,但没有一个像我想的那样有效。