Transferring Strings Between Vortex and Simulink


We want to support transferring strings between Vortex and Simulink. While there is a "string" type in Simulink, it is not available in the public API and so cannot be incorporated directly into the Vortex Simulink block. To circumvent this limitation, we opt to treat strings in the Vortex block as vectors of unsigned 8 bit integers (uint8) and use the string blocks available in the Simulink library to transform, pad and display the strings appropriately. 

For the sake of instruction, I will consider the case of sending strings from Vortex to Simulink separately from the reverse case, as they are not entirely symmetric. In the end, I will combine everything into a single asset that QA can use to validate this feature.

Sending Strings from Vortex to Simulink

This is the easier case. All we need to do is convert the uint8 vector coming from Vortex to a Simulink string and display it. We do this using Simulink's "ASCII to String" and "Display" blocks, as illustrated in the model below:

To test this model, we create a mechanism in Vortex with a Simulink Block Extension and VHL interface that hardcodes a string (e.g. "hello from Vortex") and sends it to the input of the Simulink block extension via a connection container.

If everything is connected correctly, running the Simulink simulation should result in the string being displayed in the "Display" block:

Sending Strings from Simulink to Vortex

This case is a little bit trickier. We use the "String Constant" and "String to ASCII" blocks to produce the uint8 vector that will be sent to the Vortex block. However, the output port of the "String to ASCII" block drives a signal of variable size, whereas the ports in our Simulink block only accept signals of fixed size. Generalizing our block to allow variable size signals does not seem straightforward, so I opted to use a custom Matlab function block to truncate or pad the output of the "String to ASCII" block as needed to deliver a signal of fixed size (256) to the Vortex block.  

The contents of the custom Matlab function block are uninteresting. All we do is pad the signal with 0s or truncate it so that its size is 256.

Within Vortex, all we need to do is add an output field to our Simulink block extension, an input field to our VHL, and connect them in the connections container.

If everything is connected correctly, running the Simulink simulation should result in the string from Simulink being displayed in the Vortex Simulink extension output field.

The full Simulink model is illustrated below:

Limitations

The only limitation is that the string size is fixed, currently to 256 characters. We do not currently support allowing the user to change this hardcoded value, and I believe it may be more trouble than its worth to do so. If we find this size restrictive to many clients, we can just change it internally. The max size for strings that Simulink supports is 32,766, anyway, so in the worst case scenario we can just hardcode the buffer to accommodate this size.

Assets

For the sake of QA, the assets used in this document can be found here:

L:\Exchange\2AdamB\vortex-simulink-strings.7z

Ensure Vortex and Matlab are configured correctly, as described in the configuration section at the page below, before trying to validate this feature using the above assets.

Making a Co-Simulation Between Vortex and Simulink

References

https://www.mathworks.com/help/simulink/ug/simulink-strings.html