sending data from Matlab to C++ over UDP
Show older comments
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!
Accepted Answer
More Answers (1)
kuan jiang
on 27 May 2019
0 votes
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 Comments
Yuval Porat
on 27 May 2019
kuan jiang
on 28 May 2019
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.
Categories
Find more on MATLAB Compiler SDK 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!