Strings

Die SPS-Datentypen STRING und WSTRING sind laut PLCopen-Mapping beide auf den OPC UA-Datentypen String gemappt. Der Unterschied liegt in der internen Speicherung:

Im Folgenden werden beide Datentypen erläutert. Genauere Details entnehmen Sie bitte der TwinCAT 3 PLC-Dokumentation.

STRING

Der Datentyp STRING kann in TwinCAT entweder als Latin-1 oder als UTF-8 interpretiert werden.
Die Interpretation wird über das Attribut {attribute 'TcEncoding'} gesteuert:

Beispiele:

// 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

Der Datentyp WSTRING folgt der UCS2-Kodierung (2 Byte pro Zeichen). Die Initialisierung erfolgt immer mit doppelten Anführungszeichen.

Beispiel:

wsMyWSTRING : WSTRING := "This is a WSTRING.";