Hello,
I have a problem receiving UDP packages from the network using Matlab. The UDP packages are send from another pc continuously. First of all, in Simulink it works fine, but I need it to work in Matlab code due to the use of data to update an existing figure.
*I use a timer object to receive the UDP packages *
fs=100;
handles.t=timer('StartDelay',1,'Period',1/fs,'TasksToExecute',1000,'ExecutionMode','fixedRate','busymode','error');
handles.t.StartFcn={@my_start_fcn,'My start message'};
handles.t.StopFcn= {@my_stop_fcn,'My stop message'};
handles.t.TimerFcn={@TimerActionTest};
handles.t.ErrorFcn={@my_callback_fcn,'My error message'};
TP.fid = udp('192.168.14.10','localport',27123);
set(TP.fid,'InputBufferSize',24,'OutputBufferSize',24);
fopen(TP.fid);
start(handles.t)
waitfor(handles.t)
fclose(TP.fid);
*The TimerAction code: *
function trialProgress = TimerAction(obj,event)
iData=fread(TP.fid,24);
iData=typecast(uint8(iData),'single');
TP.cursorPos=iData(2);
The problem is, in Simulink this works realtime, but in Matlab the data is about 4-5 seconds behind. It seems to me like a buffer that I have to reset, or that I have to choose for "take the last UDP package" instead of the oldest one in the buffer.
Does anyone have an idea? Or another suggestion for receiving the UDP packages in a quicker (realtime) way?
Thanks in advance.
**One option that worked for real time receiving was positioning fopen and fclose inside the TimerAction, but then the timer could not keep up the 100Hz.