我正在嘗試使用c代碼爲我的EV3創建simulink塊,以便通過UDP Pakets從其傳感器發送測量值和/或從其他硬件(樹莓派)接收數據。但是我在互聯網上找不到任何具體的例子。我試圖從https://www.abc.se/~m6695/udp.html的例子中編寫自己的代碼。我預計它會起作用,因爲EV3是一個linux系統。但是,它不起作用。如何使用Simulink外部模式在Lego Mindstorms EV3上發送和接收UDP Pakets?
的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上的樂高集團的標準固件還是使用第三方操作系統?此外,您的兩個代碼示例看起來像是在接收數據,傳輸是什麼?你正在編譯和安裝EV3上的c代碼嗎? –
Hallo David,我使用的是樂高集團的標準固件。而且,我正在編寫接收數據的代碼。基本上我想要使用該代碼接收所有類型的數據,尤其是來自其他硬件的測量數據。目前,我正在從我的樹莓派發送一個無限的正弦信號,並希望EV3能夠接收信號。 C代碼正在與simulink模型一起編譯到EV3上,並由matlab功能塊在那裏執行。 – limck
我剛剛上傳了上述問題編譯期間收到的警告。希望你能幫助我,謝謝:) – limck