Strings
According to the PLCopen mapping, the PLC data types STRING and WSTRING are both mapped to the OPC UA data types String. The difference lies in the internal storage:
- STRING: Characters are 1 byte long.
- WSTRING: Characters are 2 bytes long.
Both data types are explained below. For more details, please refer to the TwinCAT 3 PLC documentation.
STRING
The STRING data type can be interpreted in TwinCAT as either Latin-1 or UTF-8.
The interpretation is controlled via the {attribute 'TcEncoding'} attribute:
- Latin-1 is used without an attribute.
- UTF-8 is used with attribute.
Samples:
// Standard: Latin-1
sMyStringText : STRING := 'This is a STRING';
// UTF-8 from STRING
{attribute 'TcEncoding' := 'UTF-8'}
sMyUTF8Text1 : STRING := sLiteral_TO_UTF8('The dinner costs 30 €.');
// UFT-8 from WSTRING
{attribute 'TcEncoding' := 'UTF-8'}
sMyUTF8Text2 : STRING := wsLiteral_TO_UTF8("The dinner costs 30 €.");
// UFT-8 simple STRING
{attribute 'TcEncoding' := 'UTF-8'}
sMyUTF8Text3 : STRING := 'Hello World.';
WSTRING
The WSTRING data type follows the UCS2 coding (2 bytes per character). Initialization always takes place with double quotation marks.
Sample:
wsMyWSTRING : WSTRING := "This is a WSTRING.";