How to speed up UDP connections?

11 次查看(过去 30 天)
Stefan
Stefan 2012-10-29
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 个评论
Stefan
Stefan 2012-10-31
编辑:Stefan 2012-10-31
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 2012-11-6
编辑:Stefan 2012-11-6
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?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Beamforming and Direction of Arrival Estimation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by