Converting a simulink bus to bytes and back

6 views (last 30 days)
Hello, I am attempting to C++ code-gen a matlab function block that converts a non-virtual bus to a byte array for storage in the OS. I also need to then convert this byte array back into a struct/bus. In C, this is easy to do with a struct and a union but i'd be nice to have a matlab solution that works for any bus else I mine as well just use C for everything.
I can convert the bus to bytes by using:
1. Getting and iterating through the fieldnames
2. Flattening the resulting data then typecasting to unit8
3. Concatenating this data to a variable sized byte array.
4. Write the byte array to the OS.
For converting bytes to bus I can:
1. Pull the byte array from the OS.
1. Get fieldnames for all the elements of the bus object
2. Determine the shape and byte size of a given bus entry
3. Pull the number of byte elements out of the buffer, cast it to the data type of the bus entry, then reshape it to be the same size as the bus.
While this process has worked for some buses I get random code gen errors depending upon the bus. Is there a better way to do this? Again keep in mind, it must code gen.

Answers (0)

Categories

Find more on Simulink Functions 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!