Read/Write string types

ADS Server usually support strings in 2 flavors. The Default (ANSI) and the Unicode encoding (STRING vs. WSTRING) The ANSI encoding reserves 1 byte per character. Unicode reserves 2.

The strings are of fixed size and therefore the length of the the reserved space within the process image is important.

HowTo Read/Write string values

Reading writing ANSI Streams:

Read/Write ANSI Strings

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

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

    try
    {
    // Read ANSI String string[80]
    int byteSize = 81; // Size of 80 ANSI chars + /0 (STRING[80])
    AdsStream readStream = new AdsStream(byteSize); // Size of 80 ANSI chars + /0 (STRING[80])
    AdsBinaryReader reader = new AdsBinaryReader(readStream);
    client.Read(handle, readStream, 0, byteSize); // Read 81 bytes
    string value = reader.ReadPlcString(byteSize,Encoding.Default);

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

Reading writing UNICODE Streams:

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);
    }
}

Reading writing strings with ReadAny/WriteAny group of methods:

Read/Write Unicode Strings

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

    int stringHandle = client.CreateVariableHandle("MAIN.string"); // Symbol "string" defined in MAIN as STRING
    int wStringHandle = client.CreateVariableHandle("MAIN.wstring"); // Symbol "string" defined in MAIN as WSTRING

    try
    {
    string str = client.ReadAnyString(stringHandle, 80, Encoding.Default);
    string wStr = client.ReadAnyString(wStringHandle, 80, Encoding.Unicode);

    string changedValue = "Changed";

    // Attention, take care that the memory of the string in the process image is not exceeded!
    client.WriteAnyString(stringHandle, changedValue, 80, Encoding.Default);
    client.WriteAnyString(wStringHandle, changedValue, 80, Encoding.Unicode);
    }
    finally
    {
    client.DeleteVariableHandle(stringHandle);
    client.DeleteVariableHandle(wStringHandle);
    }
}