gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:08:12 26 4
gpt4 key购买 nike

我正在尝试使用 c 代码为我的 EV3 创建一个 simulink block ,以从其传感器发送测量值和/或通过 UDP 数据包从其他硬件(树莓派)接收数据。但是我在互联网上找不到任何具体的例子。我尝试按照 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,'\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;
}

}

}

}

}

当我在外部运行该模块时,模型显示正在运行,但 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)

关于c - 如何使用 Simulink 外部模式在 Lego Mindstorms EV3 上发送和接收 UDP 数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33785571/

26 4 0
文章推荐: c - 用 C 语言制作问答游戏
文章推荐: c# - Microsoft.AspNet.SignalR : Getting Uncaught TypeError: Object # has no method 'Logon'
文章推荐: c - 泛化函数指针