Simulink xPC C-function static variable issues
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to implement a Simulink model for xPC, in which I need to call a C function. This C function has some static variables, so that it remembers the value from the last step at the new step (from last call to current call).
Now the problem:
It is one function. But I call it from TWO different Simulink S-function-creator blocks. I expected that two instances are created of this funcion's object, meaning the static variables in block 1 will not be mixed with those in block 2. But this is not the case.
Could anyone please give me some advice how to isolate these instances? I.e. I what to have two instances of my C code running, so that static variables are NOT mixed. The two blocks must work competely separately.
(Note: I ask for 2 blocks here, but the issue is valid for N blocks too, so I cannot just rename my C function, as I would end up having many C functions doing the same thing).
*********************************
---- C function ---- ..... int b; static int a[100]; ....
called from (with different inputs, not shown here) -- S-function-builder block 1 -- AND -- S-function-builder block 2 -- ...
************************************
Thank you for any advice. Istvan
0 个评论
回答(1 个)
Kaustubha Govind
2011-12-12
I would recommend declaring D-Work vectors in your S-functions, and work with that memory from your C-function (ie. modify it to use a passed-in pointer which will be your DWork memory, rather than static variables).
2 个评论
Kaustubha Govind
2011-12-13
There's a lot of S-function demos in the documentation (type "sfundemos" to see them by classification). sfcndemo_sfunmem seems like a good beginner example.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!