How to define input parameters for BytesAvailableFcn (tcpclient)

10 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 16 de En. de 2023
Editada: Walter Roberson el 17 de En. de 2023
  6 comentarios
Walter Roberson
Walter Roberson el 28 de Sept. de 2024
Editada: Walter Roberson el 29 de Sept. de 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 el 29 de Sept. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by