Main Content

Control Real-Time Application by Using C# Code

This example shows how to develop a C# program that controls a Simulink Real-Time application by using the functions from the Simulink Real-Time XIL API support package. The C# example code shows how to use the XIL C# API calls to load, run, and stop a Simulink Real-Time application. The example code also shows how to record signal data.

Prepare for API in C# Program

1.Install the Simulink Real-Time XIL API support package by using the Add On Explorer.

2.Copy modelslrt_ex_pendulum_100Hzto your working folder.

open_system(fullfile(matlabroot,'toolbox/slrealtime/examples/slrt_ex_pendulum_100Hz.slx'));

3.Build modelslrt_ex_pendulum_100Hz.

model ='slrt_ex_pendulum_100Hz'; evalc(“slbuild(模型));

4.Create an XIL configuration file.

slrealtime.createPortConfigureFile("configFile.xml","192.168.7.5",'slrt_ex_pendulum_100Hz');

Create C# Program

1.Open Visual Studio 2019 and create a project for Console App (.NET core).

2.As project references in visual studio, addASAM.XIL.Implementation.Testbench.dll,ASAM.XIL.interfaces.dll, andMathWorks.ASAM.XIL.Server.dll. These files are available after you install the support package.

FindASAM.XIL.Implementation.Testbench.dllandASAM.XIL.Interfaces.dllin folderC:\Program Files (x86)\ASAM e.V\ASAM AE XIL API Standard Assemblies 2.1.0\bin.

FindMathWorks.ASAM.XIL.Server.dllin folderC:\ProgramData\MATLAB\SupportPackages\\toolbox\slrealtime\xil\src\bin\win64.

3.Copy the example C# programmyRealTimeAppController.cscontent to your current Visual Studio project. Update the project.

To find filemyRealTimeAppController.cs, open this example and view the example folder.

4.Build the solution in your Visual Studio project.

Run the C# Program

1.Run your application at the operating system command prompt. Enter:

appName configFilePath csvFilePath

The parts of this command are:

  • Application name

  • Full file path to your configuration file

  • Full file path of a CSV file in which the solution is saved

When you run the application, it loads and runs the Simulink Real-Time applicationslrt_ex_pendulum_100Hz.mldatxon the target computer. While running, the signal data for the signalsslrt_ex_pendulum_100Hz/Pendulum:1andslrt_ex_pendulum_100Hz:2are recorded for about 3 seconds. The data is saved into the CSV file that you selected. When done, the application stops on the target computer.

2.Check signal data saved in the CSV file.