How to Define C Caller Function Argument as Input

5 visualizzazioni (ultimi 30 giorni)
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 Commento
John Wale
John Wale il 17 Dic 2021
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!

Accedi per commentare.

Risposte (1)

Maneet Kaur Bagga
Maneet Kaur Bagga il 6 Mag 2024
Modificato: Maneet Kaur Bagga il 6 Mag 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!

Categorie

Scopri di più su Simulink Functions in Help Center e File Exchange

Tag

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by