C# code sample to use shared library (dll) generated from Embedded Coder
21 次查看(过去 30 天)
显示 更早的评论
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();
}
}
}
0 个评论
回答(1 个)
Vasco Lenzi
2020-9-25
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Deployment, Integration, and Supported Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!