Borrar filtros
Borrar filtros

When exactly does BytesAvailableFcn run?

2 visualizaciones (últimos 30 días)
Theo Husby
Theo Husby el 16 de Jun. de 2023
Respondida: Walter Roberson el 18 de Jun. de 2023
I'm writing a GUI that handles asynchronous communication between MATLAB and a serial device, and am using the serialport class's BytesAvailableFcn callback to handle incoming messages. I have a basic question about its behaviour.
Let's say the callback is configured to run every time a byte is received. If Matlab receives a byte while it is executing another function or stack of functions, does the callback run immediately (asynchronously) and interrupt the other function, or does the BytesAvailableFcn callback get queued up to run after the other functions have completed?
  3 comentarios
Kautuk Raj
Kautuk Raj el 18 de Jun. de 2023
When using the serialport class's BytesAvailableFcn callback in MATLAB, the callback is executed asynchronously. However, it does not interrupt other functions that are currently executing. Instead, it gets queued to run after the currently executing function (or stack of functions) has completed.
Walter Roberson
Walter Roberson el 18 de Jun. de 2023
Are you saying that BytesAvailableFcn are only executed when MATLAB returns to the command line, either because there is nothing left to do or because a breakpoint or keyboard command has been executed? Or are you saying that BytesAvailableFcn only executes when MATLAB returns to the command line because there is nothing left to do (since that is the only condition under which the currently executing stack of functions has completed.)

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 18 de Jun. de 2023
Timer functions are (somewhere) documented as executing at the beginning of the next line of MATLAB code that executes. I would not be surprised if BytesAvailableFcn are executed under the same circumstances.
To clarify, if you had something like
A(); B(); C();
D();
and the timer fired while B() was executing, the timer would not deliberately be serviced as soon as B completed -- the rule is not that the timer executes at the end of the current "statement" . For the sake of discussion, assume for a second that B is something built-in or is a call to something external that is not internally coded in MATLAB. So C would start executing. If C is built-in/external there would be no opportunity for the interrupt to be serviced before the end of the line. But if C is itself coded in MATLAB, then inside C there would be multiple code lines, and at the beginning of the first of those to be executed, the interrupt could be serviced. The service would be at the beginning of the very next time that MATLAB is at the beginning of a line of code at any level
I suspect BytesAvailableFcn is executed under the same circumstances.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by