Using Simulink® Strings

Simulink® strings are explicitly allowed and can be used with the TwinCAT Target for Simulink®.

Restriction

Depending on the MATLAB® release version, code interface packaging setting and the set C/C++ standard, the Simulink CoderTM compiles a Simulink® string into the std::string data type. If this Simulink® string is then used as a model input or model output, please note that these entries cannot be linked to other objects in TwinCAT by mapping. Mapping in TwinCAT assumes a static data type size, which is not the case with std::string.

Handling in TwinCAT

So that you can still use the inputs and outputs, it is recommended that the PLC-FB (no mappings necessary) or the TcCOM Wrapper FB are used. For the standard inputs and outputs of the FBs, the Simulink® string entries are not displayed in either case. These are to be set separately via getter and setter methods on the FB.

Using Simulink® Strings 1:

Simulink® bus with Simulink® strings: restricted use

The following situation is currently not supported: a Simulink® string cannot be used in a Simulink® bus that serves as input or output of the model if it is mapped as std::string by the Simulink CoderTM.

Example

Let's take the following Simulink® model with a mixture of string and non-string inputs and outputs.

Using Simulink® Strings 2:

When this model is compiled with MATLAB R2022a and "C++ Class" code interface packaging, the data type std::string is generated by the Simulink CoderTM. In the following figure, it can be seen that the string inputs and outputs are not present in the process image in TwinCAT.
Only the non-string inputs and outputs are present in the process image.

Using Simulink® Strings 3:

To be able to write and read the strings, either the TcCOM Wrapper FB or the PLC FB must be used. Corresponding getter and setter methods are automatically created at the function blocks.

Using Simulink® Strings 4:

Sample code using the PLC-FB:

VAR
   fbStringSample : FB_string_sample;
   
   myStringIn   : T_MaxString;
   myStringOut1 : T_MaxString;
   myStringOut2 : T_MaxString;
   nSize : ULINT;
   nSize2: ULINT;
   fIn   : LREAL;
   fOut  : LREAL;
END_VAR
// put sting input
fbStringSample.put_StringInput(c_str := ADR(myStringIn));

// call function
fbStringSample(fNonStringInput := fIn, fNonStringOut3 => fOut);

// get string outputs 
nSize := SIZEOF(myStringOut1);
fbStringSample.get_StringOut1(c_str := ADR(myStringOut1), size := nSize);  // size in VAR IN OUT!

nSize2 := SIZEOF(myStringOut2);
fbStringSample.get_StringOut2(c_str := ADR(myStringOut2), size := nSize2);