How to Share Buffer Between C Function Code (C Caller, C Function, S Functions)?
8 次查看(过去 30 天)
显示 更早的评论
Hi, a little bit of context about what I'm trying to achieve.
I have a shared list of objects that needs to be accessed by multiple C functions (for example Insert, Remove, Get at X index, etc). I tried to make this work using C Caller blocks, but the behaviour I am observing is that each C Caller block has its own list even though it is static and declared globally in a shared header.
So I am trying to share the state of some variables between multiple C functions. I read about S Functions, C Functions block and C Caller block, but I haven't found a clear way to do it. Work vectors look interesting, but they don't seem to be supporting complex types like structures.
Thank you very much for your time.
0 个评论
回答(2 个)
Pratyush
2023-7-31
I understand that you are looking to share the state of variables between multiple C functions in MATLAB, one possible solution is to use the S-Function block.
To share complex types like structures between multiple C functions, you can use pointers. Here's a general outline of how you can achieve this:
1. Create a structure that represents your list of objects in MATLAB. For example:
% MATLAB code
list = struct('objects', cell(1, N));
2. Write a C function that takes a pointer to the list structure as an argument and performs the desired operations (insert, remove, etc.). This function should be declared as an external function in MATLAB.
/* C code */
void modifyList(struct List* list) {
// Perform operations on the list
}
3. Create an S-Function block in Simulink and write the C code that implements the desired functionality. You can use the S-Function template provided by MATLAB as a starting point.
4. In the S-Function code, include the header file that defines the structure and the C function. Then, declare a pointer to the list structure and pass it to the C function.
/* S-Function code */
#include "list.h"
void mdlInitializeSizes(SimStruct *S) {
// ...
}
void mdlStart(SimStruct *S) {
// Allocate memory for the list structure
struct List* list = malloc(sizeof(struct List));
// Pass the pointer to the C function
modifyList(list);
// ...
}
void mdlOutputs(SimStruct *S, int_T tid) {
// ...
}
void mdlTerminate(SimStruct *S) {
// Free the memory allocated for the list structure
free(list);
}
5. Build the S-Function and use it in your Simulink model. The S-Function block will have access to the shared list structure, and you can perform operations on it using the C functions.
Note that this approach requires you to manually manage memory allocation and deallocation for the list structure.
Roy Mathew
2023-8-3
For the C caller block, if you are using a shared header to specify static globals, the objects would be shared by all C Callers and C Function blocks in the same model.
If you are using C Caller blocks in multiple models and would like to share the same headers, you can use a Simulink Library model and specify the Headers/Sources in Modeling->Simulation Custom Code setting. All the C callers can be defined in the library model. The C caller blocks in the library can then be used across multiple models by creating links and the global data would be shared.
This page has an example that uses C Caller blocks from a library model.
https://www.mathworks.com/help/simulink/slref/bring-custom-image-filter-algorithms-as-reusable-blocks-in-simulink.html
0 个评论
另请参阅
类别
在 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!