How to define input parameters for BytesAvailableFcn (tcpclient)

10 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jan 2023
Edited: Walter Roberson on 17 Jan 2023
  6 Comments
Walter Roberson
Walter Roberson on 28 Sep 2024
Edited: Walter Roberson on 29 Sep 2024
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
Nitin Kumar on 29 Sep 2024

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.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!