About legacy code support, External ctypes, and callbacks.
5 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!