Matlab with external C function: coder.ceval pass a pointer to a structure (including arrays) to a external function
7 次查看(过去 30 天)
显示 更早的评论
Hello,
I kinda want to do a similar thing as here , exept that the structure I want to pass includes vectors or arrays. Therefore the code is not working.
In general I try to write a driver block for Simulink for UART communication for a raspberry pi with a Matlab System. The UART code is working fine.
In my System code I create a struct and fill it with the inputs of the System. Now I want to pass a pointer to that struct to my extern send function (with some other arguments).
Here is the Code:
toFluco_struct = struct('velocity',u1,'heading',u2);
coder.cinclude('piData.h');
coder.cstructname(toFluco_struct, 'piData_toFluco_t', 'extern');
coder.ceval('pidata_send', obj.fd, obj.packetID, coder.ref(piData_toFluco), obj.size);
piData.h
typedef struct { float velocity [3]; float heading [5]; } piData_toFluco_t;
void pidata_send(int uartsocket, uint8_t packet_id, piData_toFluco_t* packet_ptr, uint8_t size);
piData.c
void pidata_send(int uartsocket, uint8_t packet_id, piData_toFluco_t* packet_ptr, uint8_t size){
uart_send(uartsocket, "\xff\xff\xff\xff", 4); // Start-Byte
uart_send(uartsocket, &packet_id, 1); //Packet ID senden
uart_send(uartsocket, packet_ptr, size); //Payload senden
I'm getting the following error message when compiling:
Simulink detected an error 'coder.ref may only be applied to an expression of type V or V(E) where V is a variable and E is a numeric expression.'.
I would appreciate any help
Cheers Michael
0 个评论
回答(1 个)
Akshat Dalal
2025-3-2
Hi Michael,
The error message you're encountering suggests that the coder.ref function is being used incorrectly. This function requires a variable, not an expression or a temporary value, as its argument. You will have to create a variable of the new struct type and then pass it as an argument to coder.ref. Here is how the updated code might look:
coder.cstructname(toFluco_struct, 'piData_toFluco_t', 'extern');
piData_toFluco = toFluco_struct; % Create a variable to hold the structure
coder.ceval('pidata_send', obj.fd, obj.packetID, coder.ref(piData_toFluco), obj.size); % Call the external C function
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Development Computer Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!