MATLAB: Using printf/scanf in External Mode Simulation

Embedded Coder

I am generating an executable from my model for External Mode simulation that I will run on a target machine and connect to from my host computer. The executable has "printf" and "scanf" statements included in it to monitor its error status and provide yes/no inputs, for example:
1) “X is doing Y”
2) “Do you accept [y/N]:"
The documentation states that "printf" and "scanf" statements are supported for External Mode simulation:
"External mode simulations support the use of printf calls to display error and information messages from the target program."
However, these messages are displayed in the console on the target. Right now, we have to SSH to the console on the target to interact with the "printf" and "scanf" statements. Is there a way for the input/output of these statements to show up in Simulink on the host computer?

Best Answer

  • There is unfortunately not a way to interact with printf/scanf statements directly from Simulink. These functions are supported in the generated code, but they only provide interaction through the console where the executable is running.
    You can still interact with the External mode execution directly from Simulink using tunable parameters, dashboard blocks, and instrumentation blocks (e.g. displays, scopes, to workspace, etc.). See the supported features here:
    How you choose to use these blocks depends on your use case. For your application, it sounds like a viable approach would be to have a "status" output from the S-function blocks, which might be a code that corresponds to "X is doing Y". You may also consider using a Manual Switch block for "y/n" inputs.
    A different approach you may consider is printing output to a file instead of printing to the console. However, this may not satisfy your use case if you want to deploy to a separate target (i.e. not running the executable on the host).
    As a final workaround you may connect to the console running the executable via SSH, as mentioned.