Error Running Simulink in External Mode on Raspberry Pi

10 views (last 30 days)
Hi everyone, I've been trying to deploy some standalone code from Simulink on a Raspberry Pi 4B but I am having some issues. My application requires the generated code to read from a file within the directory, and also write to a file within the directory on the Raspberry Pi. There doesn't appear to be any blocks that can do this, so I wrote some code in a Matlab Function block with some guidance from one of the examples that came with the toolbox. My model builds and is sent to the device no issues, but there are two errors that occur at Run Time and I haven't found any useful solutions to the issues in the documentation or online anywherer yet. I tried running it in externel mode with "Monitor and Tune", as well as the "Build, Deploy, and Start".
Right now I am trying to make a very basic model run, It has a Matlab Function block for reading a file, a Matlab Function block for writing to a file, and one display block for when I was trying to run it in Externel mode. Here are the codes:
Read Function:
function output = ReadFromFile()
%#codegen
% Include the C header stdio.h
coder.cinclude('stdio.h');
% Define the file name
filename = 'SimulinkInputTest.txt';
% Open the file for reading
fid = coder.opaque('FILE *');
fid = coder.ceval('fopen', filename, ['r', 0]);
% Read the number from the file
output = coder.nullcopy(0.0);
coder.ceval('fscanf', fid, coder.const('%f'), coder.wref(output));
% Close the file
coder.ceval('fclose', fid);
end
Write Function:
function appendToTextFile(value)
%#codegen
% Include the C header stdio.h
coder.cinclude('stdio.h');
% Define the file name (you can specify any file name)
filename = 'SimulinkOutputTest.txt';
% Open the file in append mode
fd = coder.opaque('FILE *');
fd = coder.ceval('fopen', filename, ['a', 0]);
% Write the value to the file
coder.ceval('fprintf', fd, '%g\n', value);
% Close the file
coder.ceval('fclose', fd);
end
And this is a picture of the Model:
And here is the error:
Error occurred while executing External Mode MEX-file 'ext_comm': ExtTargetPktPending() call failed while checking for target pkt
Component:Simulink | Category:Block diagram error
Error occurred while executing External Mode MEX-file 'ext_comm': ExtSetTargetPkt() call failed on CLOSE. Ensure target is still running
Component:Simulink | Category:Block diagram error.
I am connected to the Pi with an ethernet cable.
I am not sure how to fix this error and get this very basic example working. Thanks for the help in advance.

Accepted Answer

Dinesh
Dinesh on 21 Sep 2023
Hi Kyle,
I understand that you are currently trying to deploy standalone code onto a Raspberry Pi, but you are encountering an error message: 'ext_comm': ExtTargetPktPending() call failed.
This error typically occurs when Simulink fails to receive acknowledgment from the target, indicating a loss of communication between Simulink and the board. One possible reason for this could be high communication bandwidth usage or running out of memory, resulting in temporary communication loss during runtime in external mode simulation.
Please refer to the following MATLAB Answers thread for suggestions on how to deal this issue
Hope this helps.
Best Regards,
Dinesh Reddy Gatla.
  1 Comment
Kyle
Kyle on 21 Sep 2023
Thanks for the answer Dinesh. The issue ended up being a coding error in one of my matlab function blocks. The only explination I could come up with was that the funciton block was trying to execute a line of code in the read function that was causing the entire sim to hang until it eventually hit some timeout (I believe set to 10 seconds in the settings).
I say this because once I changed my read function slightly, the model executed perfectly. I wasn't aware that fopen, fclose, and fprintf were all supported for code generation already so coder.ceval was redundant and actually causing issues.
Thanks again for the answer, I did explore that as a possible solution when I was trying to solve this error.

Sign in to comment.

More Answers (0)

Categories

Find more on Raspberry Pi Hardware 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!