Simulink to a DLL to execute from an Desktop .Net Application

조회 수: 17 (최근 30일)
Timo
Timo 2011년 4월 28일
편집: Rik 2020년 9월 1일
Hello Matlabers,
Situation: in the Moment I'm working on an project, with code written in C# and an Simulink model. The C# code can controll a crane. The Simulink model measures the position of the crane. I want to join them to a Control Loop, with a PID controller in the Simulink model. I now working on the Simulink model and want to avoid the investing time in the wrong direction (If this interaction is not possible). Also I need to know which products I need so I can start the process ( ;-) ) of getting them upfront.
My Question is: How can I greate a Dll which I can call from an .Net application. And how to call it from .Net (NOT: How to call an dll in general?). Which are the limitations to the simulink model.
My Results so far: It is posible to generate c and c++ Code form an Simulink Model. The Real-Time Workshop now Simulink Coder makes this possible. But I didn't find something about the interaction between a Simulink model and normal Desktop Application.
Thanks Timo

채택된 답변

Jarrod Rivituso
Jarrod Rivituso 2011년 4월 28일
You can certainly do this with your Simulink model.
Simulink Coder (formerly Real-Time Workshop) allows you to generate C/C++ code from your Simulink model, as you know. Your options from there are
1. Integrate that code directly with your C# application and build them together. This is a common approach. It is fairly easy to get all of the Simulink dependencies using a feature called packngo:
The code generated from the Simulink model provides interface functions such as
<modelName>_initialize()
<modelName>_step()
which allow you to initialize and step through your model. It is then the C# application's responsibility to call them at the correct time.
2. Build the Simulink generated code into a shared library and then have your C# application use the shared library. There seems to be a way to do this but I must confess that I've never done it myself.
Here's a doc link for ya though. It seems to require the Embedded Coder product as well.
Hope this helps!

추가 답변 (8개)

Arnaud Miege
Arnaud Miege 2011년 4월 28일
You may want to try Embedded Coder to generate a Shared Library (DLL), but I am not sure how to interface to it from .NET.
HTH,
Arnaud

Timo
Timo 2011년 4월 28일
Hallo Arnaud and Jarrod,
Thank you. Your answers are very helpful. Now I will look deeper in the Information you told me.
Thanks and Greetings Timo

Timo
Timo 2011년 5월 2일
Hello again,
The Embedded Coder seems to be the right anwser for a Shared Libary. We only have the Real-Time Workshop 2010b. Because the Embedded Coder is only needed in this small project, we do not plan to acquire it at the moment. ;-(
So I have to search for a work around.
My Ideas:
1. I read about the external mode. Perhaps I'm able to generate a external host and connect to from C#.
2. I can try to recode the c code of a normal executeable Real-Time generation to a Shared Libary.
Did anyone try this?
Thanks for your help Timo
  댓글 수: 2
Kaustubha Govind
Kaustubha Govind 2011년 5월 2일
I think #2 is the better option, if you are willing to invest enough time. However, I've never tried that before and cannot say how viable it is.
Not sure what kind of performance is tolerable for your application, but if you have MATLAB installed on the machine that your .NET application will run, you could consider starting MATLAB as a COM server (see http://www.mathworks.com/help/techdoc/matlab_external/bq7_ykd-1.html) and communicate with Simulink using this interface. In other words, you will still be running the model within the Simulink environment.
Timo
Timo 2011년 5월 3일
Hello Kaustubha!
Thank you for your reply!
I want to translate it in c or c++ code! I need a performance boost. But nevertheless thank you for telling me abaut this option. Perhaps I can need it elsewhere.
Thanks and greeting Timo

댓글을 달려면 로그인하십시오.


Ralph Köhn
Ralph Köhn 2011년 8월 2일
Hello Timo!
Right now i got nearly the same situation beeing in need of converting a mdl to a .NET Assembly dll to run it from a C# project. At the moment we are utilizing assemblies created by the Builder NE, but that only works for m files from MatLab.
Did you allready found a good way to integrate mdl files in C# projects and know how to do this reliable?
Did your approach with translating to c++ and building a dll was successfull?
With best reguards, Ralph

Gregor
Gregor 2011년 12월 23일
Ralph,
starting to crouch down the same rabbit hole. What is your latest on using Matlab NE to use Simulink models? We successfully build an assembly of an .m file that accesses a simulink model, but we get errors in c# when instantiating the class.
Kind regards, mit freundlichen Gruessen, Gregor

Joseph
Joseph 2013년 3월 14일
Timo I'm trying to do the same thing. What I've found out is that you will need to export the Simulink model using Simulink coder to source code. Right now I'm using grt target. But this target produces a main entry point which is for EXE not for DLL. I had to modify the entry point and use __declspec (dllexport) to define my interface, then in the wrapper (C#) I had to use DllImport and declare the interface usage there. This all works but it is very hard to distinquish what I need to keep or remove from the generated grt_main.c file. So my question is this, is there a way to generated a shared library from Simulink directly?
  댓글 수: 1
Kaustubha Govind
Kaustubha Govind 2013년 3월 15일
Joseph: Yes, if you have an Embedded Coder license, you can use the Shared Library Target (ert_shrlib.tlc) to generate a shared library directly.

댓글을 달려면 로그인하십시오.


DataLars
DataLars 2019년 10월 4일
Hello,
I see that the topic is a little bit old but I hope you can help me.
How is it possible zu access the Values in the dll from c# ?
The initialize and step Command work.
Thanks for your help
Lars

PSK
PSK 2020년 8월 31일
Does anyone have C# code sample to use shared library (.dll) generated from Embedded coder.
In our model we have some inputs and output (which are extern variables as Struct in the shared lib along with model methods) which we are able to marshal as C# struct but looks like when we set the inputs struct from C# application they are not being set into the model as output is not changing.
Looking for C# code to set inputs, initialize model, step into the model, use the model output, and terminate the model.
  댓글 수: 2
Rik
Rik 2020년 8월 31일
This is not an answer but a question. The chances of getting an answer this way are fairly slim. You should post your own question and explain what you have tried yourself and where you searched for a solution.
PSK
PSK 2020년 8월 31일
편집: Rik 2020년 9월 1일

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by