Callback function for UDP set with DatagramReceivedFcn is invoking only for one time?

After the first execution of UDP callback function system gives a warning message of "Warning: The DatagramReceivedFcn is being disabled. To enable the callback property either connect to the hardware with FOPEN or set the DatagramReceivedFcn property." in command window. And the callback function does not invoke again. Code for setting the UDP object is as follows :
global objudp1 %%%%global variable for udp object
strClientIP=get(handles.edClientIPAddr,'string');
objudp1=udp(strClientIP,2000,'LocalPort',2500);
objudp1.ReadAsyncMode = 'continuous';
objudp1.DatagramReceivedFcn ={@OnDataReceivedOnUdp1};%%%,handles};
objudp1.DatagramTerminateMode='On';
flushinput(objudp1);
fopen(objudp1);

2 Comments

I suspect the flushinput() should go after the fopen()
Thanks.
Now I changed the code as follows :
/*******************************************/
global objudp1 %%%%global variable for udp object
strClientIP=get(handles.edClientIPAddr,'string');
objudp1=udp(strClientIP,2000,'LocalPort',2500);
objudp1.ReadAsyncMode = 'continuous';
objudp1.DatagramReceivedFcn ={@OnDataReceivedOnUdp1};%%%,handles};
objudp1.DatagramTerminateMode='On';
fopen(objudp1);
flushinput(objudp1);
/*******************************************/
But still the problem exists.

Sign in to comment.

Answers (2)

Looking at http://www.mathworks.com/matlabcentral/newsreader/view_thread/294903 I see a hint that the bit about the callback being disabled might be due to the connection breaking from the remote end (which would be an unusual thing for udp), or due to the udp object being destroyed or going out of scope.
Ah, and suppose the LocalPort is already in use, then the fopen() would not do what you want and the callback would probably disable itself.
That warning is almost always due to an error in your DatagramReceivedFcn code. This thread is a year old, so I doubt the original poster will come back to it, but for anyone still having this issue: debug your DatagramReceivedFcn for code errors.

1 Comment

This was my problem, thanks!
Specifically, I was using DatagramReceivedFcn in a object oriented context, so my handle was
O.udp_connection.DatagramReceivedFcn = @O.parse_message;
The parse_message method needs 3 arguments then, like
classdef test < handle
properties
u;
end
methods
function O=test
O.u=udp('127.0.0.1','RemotePort',5050,'LocalPort',5051);
O.u.DatagramReceivedFcn = @O.parse_message;
fopen(O.u);
end
function parse_message(O,~,~)
msg=fscanf(O.udp_connection)
% parsing msg here....
end
end
end
If it doens't have three methods the OP's error message is thrown before even entering parse_message, which precluded waiting there with a breakpoint to debug the body...

Sign in to comment.

Products

Asked:

on 28 Jan 2016

Edited:

on 3 Feb 2019

Community Treasure Hunt

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

Start Hunting!