MATLAB Answers

sending data from Matlab to C++ over UDP

조회 수: 25(최근 30일)
Yuval Porat
Yuval Porat 2018년 7월 20일
댓글: kuan jiang 2019년 5월 28일
Hi all,
I'm trying to send data back and forth between a Matlab app and a C++ app. The data is comprised of a 100 char array. Both apps are on the same computer. The Matlab app in this case is the Server and the C++ is the client.
I am able to receive data properly in the direction from my C++ app to Matlab. However I am not able to receive the data in the opposite direction from Matlab to my C++ app, though I'm using exactly the same socket.
I checked my C++ app, by using a second C++ application that talks to the first one through the same UDP interface and the data was transmitted back and forth properly.
Just for testing the connection, I'm trying to send a 100 char array with 'A' in the first cell from C++ to Matlab, and return a 100 char array with 'I' in the first cell from Matlab to C++:
CPP_IP = '127.0.0.1';
CPP_Port = 817;
OpenCPP = udp(CPP_IP,CPP_Port,'LocalPort',CPP_Port);
OpenCPP.InputBufferSize = 100;
OpenCPP.OutputBufferSize = 100;
OpenCPP.ByteOrder = 'littleEndian';
OpenCPP.Timeout = 30;
fopen(OpenCPP);
CPP_Connection = 0;
while ~CPP_Connection
fprintf('waiting for CPP to come alive...')
msg = fscanf(OpenCPP,'%c',[100 1]);
if OpenCPP.ValuesReceived == 100
if msg(1) == 'A'
fprintf('\nCPP alive and connected!\n')
CPP_Connection = 1;
end
else
fprintf('Command from CPP incomplete!')
fclose(OpenCPP);
delete(OpenCPP);
clear OpenCPP
end
end
end
msg(1) = 'I';
fprintf(OpenCPP,'%c',msg);
When running this code and my C++ app, if I check the connection properties after receiving the letter 'A', this is what I get:
UDP Object : UDP-127.0.0.1
Communication Settings
RemotePort: 817
RemoteHost: 127.0.0.1
Terminator: 'LF'
Communication State
Status: open
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 100
ValuesReceived: 100
ValuesSent: 200
I received 100 chars as expected and sent 100 chars. It looks like the sent data is not actually read by the C++, although as I said, the interface on the C++ app was checked with another C++ app and worked fine.
It seems like an issue in the communication between Matlab and C++
Any help would be highly appreciated!

채택된 답변

Yuval Porat
Yuval Porat 2018년 7월 20일
Never mind. I realized what went wrong.
Though declaring the IP and Port of the socket at the beginning, the port in which data was sent from my C++ app to Matlab was randomly selected. I fixed it by reading in the new incoming port in fscanf:
[Buff,count,msg,datagramaddress,datagramport] = fscanf(OpenCPP,'%c',[100 1]);
Then, changing the port of my CPP connection to the new one using:
OpenCPP.RemotePort = datagramport;
and writing my data using fprintf:
fprintf(OpenCPP,'%c',Buff);

추가 답변(1개)

kuan jiang
kuan jiang 2019년 5월 27일
hello, i'm trying to make communnication between c++ app and matlab like you did,could you tell me how to realize it
thanks a lot.
  댓글 수: 2
kuan jiang
kuan jiang 2019년 5월 28일
Ah! thank you very much.Generally, i want to make co-simulation between MATLAB and ns-3 (a network simulator), and i read some papers found they using socket to combine these two simulators to realize data transfer from matlab to ns3 and back ,question is i don't know how the matlab use socket to transfer data to external software and get back data...
Thanks a lot.

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

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by