필터 지우기
필터 지우기

In simulink, how can use the UDP receive information from another Linux target.

조회 수: 4 (최근 30일)
君明 张
君明 张 2022년 8월 3일
댓글: 君明 张 2022년 8월 24일
I have a Linux c++ code, it can work well with simulink instrumen UDP send toolbox, I can receive the data from the matlab. But when I send my data to the matlab, it can't work, does anyone can help me ?Thank you !
#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define BUFFER_SIZE 1024 // buf size
int main(int argc, char *argv[])
{
struct sockaddr_in serv_addr;
double recv_buf[1024]={0};
int ret,sock_fd;
socklen_t addr_len;
// create two-way transmission socket
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
perror("error");
// initialize address
memset(&serv_addr, 0, sizeof(struct sockaddr_in));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5002);
serv_addr.sin_addr.s_addr = inet_addr("192.168.1.101");
// // bind the local port
// if ((bind(sock_fd, (struct sockaddr *)&serv_addr,
// sizeof(struct sockaddr_in))) < 0)
// {
// perror("bind");
// return -1;
// }
double u[3]={1,2,3};
while(1)
{
addr_len = sizeof(struct sockaddr_in);
if((ret=recvfrom(sock_fd, recv_buf, sizeof(recv_buf),0, (struct sockaddr *)&serv_addr, &addr_len))<0)
return -1;
for(int i=0;i<7;i++)
std::cout<<recv_buf[i]<<std::endl;
if((sendto(sock_fd,u,sizeof(u),0, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr_in)))<0)
return -1;
}
return 0;
}
  댓글 수: 2
君明 张
君明 张 2022년 8월 3일
And I have try to set the matlab as the server, but it does't work, and it cann't receive information form matlab.
君明 张
君明 张 2022년 8월 24일
I know why now, in simulink, it doesn't like a application. In actually, it's real two application. So I change the udp send code, I write the IP&port, it can work now.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by