Simulink xPC C-function static variable issues

1 回表示 (過去 30 日間)
Istvan
Istvan 2011 年 12 月 12 日
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

回答 (1 件)

Kaustubha Govind
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 件のコメント
Istvan
Istvan 2011 年 12 月 13 日
Thanks you for your answer.
Yes, I saw the D-Work vectors just about now, but I am not proficient in S-function writing, so I used the S-function-builder block. There, however, there is no way of using D-Work vectors...
Do you maybe nkow a good source of a --simple-- demo for a really minimalistic C S-function using D-Work vectors?
Thanks again -
Kaustubha Govind
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 ExchangeSimulink Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by