简体   繁体   English

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

[英]How can I recieve data over UDP on embedded device

currently I am working on a UDP-Client Script for a Mbed Project.目前我正在为 Mbed 项目开发 UDP 客户端脚本。 I work with the B-L475E-IOT01A development board and want to recieve data from another network device.我使用 B-L475E-IOT01A 开发板,想从另一个网络设备接收数据。

The sending side (on another device) works.发送方(在另一台设备上)工作。 I tested it with a python script on my computer.我在我的电脑上用 python 脚本测试了它。

I have the following problem: I can establish conection to the network and I can initialize a UDP socket with the open() function.我有以下问题:我可以建立到网络的连接,我可以用open()函数初始化一个 UDP 套接字。 My problem is, that if I try to bind() the socket to either a port or a socket address I get the nsapi_error_t code -3002 , which means "unsopported functionality".我的问题是,如果我尝试bind()套接字bind()到端口或套接字地址,我会得到nsapi_error_t代码-3002 ,这意味着“不受支持的功能”。 Here is the code:这是代码:

#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");
}

I deleted the wifi data for safaty reasons.出于安全原因,我删除了wifi数据。 Here is the output:这是输出:

Connecting to ...
IP: 192.168.178.25
0
-3002

My question is, why I cant bind my board to a specific port.我的问题是,为什么我不能将我的开发板绑定到特定端口。 Do you have any ideas?你有什么想法? Thank you in advance.先感谢您。

MBED declares socket.bind as bind (const Host &me) . MBED 将socket.bind声明为bind (const Host &me) The argument is not a port number, but a Host instance.参数不是端口号,而是Host实例。 Change it to将其更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM