How to define input parameters for BytesAvailableFcn (tcpclient)
조회 수: 10 (최근 30일)
이전 댓글 표시
Hy all,
my problem concerns the use of input arguments when I define the BytesAvailableFcn of a tcpclient connection.
I create a tcpclient connection:
t = tcpclient('address', port);
And I send a request to the server (which starts automatically to send data):
write( t, 'request' );
I want to have a callback function triggered by a bytes available event:
configureCallback( t, "terminator", @MyCallbackFcn );
MyCallbackFcn is written in a separated MyCallbackFcn.m file. The callback function MyCallbackFcn is triggeredd whenever a terminator is available to be read from the remote host specified by the TCP/IP client.
At the moment, MyCallbackFcn uses global parameters (var1, var2, etc.) defined in the main program:
function MyCallbackFcn( src, ~ )
global var1 var2
...
end
This algorithm works well. But in order to avoid the use of global parameters, I would like to define input parameters for MyCallbackFcn fonction. My problems start here, because I do not find the right form to write:
- the definition of the callback function in the main program : configureCallback( t,...
- the function MyCallbackFcn in the MyCallbackFcn.m file
Thank you in advance for your help.
Raphaël
댓글 수: 0
채택된 답변
Walter Roberson
2023년 1월 16일
편집: Walter Roberson
2023년 1월 17일
댓글 수: 6
Walter Roberson
2024년 9월 28일
편집: Walter Roberson
2024년 9월 29일
configureCallback(t, @(src,event) MyCallbackFcn(src, event, var1, var2)
is correct syntax for configuring a callback to MyCallbackFcn passing in two extra parameters, including var1 and var2 . The var1 and var2 values would be "captured" at the time the @(src,event) was processed -- any changes to var1 or var2 made after that point in the code would have no effect on the configured callback.
Nitin Kumar
2024년 9월 29일
We need to pass the "terminator" argument. Also, a closing parenthesis is missing.
configureCallback(t, "terminator", @(src, event) MyCallbackFcn(src, event, var1, var2))
Just small things but took me some time to figure out. Thanks.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!