0

I am trying to create a simulink block using c-code for my EV3 to send the measured value from its sensors and/or receive datas from other hardware (raspberry pi) via UDP Pakets. However I can't find any concrete example in internet. I tried to write my own code following the example from https://www.abc.se/~m6695/udp.html. I expected it to work, since EV3 is a linux system. However, it is not working.

The c-code library of the udp_receiver (updated):

#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,'\0', 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;
      } 

    }

  }

 }

}

When I run the block externally, the model is shown running, but T=0.000 all the time. I can't even stop the model now.

Hope to get help from you. Thanks guys!

4

1 回答 1

0

./udp_send_receive.c:14:警告:内置函数“退出”的隐式声明不兼容

你不见了

#include <stdlib.h>

./udp_send_receive.c:31:警告:内置函数“memset”的隐式声明不兼容

./udp_send_receive.c:66:警告:内置函数“memset”的隐式声明不兼容

./udp_send_receive.c:76:警告:内置函数“strlen”的隐式声明不兼容

你不见了

#include <string.h>

./udp_send_receive.c:44:警告:从不兼容的指针类型传递“recvfrom”的参数 5

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

./udp_send_receive.c:70:警告:传递 'inet_aton' 的参数 1 使指针从整数而不进行强制转换

./udp_send_receive.c:76:警告:传递“strlen”的参数 1 使指针从整数而不进行强制转换

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

void udp_send(char *data, char *SERVER, int PORT)
于 2015-11-18T20:13:08.303 回答