Simulink - C++ interface: How data transfer is done between a visual c++ program and the simulink engine
조회 수: 5 (최근 30일)
이전 댓글 표시
I am having difficulty in understanding how exactly a C mex-S function accepts external data from a C program. I need to pass data (two inputs) from an MFC application to a simulink model and then retrieve the data output( two outputs) from the model and send it back to my MFC application.
My grey area is how exactly does the data transfer process take place? If I have the value I want to transfer stored in a variable in my MFC application, how do I then call my simulink model accept this variable from my MFC application as an input, process it, and then relay this ouput data back to my MFC application?
I tried using the S Function Builder in simulink but I was very confused when it came to which libraries I should include. Also, I am not seeing how using an S function transfers data to and from the application because I do not know how the simulink model knows how, when and even where to look for the input data.
I have looked through much of the documentation on S functions but none seem to specifically outline how the process is done (or maybe I am interpreting some things wrong).
Some specifics:
- The input and output data I want to transfer is of type float/double/int16 (any type that can represent decimals)
-The inputs and outputs of the model are real numbers (no imaginary term)
-The number of inputs is two(2) and the number of outputs is also two(2).
-The data transfer rate is discrete (not sure if discrete is the technically correct term in this case so I will illustrate via example) e.g. a user should be able to input the values to the MFC program and recieve the output after processing by simulink. If he decides to leave for 5 minutes and then comes back and enters a new set of inputs, the results should be again readily available (like using a calculator)
- MATLAB version: R2008b
- Visual Studio Version: 2010 Ultimate
-The run of the program is dictated by the MFC application. The application should call the simulink model to do the processing when desired
Also, is it necessary to use the Visual Studio compiler or can I simply use the native MATLAB compiler?
Please advise if it will be necessary to upgrade my MATLAB version or downgrage my Visual Studio version
Any help/example code/flow diagram of data transfer process would be appreciated. Thank you.
댓글 수: 0
채택된 답변
Ken Atwell
2011년 11월 13일
MEX S-Functions are generally envisioned as being a way to call C/C++ functions from MATLAB, and you are looking to the opposite (call MATLAB/Simulink from a C++ program). The MATLAB Engine might be better aligned with what you need to do.
댓글 수: 4
Kaustubha Govind
2011년 11월 14일
I would recommend using the MATLAB Engine API (engPutVariable and engGetVariable) to write/read variables to/from the MATLAB workspace. You can then run your Simulink model using the MATLAB "sim" command and have your model read/write from the MATLAB workspace using the "From Workspace" and "To Workspace" blocks.
추가 답변 (1개)
Mike Woodward
2012년 6월 26일
Hi,
You might want to check out this free download: http://www.mathworks.com/programs/simulink-visual-studio/simulink-and-visual-studio.html
The download shows how to build a Visual Studio project that outputs a .mexw64 file. It shows how parameters are passed into C code.
Mike
댓글 수: 1
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!