How to use real time workshop to convert the matlab functions' fread" and "fwrite" into c code?

Asked by David on 27 Jun 2013 at 22:50
Latest activity Commented on by Ryan Livingston about 7 hours ago

Hi,

I was wondering if there was a way to convert the Matlab functions fread and fwrite to C code using Real time Workshop or Matlab Coder. I saw in the "Matlab Coder" user guide that fread and fwrite are not supported functions that can be converted to C code. I was wondering if there was a way around this. Is there another function that does the same thing as fread and fwrite that is supported by Real Time Workshop/Matlab Coder. My bigger goal is to run Matlab in real time. I realize I can only do this by first converting my Matlab code to C code. However,I cannot convert the Matlab functions fread and fwrite to C using Real Time Workshop. (FYI: My matlab code is reading and writing to the COM port.)

Thanks in advance, David

0 Comments

David

1 Answer

Answer by Walter Roberson on 27 Jun 2013 at 23:29

I have never used those, but my understanding is that you can change the call names (e.g, fread to FREAD), and coder.extrinsic('FREAD'), and then you supply minor interface code along the lines of

#include <stdio.h>
int FREAD(.... prototype here ....) {
  return fread(....)
}

except that you would need to do whatever is necessary to deal with an array (e.g., passing in a buffer and buffer size, reading the data to the buffer, returning the fread() status.)

1 Comment

Ryan Livingston about 7 hours ago

Actually, coder.extrinsic(<fcn_name>) causes calls to the function 'fcn_name' to be made by calling out to MATLAB via MEX. Therefore, it will introduce calls to MATLAB via the MEX API. Such calls must be able to be eliminated before generating standalone C code for generating an EXE, DLL or LIB.

You can use

 coder.ceval

to call C functions from your MATLAB code. There is an example of calling C's FREAD here:

http://www.mathworks.com/products/matlab-coder/examples.html?file=/products/demos/shipping/coder/coderdemo_readfile.html

which uses coder.ceval() to do so.

Walter Roberson

Contact us