How to prevent timer from executing code between two lines?

Apparently, a TimerFcn execution can occur between any two lines. This is a major problem for me right now, as I want to push commands to a serial object and immediately query the response. However, since the timer function is also pushing commands, it can occur that between lines
fprintf(serialObj, 'Command');
reply = fscanf(serialObj);
the timer function also issues a command with fprintf to the same serial device. Consequently, the received reply is not necessarily from the command issued on the line before.
Therefore, how can I force the aforementioned two lines to execute consecutively, without the possibility of the timer function executing code between them?

 Accepted Answer

Write an explicit function for the TimerFcn that checks if a flag is set. Set that flag before your fprintf here and un-set it after the fscanf. You could even use multiple flags, so the TimerFcn returns if this block is busy and after this block you simply call the TimerFcn.

3 Comments

Thank you. This would have definitely worked, but instead I wrote a simple MEX file that just runs fprintf and fscanf after each other to communicate with my serial port. Since a timer cannot run or interrupt inside a MEX execution (not to my knowledge, at least), I'm safe now.
See my own answer below for the MEX code.
Update 10 hours later
The problem seemed to be gone, but a higher sampling rate brought it back. So, it turns out that a timer can still start between fprintf and fscanf, even though they are called from inside a MEX function! So I implemented your flag suggestion and that does work of course.
It's a pity your solution didn't work. It seemed like a nicer solution than the several flags my solution requires.
Anyway, happy to help. If you have any problems, feel free to comment here again (or open another question of course). (once you un-accept your own answer, the button to accept mine should re-appear btw. accepting this one will make it easier for other people, preventing them to try the mex file first as well)
I agree that the MEX "solution" was more elegant and perhaps easier to maintain for further timer functions. But of course I need a real solution, and for that your idea is perfect.

Sign in to comment.

More Answers (2)

Update: This does not seem to actually solve the problem. See the accepted answer and comments for a solution that does work.
I simply solved the problem by using fprintf and scanf from a MEX file:
#include "mex.h"
// Used to write and immediately read from a serial port
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray prhs[]){
// fprintf the command to the serial port
mexCallMATLAB(0, 0, 2, prhs, "fprintf");
// fscanf the serial port and output to lhs
mexCallMATLAB(1, plhs, 1, prhs[0], "fscanf");
}
You can reduce the problem by literally putting the two statements on the same line:
fprintf(serialObj, 'Command'); reply = fscanf(serialObj);
I say reduce because the if whatever code you have on one line calls functions from .m or .p files (not sure about built-in) then the "between two lines" can apply with respect to the lines in the called functions.

1 Comment

I believe fscanf is overloaded by the serial object (you can see this by stepping into an fscanf call on a serial object) and is implemented at the end with a Java fscanf call. Between all lines of this implementation, I guess another timer funcion call can occur, so putting fprintf and fscanf on the same line will likely not solve the problem.

Sign in to comment.

Categories

Find more on Programming Utilities in Help Center and File Exchange

Asked:

on 17 Jan 2018

Edited:

on 18 Jan 2018

Community Treasure Hunt

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

Start Hunting!