Import FMU via loadlibrary

11 views (last 30 days)
Christia
Christia on 6 Jan 2014
Edited: Christia on 15 Jan 2014
Hello I'm experimenting with different styles of Model-Exchange. I found an upcoming standard named FMU on https://www.fmi-standard.org/ which contains a dll and a xml File. I managed to parse results from the SDK to Matlab. But I would prefer, Matlab to generate the simulation of the FMUs. Now i want to write an import-function for FMUs in Matlab (I know theres are toolboxes e.g. by modelon or I could use FMI Library, but I would prefer to programm it by my own to learn more about xml Parsing and running dlls in Matlab/Simulink). Is it possible/a good idea to write a xml Parser and load the dll by loadlibrary? Then I could use the Matlab Fct Block to transfer the simulation of the FMU to Simulink. As I understood FMUs must fit to the interface defined by the fmiFunctions.h. But using this header for loalibrary didnt work for me so far. Or is there a way simpler, better or other way to import a dll (the FMU)? I would be glad about any hint!
Thanks for your hint. I read the hints in the header for fmi standard 2.0, which says: For FMUs compiled in a DLL/sharedObject, the "actual" function names are used and "FMI_FUNCTION_PREFIX" must not be defined.
But it seams one needs to define the Prefix anyways.By using the correct header which was used to generate the fmu i can call all necessary functions. Anyways ill get the warning: FMU from SDK Warning: The data type 'FcnPtr' used by structure fmiCallbackFunctions does not exist. The structure may not be usable. > In loadlibrary at 360 (1 time) > In loadlibrary at 421 (3 times)
Dymola FMU > In loadlibrary at 360 (1 time) > In loadlibrary at 421 (3 times) Warning: The data type 'FcnPtr' used by structure fmiMECallbackFunctions does not exist. The structure may not be usable.
> In loadlibrary at 421 (4 times) Warning: The data type 'FcnPtr' used by structure fmiCoSimCallbackFunctions does not exist. The structure may not be usable.
Cause Im having tons to do at the moment I cant find time to test or programm alot. When Ive more information or finished code I'll answer here. My question was more about not what to do exactly but more to ask if I'm on the right way for implementing an import tool in matlab.
I decided to generate a mexfile from the main.c (which generates the fmusim_cs.exe). For this i replaced the main fct by mexfunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]). Additionally i parsed the arguments in this fct instead of using the extern fct parseArguments. I created a mex with mex -v COMPFLAGS="$COMPFLAGS /DFMI_COSIMULATION /wd4090 /nologo" main.c sim_support.c xml_parser.c stack.c libexpatMT.lib (having all files in 1 directory). But when i want to call it [c,d,e]=main('cs','bin\pfer.fmu',12.0,0.01,0,'c') Matlab crashes and I cant find out what i did wrong. It would be very kind if you could help me.

Accepted Answer

Philip Borghesani
Philip Borghesani on 8 Jan 2014
Function pointers are not supported by loadlibrary or calllib there is no way to create one in MATLAB that can be passed to and called by a standard library.
To call functions that require the use of function pointers you will need a C mex function. It is possible to use a combination of loadlibrary and mex to interface to a library or to do all the work in mex the choice is up to you.

More Answers (1)

Philip Borghesani
Philip Borghesani on 6 Jan 2014
More information (edit your question) on what you have tried and any errors you are getting would be helpful. I will make a wild guess based on a quick look at FMU.
fmiModelFunctions.h suggests the correct way to use it is:
#define MODEL_IDENTIFIER MyModel
#include "fmiModelFunctions.h"
To do this sort of thing with with a loadlibary command make a new header containing required lines of code above and any others then load the library with: loadlibrary('mydll','myheader.h','addheader','fmiModelFunctions').

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!