How to speed up UDP connections?

7 views (last 30 days)
Stefan
Stefan on 29 Oct 2012
I have to different versions of Matlab (6.5 and 7.11) communicating with each other. I use UDP as protocol. But it's very slow. Just to test the whole communication thing, I created to Simulations. Sim1 adds 5 to the incoming value, Sim2 subtracts 3. So Sim1 gets an initial value (in this case 1), calculates, gives the result to Sim2, where the same thing happens and so on. I want to calculate for 100 steps and it takes about 15s! Which is too long, cause in the real simulation I have ~400000 steps. How can I optimize the whole thing? Any ideas?
remotehost = '127.0.0.1';
remoteport = 10000;
u = udp(remotehost,remoteport,'LocalPort',10001);
fopen(u);
sample_time=0.1;
A=[1 1];
for sample=1:100
if isempty(A)
A=fread(u,2,'double');
end
num_ele=length(A);
for i=1:num_ele/2
udp_in(i,1)=A(2*i-1);
udp_in(i,2)=A(2*i);
end
sim('sim1',sample*sample_time)
A=[];
l2r=[sample*sample_time,udp_out(1)];
fwrite(u,l2r,'double')
end
fclose(u);
delete(u);
clear u;
  5 Comments
Stefan
Stefan on 31 Oct 2012
Edited: Stefan on 31 Oct 2012
Yes, sometimes I get:
Warning: The specified amount of data was not returned
within the Timeout period.
How can I configure for reading packet-by-packet and what does it mean?
Stefan
Stefan on 6 Nov 2012
Edited: Stefan on 6 Nov 2012
I stil have problems with the speed. I use the following two codes:
u = udp('localhost', 5003, 'LocalPort', 5002);
u.Timeout = inf;
u.DatagramTerminateMode = 'off';
u.InputBuffersize = 4096;
u.OutputBuffersize = 4096;
fopen(u);
sample_time = 0.02;
pos = [1;7];
vel = [1;0];
tic
for sample=0:100
pos = pos + (vel * 1);
%fwrite(u, [pos;0], 'float');
fwrite(u, pos, 'float');
vel = fread(u, [2,1], 'float');
end
toc
disp(sprintf('%0.2f: Pos[%03d,%03d], Vel[%03d,%03d]',...
sample * sample_time,...
pos(1,1), pos(2,1),...
vel(1,1), vel(2,1)...
));
fclose(u);
delete(u);
clear all;
and
u = udp('localhost', 5002, 'LocalPort', 5003);
u.Timeout = inf;
u.DatagramTerminateMode = 'off';
u.InputBuffersize = 4096;
u.OutputBuffersize = 4096;
fopen(u);
sample_time = 0.02;
vel = [];
for sample=0:100
pos = fread(u, [2,1], 'float');
fprintf('Read: [%d %d]\n', pos);
vel = [1;2] + [sample;sample];
%fwrite(u, [vel;0], 'float');
fwrite(u, vel, 'float');
end
fclose(u);
delete(u);
clear all;
But I figured out some strange things: The code written above will take around 12sec (which is too long). If you use the fwrite command with [pos;0] instead of pos the duration will be decreased to around 1sec! But the results are wrong :( Which makes sense, because Matlab will use the zero at the third position as a value for the next timestep. But this shows, that the communcation can be faster. Any ideas how to deal with that?

Sign in to comment.

Answers (0)

Categories

Find more on Beamforming and Direction of Arrival Estimation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!