AdsBinaryReader.ReadPlcUnicodeString Method

Reads a PLC string from the current stream (Encoding.Unicode, Unicode Encoding)

Namespace:  TwinCAT.Ads
Assembly:  TwinCAT.Ads (in TwinCAT.Ads.dll) Version: 4.3.0.0

Syntax

C#

public string ReadPlcUnicodeString(
    int byteLength
)

VB

Public Function ReadPlcUnicodeString ( 
    byteLength As Integer
) As String

Parameters

byteLength

Type: System.Int32
The length of the string in the PLC (byte length equals character count on PLC + '\0')

Return Value

Type: String
The string being read (until the first '\0' character)

Remarks

The byte length of a STRING[80] in the PLC is 81. The byte length of a WSTRING[80] in the PLC is 162. Because of ANSI Encoding the number of Chars could differ with the number of Bytes (e.g on Double Byte Codepages DBCS, Codpage 932, Japan)

Examples

The following code shows how to Read/Write UNICODE string values..

Read/Write Unicode Strings

using (TcAdsClient client = new TcAdsClient())
{
    client.Connect(851); // Connect to local port 851 (PLC)

    int handle = client.CreateVariableHandle("MAIN.wstring"); // Symbol "wstring" defined in MAIN as WSTRING

    try
    {
    // Read UNICODE String wstring[80]
    int byteSize = 2 * 81; // Size of 80 UNICODE chars + /0 (WSTRING[80])
    AdsStream readStream = new AdsStream(byteSize); 
    AdsBinaryReader reader = new AdsBinaryReader(readStream);
    client.Read(handle, readStream, 0, byteSize); // Read 2*81 bytes
    string value = reader.ReadPlcString(byteSize,Encoding.Unicode);

    // Write ANSI String string[80]
    AdsStream writeStream = new AdsStream(byteSize);
    AdsBinaryWriter writer = new AdsBinaryWriter(writeStream);
    value = "Changed";
    writer.WritePlcString(value, 80,Encoding.Unicode); 
    client.Write(handle, writeStream, 0, byteSize);
    }
    finally
    {
    client.DeleteVariableHandle(handle);
    }
}

Reference

AdsBinaryReader Class

TwinCAT.Ads Namespace