MATLAB Answers

0

Converting a simulink bus to bytes and back

Asked by Nathan Zimmerman on 9 Aug 2017
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.

  0 Comments

Sign in to comment.

0 Answers