sending data from Matlab to C++ over UDP

5 views (last 30 days)
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

Yuval Porat
Yuval Porat on 20 Jul 2018
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);

More Answers (1)

kuan jiang
kuan jiang on 27 May 2019
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
Yuval Porat on 27 May 2019
Hi, if you need general instructions I suggest reading the Matlab documentation on tcp/udp communications
https://de.mathworks.com/help/matlab/tcpip-communication.html
https://de.mathworks.com/help/instrument/tcp-ip-and-udp-interface.html
If you faced the same problem as mine, please read the answer I posted where I detailed the solution I found.
If you're having another problem, please be more specific and the community will try to help you.
Thanks.
kuan jiang
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.

Sign in to comment.

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!