如何使用 Simulink 外部模式在 Lego Mindstorms EV3 上发送和接收 UDP 数据包?

How to send and receive UDP Pakets on Lego Mindstorms EV3 using Simulink External Mode?

我正在尝试使用 C 代码为我的 EV3 创建一个 simulink 块,以从其传感器发送测量值 and/or 通过 UDP 数据包从其他硬件 (raspberry pi) 接收数据。但是我在互联网上找不到任何具体的例子。我尝试按照 https://www.abc.se/~m6695/udp.html 中的示例编写自己的代码。我希望它能工作,因为 EV3 是一个 linux 系统。但是,它不起作用。

udp_receiver 的 c 代码库(已更新):

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

// BUFLEN = max size
#define BUFLEN 512
#define NPACK 1


double * linux_udp_empfaenger(int32_t PORT)
{
  struct sockaddr_in si_me, si_other;

  int s, i, j, slen = sizeof(si_other) , recv_len;
  char buf[BUFLEN];
  //char buf_h[BUFLEN];
  unsigned short recv_port = (unsigned short) PORT;
  double * data;

  //create a UDP socket
  if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  {
    return 0;
    exit(1);
  } else{

    // zero out the structure
    memset((char *) &si_me, 0, sizeof(si_me));

   si_me.sin_family = AF_INET;
   si_me.sin_port = htons(recv_port);
   si_me.sin_addr.s_addr = htonl(INADDR_ANY);

   //bind socket to port
   if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
   {
     close(s);
     return 0;
     exit(1);
   } else{

    //clear the buffer by filling null, it might have previously received data
    //memset(buf,'[=10=]', BUFLEN);

    for (i=0; i<NPACK; i++) {
     if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)==-1)
      {
        close(s);
        return 0;
       } else {
        //Convert buf into usable host format
        //buf_h = ntohs(buf);

        // Convert data from string to double
        //data = atof(buf);

        int N_DATA = sizeof(buf)/sizeof(buf[0]);

        for (j = 0; j < N_DATA; j++){
        data[j] = (double)be64toh(((uint64_t *)buf)[j]);
        }

        close(s);
        return data;
      } 

    }

  }

 }

}

当我运行外部块时,模型显示运行ning,但T=0.000一直。我现在连模型都停不下来了

希望得到您的帮助。 谢谢大家!

./udp_send_receive.c:14: warning: incompatible implicit declaration of built-in function 'exit'

你不见了

#include <stdlib.h>

./udp_send_receive.c:31: warning: incompatible implicit declaration of built-in function 'memset'

./udp_send_receive.c:66: warning: incompatible implicit declaration of built-in function 'memset'

./udp_send_receive.c:76: warning: incompatible implicit declaration of built-in function 'strlen'

你不见了

#include <string.h>

./udp_send_receive.c:44: warning: passing argument 5 of 'recvfrom' from incompatible pointer type

不确定如何解决这个问题。定义 __USE_GNU 可能会有所帮助。参见 sys/socket.h

./udp_send_receive.c:70: warning: passing argument 1 of 'inet_aton' makes pointer from integer without a cast

./udp_send_receive.c:76: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast

需要在参数声明中添加*char 是单个字符。 char * 是一个字符串。

void udp_send(char *data, char *SERVER, int PORT)