简体   繁体   中英

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

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!

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

You are missing

#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'

You are missing

#include <string.h>

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

Not exactly sure how to fix this one. Defining __USE_GNU may help. See 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

Need to add * to parameter declarations. char is a single character. char * is a string.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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