【发布时间】:2015-11-18 16:39:57
【问题描述】:
我正在尝试使用我的 EV3 的 c 代码创建一个 simulink 块,以通过 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 一直。我现在什至无法停止模型。
希望能得到你的帮助。 谢谢大家!
【问题讨论】:
-
您是在 EV3 上使用 LEGO Group 的标准固件还是使用第 3 方操作系统?另外,您的两个代码示例看起来都在接收数据,在传输什么?你是在EV3上编译安装c代码吗?
-
你好大卫,我使用的是乐高集团的标准固件。是的,我正在编写代码来接收数据。基本上我想使用该代码接收所有类型的数据,尤其是来自其他硬件的测量数据。现在,我正在从我的树莓派发送一个无限正弦信号,并希望 EV3 能够接收到该信号。 c 代码与 simulink 模型一起编译到 EV3 上,并由 matlab 功能块在那里执行。
-
我刚刚上传了我在上述问题的编译过程中收到的警告。希望你能帮助我,谢谢:)
标签: c matlab udp simulink mindstorms