UDP connection in two instances of Matlab

3 views (last 30 days)
Hi, I'm trying to sendi information between 2 instances of Matlab running at the same time. I've been able to use UDP in the past, but this doesn't seem to work.
% I would be sending info through instance A (integers with varying sizes each time, but only one integer at a time)
u = udp('192.168.1.1', 2011, 'LocalPort', 2012);
fopen(u);
data = 1000;
fwrite(u, data);
And then waiting for the data to arrive using another instance of matlab:
% and receive data on instance B
u = udp('192.168.1.1', 2012, 'LocalPort', 2011);
fopen(u);
signal = [];
while isempty(signal) == 1 % pooling until signal is received
signal = fscanf(u,'%d');
end
What am I missing?
I would also ideally need to send a message from the first instance to be read by the second instance later.
So Instace A would give me the number I need and send it as soon as it gets it. Meanwhile, instance B might be doing another operation and not able to receive the data instantly, but with a delay.
I have already made sure the ports are not block by the windows firewall udp tcp/ip connection.
How can we do that?
Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 17 May 2019
You are fwrite() of the data, which writes binary. You are fscanf() of the data, which expects text.
fprintf(u, '%d\n', data);
  3 Comments
Walter Roberson
Walter Roberson on 20 May 2019
Tested code attached.
Note: this is for use talking from a system to itself.
Mateo Leganes Fonteneau
Mateo Leganes Fonteneau on 20 May 2019
Thank you. Indeed, the remote host was the problem after fixing fprintf.
Seeting RemoteHost to '127.0.0.1' works for UDP. Remotehost as '192.168.1.1' was working for TCP/IP but not for UDP.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!