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
Walter Roberson
2016년 1월 28일
I suspect the flushinput() should go after the fopen()
Dominic
2016년 1월 29일
편집: Walter Roberson
2016년 1월 29일
답변 (2개)
Walter Roberson
2016년 1월 29일
편집: Walter Roberson
2017년 1월 26일
0 개 추천
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.
Greg
2017년 1월 26일
0 개 추천
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
Duijnhouwer
2019년 2월 3일
편집: Duijnhouwer
2019년 2월 3일
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...
카테고리
도움말 센터 및 File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!