Passing Strings Between Models

This example shows how to generate SimWB-compliant code for Simulink® models that exchange strings. The strings are converted to their ASCII equivalents and passed back and forth as uint8 arrays. Models exchange strings, for example, to simulate protocol handshakes and communicate commands.

Contents

Example Models

This example uses two models, ccur_outstring and ccur_instring, which are both shipped with the MLToolkit in the $simwbroot$/examples folder. Open these models by typing ccur_outstring and ccur_instring at the MATLAB command prompt.

ccur_outstring.mdl ccur_instring.mdl

In ccur_outstring, the value of three source signals is encoded, using strings to specify the header and footer of each source signal. The encoded signal, which contains strings and numbers, is converted to its ASCII equivalent and output as an uint8 array. ccur_instring decodes this signal and decomposes it to display each signal's value.

The models show two ways of exchanging the encoded signal:

Generate SimWB-Compliant Code

Because the two models share a signal, SWshareddata, before generating code using the MLToolkit GUI, you must modify the SimWB preferences. In the Simulink editor, select SimWB > Preferences. The SIMulation Workbench Preferences GUI opens. In the RTDB tab:

Following the workflow described in Generate Code for Simulink Model Using MLToolkit, generate SimWB-Compliant Code for ccur_outstring. Load the code to an RTDB named ccur_string. For the Create RTDB Variables option, use the default settings, which will create RTDB variables for all source/sink blocks with the SW prefix.

Next, generate code for ccur_instring and load it to the same RTDB, ccur_string. Similar to ccur_outstring, when you create the RTDB variables, use the default settings for the Create RTDB Variables option. When you click Create and Upload RTDB, the RTDB Append/Overwrite dialog appears. Click Append/Update.

The signal.db file on the SimWB host machine (located in the relevant RTDB folder) shows that the SWshareddata variable has the AIO point type. This point type is derived based on how the signal is used in both models. However, the signal.db file on the local machine (located in the current MATLAB work directory) does not reflect the overall point type for this signal. Rather, it reflects the point type for the signals as relevant to the last model used by the MLToolkit GUI, ccur_instring.

Now that you have appended the RTDB and generated code for ccur_instring, you must regenerate the code for the first model, ccur_outstring. Regenerating the code ensures that it uses the latest signal point types.

Run Test Session in SimWB

Open the Simulation Workbench Control Center on your local machine. Load the ccur_string RTDB. To inspect its analog signals, in the editor menu, select RTDB & I/O > Analog Points.

Analog points of ccur_string RTDB

Notice that the SWshareddata signal has the AIO point type. To inspect the string signals, in the editor menu, select RTDB & I/O > String Points.

String points of ccur_string RTDB

Map mystringin to mystringout using the RTDBItem-Copy I/O Mapping.

First, add the RTDBItem-Copy an I/O device. In the editor menu, select RTDB & I/O > I/O Devices. From the Available Devices list, select RTDBItem-Copy. Click Add . Then, click Apply .

Next, in the editor menu, select RTDB & I/O > I/O Mappings. Open the RTDBItem-Copy-1 tab. Select mystringin. On the right-hand-side, check the mystringout check box to map the two variables to each other. Then, click Apply .

Create a test for the ccur_string RTDB.

Test for the ccur_string RTDB

Run a session of this test. Open the Real-Time Viewer by selecting Monitor > RT Viewer in the editor menu. Add all the analog variables to a chart and all the string variables to a table. You observe that the analog signals are decoded as expected and the strings are detected as expected.

Limitations

Related Links

Blocks

Examples