C# code sample to use shared library (dll) generated from Embedded Coder

21 visualizaciones (últimos 30 días)
PSK
PSK el 31 de Ag. de 2020
Respondida: Vasco Lenzi el 25 de Sept. de 2020
Does anyone have C# code sample to use shared library (.dll - c/c++) 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).
We are able to marshal input/output c/c++ structs as C# structs 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.
=====================================================================================
Embedded coder generated DLL: would have following extern structs which are C/C++ structs
typedef struct {
real_T In1; /* '<Root>/In1' */
} ExtU_SumLogic_Sim_T;
/* External outputs (root outports fed by signals with default storage) */
typedef struct {
real_T Out1; /* '<Root>/Out1' */
} ExtY_SumLogic_Sim_T;
/* External inputs (root inport signals with default storage) */
extern ExtU_SumLogic_Sim_T SumLogic_Sim_U;
/* External outputs (root outports fed by signals with default storage) */
extern ExtY_SumLogic_Sim_T SumLogic_Sim_Y;
/* Model entry point functions */
extern void SumLogic_Sim_initialize(void);
extern void SumLogic_Sim_step(void);
extern void SumLogic_Sim_terminate(void);
=====================================================================================
C# code sample we tried to marshall the inputs and run the model:
using System;
using System.Runtime.InteropServices;
namespace X{
class Program{
private IntPtr inputIntPtr;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
// C# structs
struct SUM_U
{
public double IN1;
}
struct SUM_Y
{
public double OUT1;
}
[DllImport("SumLogicwin64.dll", CharSet = CharSet.Unicode)]
public static extern int SumLogic_Sim_initialize();
[DllImport("SumLogicwin64.dll", CharSet = CharSet.Unicode)]
public static extern int SumLogic_Sim_step();
[DllImport("SumLogicwin64.dll", CharSet = CharSet.Unicode)]
public static extern int SumLogic_Sim_terminate();
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr LoadLibrary(string lpszLib);
static void Main(string[] args){
int i = SumLogic_Sim_initialize();
IntPtr hdl = LoadLibrary("SumLogicwin64.dll");
IntPtr addr1 = GetProcAddress(hdl, "SumLogic_Sim_U");
IntPtr addr2 = GetProcAddress(hdl, "SumLogic_Sim_Y");
var s = (SUM_U)Marshal.PtrToStructure(addr1, typeof(SUM_U));
s.IN1 = 12.23; // NOT SURE IF THIS Input was actually set into the model or not
int j = SumLogic_Sim_step(); // Guess this will run the model
var output = (SUM_Y)Marshal.PtrToStructure(addr2, typeof(SUM_Y));
Console.WriteLine(output.OUT1); /// is not giving expected ANSWER; dont know if
int k = SumLogic_Sim_terminate();
}
}
}

Respuestas (1)

Vasco Lenzi
Vasco Lenzi el 25 de Sept. de 2020
hi PSK
after you posted this some links are added automatically into your right. One of this brings you to this documentation page:
Package Generated Code as Shared Libraries
You can see how they do it with C. My suggestion would be to look at the C code and adapt it to C#
You need to call the step function multiple times, and each time you do it the simulink algorithm advances of one step.
Hope this helps
Vasco

Categorías

Más información sobre Deployment, Integration, and Supported Hardware en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by