Using simulink s-finctions to convert bus structures to byte arrays (and back). Can this be done?

7 views (last 30 days)
I have a non-virtual bus consisting of mixed signals (double and and integer). I need to convert this bus to an array of bytes (HW interface constraint). In C, I believe the easy way to do this is to define a union between the bus structure and a byte array. Using the s-function builder, is it possible to do something similar? My idea is to add something like this to the s-function builder "Outputs" section:
testbus *temp = NULL;
temp = u0;
y0=u0;
Upon building, you get:
void to_bytes_Outputs_wrapper(const testbus *u0,
testbus *y0, SimStruct *S)
{
testbus *temp = NULL;
temp = u0;
(y0)=(u0);
}
This doesn't work. Is there a way to make this work? Should I be looking at using the mxMalloc function?
Thanks, Martin

Answers (1)

MarkB
MarkB on 1 Mar 2011
It is important to remember that, even though it may have the same field names as the C "struct" that you have in mind, the data on the signal line for the S-function block isn't an instance of the "struct" type that you have in mind. The actual structure of buses on S-function ports is a little complicated, so I recommend that you "cheat" by using the Legacy Code Tool as a start.
If you have a bus object defined in the base workspace, you can use it as an inport data type for Legacy Code Tool to create an S-function for you. This will generate the code that accesses the bus object on the block's input port for you.
Since the data doesn't need to be a byte array until inside the S-function block (I'm inferring from your post a bit), you can write regular C code for the body of the S-function to do the field accessing and casting that you need to convert/cast it to a byte array inside. Simulink/bus objects have no concept of C "union" types, so you won't be able to define something like this in Simulink itself, but that won't stop you from being able to do it in C inside the S-function.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!