scanf/printf not working when calling generated C code

36 views (last 30 days)
Victor Lam
Victor Lam on 23 Feb 2026 at 11:36
Edited: dpb on 23 Feb 2026 at 21:39
In Matlab I have generated C code (.c/.h files) from a Matlab function using codegen from Matlab Coder.
I call this function in a while loop on an ARM quad-core Cortex A53 using AMD Vitis.
Before calling this function I use scanf/printf to get user input and to print to the serial monitor. However, scanf/printf only work once in the 1st iteration (before calling the function) and not in the 2nd iteration or later (after calling the function).
Is this a common problem and has anyone encountered this problem before?
I think, but am not sure, that the problem is in the generated C code and not in Vitis itself.
Thank you in advance!
  3 Comments
Victor Lam
Victor Lam 22 minutes ago
Let's say the sequence of one iteration is: scanf + printf + function. In the 1st iteration it asks for user input and prints a message, but then the while loop runs forever without ever asking for user input or printing anything again in the 2nd iteration or later.
But if I comment out this function then in every iteration it always asks for user input and prints a message, as one would normally expect.
I used debugger mode to verify that the function has terminated and is not stuck. I also verified for specific inputs that the output of the function in C is identical to the Matlab function.
Does this behaviour occur more often or has anyone seen something like this before?
dpb
dpb about 6 hours ago
Edited: dpb 10 minutes ago
Probably no one has ever done precisely the same thing with the combination of environment and hardware so it's extremely likely the answer to that question is "No".
Knowing nothing of what the function does nor anything about the system you're running, it's unlikely anybody here can help.
If not calling the function removes the symptoms, then it is conclusive that something in either the calling/return linkage is broken or the function itself did something damaging outside returning its expected output(s). One next debugging step would be to create an empty null function in its stead and test calling it. If that is successful, then add the argument list alone, then the output arguments until find what step causes something to break. You could also try replacing the function call with something simple like timestwo.c and make sure the code generator can handle the simple case first. It's always possible there is something unique in the development environment it doesn't know about -- maybe a default convention assumed isn't the same as expected.
"... I use scanf/printf to get user input and to print to the serial monitor"
I would suspect that the serial connection got broken would be a fairly likely possibility...
If that doesn't uncover the problem, you'll probably have to submit the whole enchilada to Mathworks suppor.

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!