I am trying to process Data from an UDP Server close to realtime.
For that I wrote this MatLab Code to fill a Buffer with the UDP Datagrams and Process the Data (split the strings, etc.) once the Buffer from my MatLab function is full (myBuffer).
During Processing the data (which takes about 0.9s) I need to go on receiving Data and store them in the (now) emptied Buffer.
I found the parfeval-Function of the "Parallel Computing Toolbox" which might suit my needs as I need my function "ProcessData" to run in the background.
The Problem I encountered is that I can't make it run as the parfeval function doesn't enter my function ProcessData. I tested it with setting a breakpoint in ProcessData() but the program never stops. Did I do anything wrong with the function parameters?
That's what MatLab help says: F = parfeval(p,fcn,numout,in1,in2,...) requests asynchronous execution of the function fcn on a worker contained in the parallel pool p, expecting numout output arguments and supplying as input arguments in1,in2,....
Hope you guys can help me with this problem! Thanks in advance.
function ReadVoltage
u = udp('192.168.0.164', 8080);
myBuffer = {};
MAXBUFFLEN = 100;
u.InputBufferSize = 4060;
u.ReadAsyncMode = 'continuous';
u.DatagramReceivedFcn = @DatagramReceivedFcn;
u.ErrorFcn = @ErrorFcn;
u.DatagramTerminateMode =
u.Terminator = '!';
pool = gcp();
fopen(u);
if (~strcmp(u.Status,'open'))
NetworkError(u,'Connection failed!');
end
fprintf(u, 'Requesting Data')
function DatagramReceivedFcn(u,~)
datagram = fscanf(u);
disp('Data Received!');
myBuffer{end+1} = datagram;
[~, bufflen] = size(myBuffer);
if bufflen < MAXBUFFLEN
return;
else
f = parfeval(pool, @ProcessData, 1, myBuffer);
myBuffer = {};
end
end
function ErrorFcn(u,~)
disp("An Error occured");
end
end
function datagram_values = ProcessData(myBuffer)
stringvalues = split(myBuffer, ";");
doublevalues = str2double(stringvalues)
dim_doublevalues = size(doublevalues);
i_max = dim_doublevalues(2)
j_max = (dim_doublevalues(3))-1
k_max = i_max*j_max
k=1;
while k<=k_max
for i = 1:i_max
for j = 1:j_max
datagram_values(k,1)=doublevalues(1,i,j);
k=k+1;
end
end
end
disp(datagram_values);
end