MATLAB Answers

0

fprintf not working in Raspberry Pi when 'fid' is passed through input port in Simulink

Asked by Ricardo Borges on 28 Aug 2017
Latest activity Commented on by Ricardo Borges on 17 Dec 2017
I'm developing an application to run in Raspberry Pi 3 that logs data from I2C sensors and store the values in a file.
I've structured the application using 3 blocks:
- One block (BLOCK1) that runs in the first frame and opens the file (using fopen mode 'a') and writes a header to it; this block outputs the 'fid' to be used by the next 2 blocks
- One block (BLOCK2) that runs at every frame, that uses 'fid' returned by BLOCK1 and writes sensor data to the file
- One block (BLOCK3) that runs at the last frame, that closes the file using 'fid' returned by BLOCK1
When I run this model in Normal mode, in Windows, everything is OK. But, when I run it in External Mode, the Raspberry Pi correclty executes only the first block, i.e., it opens the file and write the header. It seems to be something to do with the 'fid' argument passing.
I tried something that worked: to have the following 2 blocks:
- The same BLOCK1 above, but it closes the file and do not return the 'fid'
- The same BLOCK2 above, but it opens the file, writes the data, and then closes the file.
I'm willing to do the 1st manner in order not to pay the overhead of opening and closing the file at every frame.
Did anyone have a similar problem?

  0 Comments

Sign in to comment.

1 Answer

Answer by Reeno Joseph on 15 Dec 2017
 Accepted Answer

This is happening because for normal simulations, file I/O is extrinsic, while for the hardware targets it is not. Fid's cannot be shared between blocks when file IO is not extrinsic.
This is documented (although a little confusing) here:
https://in.mathworks.com/help/matlab/ref/fopen.html#bvlc5pc-1
If you disable extrinsic calls, then you cannot return file identifiers created with fopen to MATLAB functions or extrinsic functions. Use these file identifiers only internally.
In effect, passing fid's between blocks is sort of returning them to MATLAB.

  1 Comment

Sign in to comment.