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

9 views (last 30 days)
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 Comments
君明 张
君明 张 on 3 Aug 2022
And I have try to set the matlab as the server, but it does't work, and it cann't receive information form matlab.
君明 张
君明 张 on 24 Aug 2022
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.

Sign in to comment.

Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!