How to Define C Caller Function Argument as Input

3 次查看(过去 30 天)
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.
  1 个评论
John Wale
John Wale 2021-12-17
Update: I cut and pasted the same C-caller block into a new SImulink model, referenced the same source code and, in this model, it allows me to change the argument to input. Can anyone explain why the previous model would not allow the Port Specification to be changed?
Thanks!

请先登录,再进行评论。

回答(1 个)

Maneet Kaur Bagga
编辑:Maneet Kaur Bagga 2024-5-6
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!

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by