How to Define C Caller Function Argument as Input
Show older comments
Hello,
I've been having a problem with the C-Caller block in that it always thinks my function argument is an output. The function accepts a pointer to a structure and outputs a calculated result. Here's the function declaration and the definition of the input structure that it uses:
typedef struct
{
unsigned char data[10];
unsigned short numbytes;
} CRC_In;
unsigned short Calc_CRC(CRC_In* input);
The "C-Caller" block accepts the function ok, but insists on defining CRC_In* as an Output in the 'Port Specification'. Although this can be manually changed to 'Input' from the drop-down list, clicking on 'Apply' sets it back to 'Output'. Please could anyone suggest how to tell Simulink that the function argument is an input and not an output?
On a probably related note, when I try to compile the model, I get the error message 'Need to specify the exact size of the output argument 'in0' for the C Caller block'. I would have expected Simulink to be able to work out the structure size from its typedef declaration. Why is this not so?
Thanks,
John.

Answers (1)
Maneet Kaur Bagga
on 6 May 2024
Edited: Maneet Kaur Bagga
on 6 May 2024
Hi John,
As per my understanding, there is an error encountered while configuring a C Caller block in Simulink.
One of the possible workaround for the same could be:
Create a wrapper function around "Calc_CRC" that deals with the size problem. The function could allocate the memory for the structure and pass its size to Simulink. The wrapper function will simplify the interface for Simulink by dealing with raw arrays and size parameter.
Please refer to the code snippet below for reference.
unsigned short Calc_CRC_Wrapper(unsigned char* data, unsigned short numbytes) {
CRC_In input;
memcpy(input.data, data, numbytes);
input.numbytes = numbytes;
return Calc_CRC(&input);
}
Please refer to the MathWorks Documentation for further understanding:
Hope this helps!
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!