.netmatlabsimulinkosisoft

Simulink .NET integration


I am working on a project where I need to integrate a certain PI-AF (database) with a Simulink model. I know that PI-AF has a certain .NET AF-SDK api through which I can reference/access the elements/attributes. And I also know that you can load a .NET assembly into MATLAB and use the .NET libraries. However, can we do the same in Simulink ? I know you can create a function block in Simulink where you can write MATLAB functions that would be executed with the given inputs and also return a certain value. Is it possible to load a .NET assembly in this function block? I am completely new to Simulink and MATLAB and have never really worked on them before. It would be great if one of you guys can share your knowledge on the above.

Thanks.


Solution

  • It depends what sort of integration you are after:

    If you just want to load some data from the database, run your Simulink model using that data, and then maybe write some data back onto the database once the simulation is finished, you don't need to the MATLAB Function block you refer to. Simply write your MATLAB code (as scripts or functions, doesn't matter), and call those scripts/functions in the model callbacks: probably in the PreLoadFcn, PostLoadFcn or InitFcn callback for the code to load the data from the database, and in the StopFcn for the code to write the data back to the database.

    If, on the other hand, you want to continually interact with the database as the simulation progresses, then it's more complicated and you do need to use the MATLAB Function block, as you suggested. That block uses (C) code generation under the hood, which I suspect the .NET related functions won't support, so you'll have to define them as extrinsic (see Call MATLAB Functions in the documentation for more details). Be aware though that functions declared as extrinsic return outputs of type mxArray, which need to be converted to a known type before they can be used by other functions/statements/etc... How to do this is explained in the aforementioned documentation page.