About legacy code support, External ctypes, and callbacks.

조회 수: 4 (최근 30일)
Charles Coulton
Charles Coulton 2023년 5월 24일
답변: Shubham 2023년 6월 1일
I'm trying to work out if between Simulink.importExternalCTypes, and legacy code support, if I can get a c-caller or sfunction to work.
We have 6 enums, and structs in our legacy code, 2 enums import fine, 2 of the structs import as bus, the last 2 are composite structs that contain the previous bus types. Could it be possiable to manually define those other 2 structs as bus types? Example of structs I'm talking about.
typedef struct {
uint32 id;
uint8 length;
uint8 data[8];
}packet; // imports as a .bus fine
typedef struct {
uint8 ridx;
uint8 widx;
uint8 bufferLen;
packet* buffer;
}packetbuffer; // fails to import as a .bus because it doesn't use default types.
Would legacy code support help with a situation like this?
Also we have a system, that can be called from simulink with a C-caller, does some processing. As processing happens it hands execution back to application desenated point on certain events. How would one drive signals from blocks from this callback/event; if possiable?

답변 (1개)

Shubham
Shubham 2023년 6월 1일
Hi Charles,
Yes, it is possible to manually define the struct which doesn't import as a bus using default types. You can define a custom bus object that contains all the fields in the structure and use that bus object.
Here is an example of how you can manually define the packetbuffer struct as a bus object:
% Define the packet bus object
PacketBus = Simulink.Bus.createObject(packet);
% Define the packetbuffer bus object
PacketBufferBus = Simulink.Bus;
PacketBufferBus.Elements = {
Simulink.BusElement('ridx', 'uint8'),...
Simulink.BusElement('widx', 'uint8'),...
Simulink.BusElement('bufferLen', 'uint8'),...
Simulink.BusElement('buffer', 'BusObject', PacketBus),...
};
Regarding the second question, you can drive signals from blocks from the callback/event using Simulink's Signal-Based Function support feature. You can create a Simulink subsystem that contains the code that needs to be executed during the callback/event and connect the input and output signals of the subsystem to the relevant blocks. Then, in the callback/event, you can invoke this subsystem using the sim command. Here's an example:
function myCallback(block, event)
% Block callback function
% Invoke the subsystem to execute the code
sim('mySubsystem');
end
In this example, mySubsystem is the Simulink subsystem that contains the code that needs to be executed during the callback/event.

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by